cg
cg
cg
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{int input;
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
scanf("%d",&x1);
scanf("%d",&y1);
scanf("%d",&x2);
scanf("%d",&y2);
x=x1; y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=((2*dy)-dx);
while(x<=x2)
if(p<2){
x=x+1;
p=2*x-dx;
else{
x=x+1;
y=y+1;
p=p+2*dy;
putpixel(x,y,7);
getch();
closegraph() ;
}
Output-
Practical No -2
Write a program to draw a line using Bresenham's Line (BLA) algorithm.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}
Output-
Practical No -3
Write a program to draw a circle using Bresenham's Circle Algorithm.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
if(d<=0)
d=d+(4*x)+6;
}
else
d=d+(4*x)-(4*y)+10;
y=y-1;
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
int main(void)
errorcode = graphresult();
getch();
scanf("%d%d",&xc,&yc);
BresenhamCircle(xc,yc,r);
getch();
closegraph();
return 0;
}
Output-
Practical No -4
Write a program to draw a circle using Mid Point Circle Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\TC\\BGI");
initgraph(&g_driver,&g_mode,"C:\\TC\\BGI");
printf("********* MID POINT ELLIPSE ALGORITHM *********");
printf("\n\n Enter coordinate x and y = ");
scanf("%ld%ld",&x_center,&y_center);
printf("\n Now enter constants a and b = ");
scanf("%ld%ld",&a,&b);
x=0;
y=b;
a_sqr=a*a;
b_sqr=b*b;
fx=2*b_sqr*x;
fy=2*a_sqr*y;
d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
do
{
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d<0)
{
d=d+fx+b_sqr;
}
else
{
y=y-1;
d=d+fx+-fy+b_sqr;
fy=fy-(2*a_sqr);
}
x=x+1;
fx=fx+(2*b_sqr);
delay(10);
}
while(fx<fy);
tmp1=(x+0.5)*(x+0.5);
tmp2=(y-1)*(y-1);
d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
do
{
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d>=0)
d=d-fy+a_sqr;
else
{
x=x+1;
d=d+fx-fy+a_sqr;
fx=fx+(2*b_sqr);
}
y=y-1;
fy=fy-(2*a_sqr);
}
while(y>0);
getch();
closegraph();
}
Output-
Practical No -6
Write a program to draw a rectangle shape using Flood Fill algorithm.
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
void floodfill(intx,inty,intold,intnewcol)
{
int current;
current=getpixel(x,y);
if(current==old)
{
delay(5);
putpixel(x,y,newcol);
floodfill(x+1,y,old,newcol);
floodfill(x-1,y,old,newcol);
floodfill(x,y+1,old,newcol);
floodfill(x,y-1,old,newcol);
floodfill(x+1,y+1,old,newcol);
floodfill(x-1,y+1,old,newcol);
floodfill(x+1,y-1,old,newcol);
floodfill(x-1,y-1,old,newcol);
}
}
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(50,50,150,150);
floodfill(70,70,0,15);
getch();
closegraph();
}
Output-
Practical No -7
Write a program to draw a circle shape using Boundary Fill algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
//driver code
int main()
{
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main ()
{
int W_xmax, W_ymax, W_xmin, W_ymin;
int V_xmax, V_ymax, V_xmin, V_ymin;
float sx, sy;
int x, x1, x2, y, y1, y2;
int gr = DETECT, gm;
initgraph (&gr, &gm, "C:\\TC\\BGI");
printf ("\n****** Window to Viewport ***********\n");
printf ("Enter the coordinates for triangle \n x and y = ");
scanf ("%d %d", &x, &y);
printf ("\n x1 and y1 = ");
scanf ("%d %d", &x1, &y1);
printf ("\n x2 and y2 = ");
scanf ("%d %d", &x2, &y2);
printf ("Please enter Window coordinates \n First enter XMax, YMax =");
scanf ("%d %d", &W_xmax, &W_ymax);
printf ("\n Now, enter XMin, YMin =");
scanf ("%d %d", &W_xmin, &W_ymin);
cleardevice ();
delay (50);
//Window
rectangle (W_xmin, W_ymin, W_xmax, W_ymax);
outtextxy (W_xmin, W_ymin - 10, "Window");
//drawing a triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
// viewport
V_xmin = 300;
V_ymin = 30;
V_xmax = 550;
V_ymax = 350;
rectangle (V_xmin, V_ymin, V_xmax, V_ymax);
outtextxy (V_xmin, V_ymin - 10, "Viewport");
// calculatng Sx and Sy
sx = (float) (V_xmax - V_xmin) / (W_xmax - W_xmin);
sy = (float) (V_ymax - V_ymin) / (W_ymax - W_ymin);
x = V_xmin + (float) ((x - W_xmin) * sx);
x1 = V_xmin + (float) ((x1 - W_xmin) * sx);
x2 = V_xmin + (float) ((x2 - W_xmin) * sx);
y = V_ymin + (float) ((y - W_ymin) * sy);
y1 = V_ymin + (float) ((y1 - W_ymin) * sy);
y2 = V_ymin + (float) ((y2 - W_ymin) * sy);
// drawing triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
getch ();
closegraph ();
}
Output-
Practical No -9
Write a program to clip a line segment using 4-bit code algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
printf("\nEnter x1 and y1\n");
scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:\\TC\\BGI");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void main()
int gd=DETECT,gm;
int x=10,y=480;
initgraph(&gd,&gm,"..\\bgi");
while(!kbhit())
cleardevice();
if(y==0)
y=random(480);
x=random(640);
else
{
y=y-1;
x=x+1;
line(x-50,y,x,y-70);
line(x,y-70,x+50,y);
line(x+50,y,x,y+70);
line(x,y+70,x-50,y);
line(x,y-70,x,y+70);
line(x,y+70,x+10,y+140);
line(x,y+70,x-10,y+140);
line(x-50,y,x+50,y);
line(x,y,x+130,y+640);
delay(20);
closegraph();
restorecrtmode();
}
Output-
Practical No -12
Write a program to rotate a line about it's mid point.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int gd=DETECT,gm;
int pivot_x,pivot_y,x,y;
double degree,radian;
int rotated_point_x,rotated_point_y;
initgraph(&gd,&gm,"C://TC//BGI");
cleardevice();
scanf("%d %d",&pivot_x,&pivot_y);
scanf("%d %d",&x,&y);
line(pivot_x,pivot_y,x,y);
scanf("%lf",°ree);
radian=degree*0.01745;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));
rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED);
line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);
getch();
closegraph();
}
Output-
Practical No -13
Write a program that shows a scene of eclipse.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Initialize center of ellipse with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
getch();
closegraph();
return 0;
}
Output-
Practical No -14
Write a program that translate and rotate a circle along a horizontal
line.
Output-
Practical No -15
Write a program to rotate an ellipse about its axis alternatively.
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
putpixel(xc + x, yc - y, color);
}
}
void slidePattern(int xc, int yc, int r, int a, int b,int alpha, float p, int color)
{
setcolor(color);
float t = 3.14 / 180;
float t1, t2, d;
float angle = (p * alpha);
drawEllipse(draw_x, draw_y, a,
b, draw_ang, color);
}
void ellipseovercircle(int xc, int yc,
int r, int a, int b)
{
float theta = 0;
double h, p1;
h = (a * a) + (b * b);
h /= 2;
p1 = sqrt(h);
p1 /= r;
p1 = 1 / (p1);
// Driver code
int main()
{
// Initialize graphics function
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
closegraph();
return 0;
}
Output-