Professional Documents
Culture Documents
Program To Draw A Line Using Dda Algorithm
Program To Draw A Line Using Dda Algorithm
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
float round(float a);
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
clrscr( );
printf("Enter the coordinates of starting point:");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates of ending point:");
scanf("%d%d",&x2,&y2);
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
dx = x2-x1;
dy = y2-y1;
if(abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xincr = dx/steps;
yincr = dy/steps;
x = x1;
y = y1;
putpixel(x,y,WHITE);
for(k=1;k<=steps;k++)
{
delay(100);
x = x+xincr;
y = y+yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"ILLUSTRATION OF DDA ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}
float round(float a)
{
int b = a+0.5;
return(floor(b));
}
OUTPUT :
Enter the coordinates of starting point:200
100
Enter the coordinates of ending point:300
100
y = y+1;
putpixel(x,y,15);
p = p+2*dy-2*dx;
}
}
outtextxy(200,10,"ILLUSTRATION OF BRESENHAM'S ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT :
Enter the coordinates of starting point:200
100
Enter the coordinates of ending point:300
180
p = p+2*x+1-2*y;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT CIRCLE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT:
Enter the coordinates of center of circle:320
240
Enter the radius of circle:100
s = s+4*(x-y)+10;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF BRESENHAM'S CIRCLE
ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT :
Enter the coordinates of center of circle:300
200
Enter the radius of circle:80
{
y--;
fy = fy-aa2;
p = p+fx+bb-fy;
}
symmetry(xc,yc,x,y);
}
p = bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;
while(y>0)
{
y--;
fy = fy-aa2;
if(p>=0)
p = p-fy+aa;
else
{
x++;
fx = fx+bb2;
p=p+fx-fy+aa;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT ELLIPSE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
}
OUTPUT:
Enter the coordinates of center of ellipse:320
240
Enter the x-radius and y-radius of ellipse:100
80
y1 = x1*sin(a)+y1*cos(a);
x2 = x2*cos(a)-y2*sin(a);
y2 = x2*sin(a)+y2*cos(a);
getch( );
cleardevice( );
outtextxy(250,20,"LINE AFTER ROTATION");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
case 2: printf("Enter the fixed point:");
scanf("%f%f",&x,&y);
cleardevice( );
/* draw the original line */
outtextxy(250,20,"LINE BEFORE ROTATION");
line(x1,y1,x2,y2);
a = a*(3.14/180);
x1 = x1*cos(a)-y1*sin(a)-x*cos(a)+y*sin(a)+x;
y1 = x1*sin(a)+y1*cos(a)-x*sin(a)-y*cos(a)+y;
x2 = x2*cos(a)-y2*sin(a)-x*cos(a)+y*sin(a)+x;
y2 = x2*sin(a)+y2*cos(a)-x*sin(a)-y*cos(a)+y;
getch( );
cleardevice( );
outtextxy(250,20,"LINE AFTER ROTATION");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
1.Rotation about origin
2.Rotation about fixed point
Enter your choice:0 for exit :1
Enter the value of line coordinates:200
100
320
220
Enter the value of angle of rotation:20
x1 = x1*a;
y1 = y1*b;
x2 = x2*a;
y2 = y2*b;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SCALING");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
case 2: printf("Enter the fixed point:");
scanf("%d%d",&h,&k);
cleardevice( );
/* draw the original line */
outtextxy(200,20,"LINE BEFORE SCALING");
line(x1,y1,x2,y2);
x1 = x1*a-h*a+h;
y1 = y1*b-k*b+k;
x2 = x2*a-h*a+h;
y2 = y2*b-k*b+k;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SCALING");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
1.Scaling about origin
2.Scaling about fixed point
Enter your choice:0 for exit
1
Enter the value of line coordinates:
100
100
180
180
Enter the value of x-scaling factor:2
Enter the value of y-scaling factor:1
/*TRANSLATION OF LINE */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,a,b;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter the value of line coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the value of x-translation factor:");
scanf("%d",&a);
printf("Enter the value of y-translation factor:");
scanf("%d",&b);
cleardevice( );
outtextxy(200,20,"LINE BEFORE TRANSLATION");
line(x1,y1,x2,y2);
x1 = x1+a;
y1 = y1+b;
x2 = x2+a;
y2 = y2+b;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER TRANSLATION");
line(x1,y1,x2,y2);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
Enter the value of line coordinates:
200
100
350
100
Enter the value of x-translation factor:50
Enter the value of y-translation factor:100
/* SHEARING OF LINE */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,a,b;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter the value of line coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the value of x-shearing factor:");
scanf("%d",&a);
printf("Enter the value of y-shearing factor:");
scanf("%d",&b);
cleardevice( );
outtextxy(200,20,"LINE BEFORE SHEARING");
line(x1,y1,x2,y2);
x1 = x1+a*y1;
y1 = b*x1+y1;
x2 = x2+a*y2;
y2 = b*x2+y2;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SHEARING");
line(x1,y1,x2,y2);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
Enter the value of line coordinates:
20
20
100
100
Enter the value of x-shearing factor:1
Enter the value of y-shearing:1
break;
case 3: printf("Enter the arbitary line:");
scanf("%f%f%f%f",&x3,&y3,&x4,&y4);
cleardevice( );
/* draw the original line */
outtextxy(200,10,"LINE BEFORE REFLECTION");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
m = (y4-y3)/(x4-x3);
b = y3-m*x3;
n = m*m+1;
x1 = (1-m*m)*x1/n+2*m*y1/n-2*b*m/n;
y1 = (m*m-1)*y1/n+2*m*x1/n+2*b/n;
x2 = (1-m*m)*x2/n+2*m*y2/n-2*b*m/n;
y2 = (m*m-1)*y2/n+2*m*x2/n+2*b/n;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
1.Reflection about x-axis
2.Reflection about y-axis
3.Reflection about arbitary line
Enter your choice:0 for exit
1
Enter the value of line coordinates:
10
10
100
100
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"LINE AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
OUTPUT:
Enter the endpoints of line
100
80
470
340
Enter the rectangular coordinates of clipping window
180
150
300
280
cleardevice( );
poly[2*n] = poly[0];
poly[2*n+1] = poly[1];
/* draw the polygon */
drawpoly(n+1, poly);
rectangle(wxmin,wymin,wxmax,wymax);
sx = (vxmax-vxmin)/(wxmax-wxmin);
sy = (vymax-vymin)/(wymax-wymin);
for(i=0;i<n;i++)
{
poly1[2*i] = sx*(poly[2*i]-wxmin)+vxmin;
poly1[2*i+1] = sx*(poly[2*i+1]-wymin)+vymin;
}
poly1[2*n] = poly1[0];
poly1[2*n+1] = poly1[1];
rectangle(vxmin,vymin,vxmax,vymax);
outtextxt(150,10, WINDOW TO VIEWPORT TRANSFORMATION);
drawpoly(n+1,poly1);
/* clean up */
getch( );
closegraph( );
return 0;
}
OUTPUT:
Enter the coordinates of window
wxmin = 30
wymin = 30
wxmax = 150
wymax = 150
Enter the coordinates of viewport
vxmin = 160
vymin = 160
vxmax = 450
vymax = 450
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
INDEX
1. Program to draw a line : DDA Algorithm.
2. Program to draw a line : Bresenhams Algorithm.
3. Program to draw a circle : Midpoint Circle Algorithm.
4. Program to draw a circle : Bresenhams Circle Algorithm.
5. Program to draw an ellipse : Midpoint Ellipse Algorithm.
6. Rotation of line about origin and fixed point.
7. Scaling of line about origin and fixed point.
8. Translation of line.
9. Shearing of line.
10. Reflection of line about X-axis,Y-axis and arbitary line.
11. Program to clip a line : Cohen Sutherland Algorithm.
12. Window to Viewport transformation.