Program To Draw A Line Using Dda Algorithm
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>
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 :
#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,dx,dy,p,k,x,y;
clrscr( );
dx = x2-x1;
dy = y2-y1;
p = 2*dy-dx;
x = x1;
y = y1;
putpixel(x,y,15);
for(k=1;k<=dx;k++)
{
if(p<0)
{
x = x+1;
putpixel(x,y,15);
p = p+2*dy;
}
else
{
x = x+1;
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 :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0;
y = r;
symmetry(xc,yc,x,y);
p = 1-r;
while(x<=y)
{
if(p<0)
{
x = x+1;
p = p+2*x+1;
}
else
{
x = x+1;
y = y-1;
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:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0;
y = r;
symmetry(xc,yc,x,y);
s = 3-2*r;
while(x<=y)
{
if(s<0)
{
x = x+1;
s = s+4*x+6;
}
else
{
x = x+1;
y = y-1;
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 :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0; /*starting point*/
y = b;
symmetry(xc,yc,x,y);
fx = 0; /*initial partial derivatives*/
fy = aa2*y;
p = bb-aa*b+(0.25*aa); /*compute and round off p1*/
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode,ch ;
float x1,y1,x2,y2,a,x,y;
switch(ch)
{
case 1: 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);
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;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
#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,h,k,ch;
switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(200,20,"LINE BEFORE SCALING");
line(x1,y1,x2,y2);
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;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
#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;
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#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;
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
float x1,y1,x2,y2,x3,y3,x4,y4,m,n,b;
int ch,x,y;
do
{
printf("\n######### MAIN MENU #########\n");
printf("\n1.Reflection about x-axis\n");
printf("2.Reflection about y-axis\n");
printf("3.Reflection about arbitary line\n");
printf("Enter your choice:0 for exit\n");
scanf("%d",&ch);
if(ch==0)
{
getch( );
exit(1);
}
switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(250,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2;
y1 = y1+y/2-2*y1;
x2 = x2+x/2;
y2 = y2+y/2-2*y2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;
case 2: cleardevice( );
/* draw the original line */
outtextxy(200,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2-2*x1;
y1 = y1+y/2;
x2 = x2+x/2-2*x2;
y2 = y2+y/2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode ;
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main( )
{
int wxmin,wymin,wxmax,wymax,vxmin,vymin,vxmax,vymax ;
/* request auto detection */
int gdriver = DETECT, gmode , i , n, poly[14],poly1[14];
float sx,sy;
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:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
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);
OUTPUT:
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300
INDEX
1. Program to draw a line : DDA Algorithm.
2. Program to draw a line : Bresenham’s Algorithm.
3. Program to draw a circle : Midpoint Circle Algorithm.
4. Program to draw a circle : Bresenham’s 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.