0% found this document useful (0 votes)
1K views

Dda Line Drawing Algorithm

The document contains code snippets for computer graphics algorithms including DDA line drawing, translation and scaling of triangles, bouncing a ball within a box, circle drawing using Bresenham's algorithm, line drawing using Bresenham's algorithm, and circle drawing using the midpoint circle algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Dda Line Drawing Algorithm

The document contains code snippets for computer graphics algorithms including DDA line drawing, translation and scaling of triangles, bouncing a ball within a box, circle drawing using Bresenham's algorithm, line drawing using Bresenham's algorithm, and circle drawing using the midpoint circle algorithm.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

/* DDA LINE DRAWING ALGORITHM */

#include<stdio.h>#include<conio.h>#include<graphics.h>

#include<math.h>int round(float); void main()


{int gd=DETECT,gm,i; float dx,dy,xb,xa,yb,ya,steps;
float xinc,yinc,x,y; printf("\n\t\tENTER THE
STRATING COORDINATES");
printf("\n\n\t\t\tENTER Xa :: "); scanf("%f",&xa);
printf("\t\t\tENTER Ya :: "); scanf("%f",&ya);
printf("\n\n\t\tENTER THE ENDING CORDINATES");
printf("\n\n\t\t\tENTER Xb :: "); scanf("%f",&xb);
printf("\t\t\tENTER Yb :: "); scanf("%f",&yb);
initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice();
setbkcolor(CYAN); setcolor(BLUE);
ya = getmaxy() - ya; yb = getmaxy() - yb; dx = xb - xa;
dy = yb - ya; if(abs(dx) > abs(dy)),,,,,steps = abs(dx);
else,,,,steps = abs(dy); xinc = dx / steps; yinc = dy /
steps; x = xa; y = ya; putpixel(round(x),round(y),1);
for(i=1;i<=steps;i++),,{,,x += xinc; y += yinc;
putpixel(round(x),round(y),1); delay(5); },,, getch();
closegraph(); },,,int round(float x),,{,,,,if((x - floor(x))
>= 0.5),,,return(ceil(x)); else,,,,return(floor(x));}

//

Translation vector from the user and translate the triangle accordingly//

#include<stdio.h>#include<conio.h>#include<graphics.h>

void main(),,{,,int gdriver=DETECT;


int gmode,i,j,k,a[3][3],b[3][3],c[3][3],tx,ty;
initgraph(&gdriver,&gmode," "); printf("\nEnter the
translation vector tx: "); scanf("%d",&tx);
printf("\nEnter the translation vector ty: ");
scanf("%d",&ty); for(i=0;i<=2;i++),,{,,
for(j=0;j<=2;j++),,{,, if(i==j),,a[i][j]=1; else,,a[i][j]=0;
a[0][2]=tx; a[1][2]=ty;}},,,printf("\nEnter the
coordinates points of triangle:\n"); for(i=0;i<=2;i++)
{,,for(j=0;j<=2;j++),, {,,if(i==2),,b[i][j]=1; else
scanf("%d",&b[i][j]); }}//matrix multiplication
for(i=0;i<=2;i++),,{,, for(j=0;j<=2;j++),,{,, c[i][j]=0;
for(k=0;k<=2;k++),,{,,c[i][j]+=(a[i][k]*b[k][j]); }}}
printf("\nTranslated Coordinates are:\n");
for(i=0;i<=2;i++),,{,, for(j=0;j<=2;j++),,{..
printf("%d",c[i][j]); printf("\t"); },, rintf("\n");}
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][2],c[1][2],c[0][0],c[1][0]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);getch();closegraph();}
///hit ball wall//
#include<stdio.h>#include<conio.h>#include<graphics.h>

void main() {int gdriver=DETECT,gmode,error code;


initgraph(&gdriver,&gmode," "); setcolor(YELLOW);
int i,j; for(j=0;j<5;j++) { for(i=0;i<177;i++){
line(200,200,400,200);line(200,350,400,350);
line(200,200,200,350);line(400,200,400,350);
circle(212+i,210,10); delay(5);cleardevice();}
for(i=0;i<127;i++) { line(200,200,400,200);
line(200,350,400,350);line(200,200,200,350);
line(400,200,400,350);circle(388,212+i,10);
delay(5);cleardevice();} for(i=0;i<177;i++)
{ line(200,200,400,200); line(200,350,400,350);
line(200,200,200,350);line(400,200,400,350);
circle(388-i,340,10); delay(5);cleardevice();}
for(i=0;i<127;i++) { line(200,200,400,200);
line(200,350,400,350);line(200,200,200,400);
line(400,200,400,350);circle(340-i,212,10);
delay(5); cleardevice();}getch();closegraph();}}

//////Circle using Bresenham


# include<stdio.h># include<conio.h># include<graphics.h>
void plotcircle(int xc,int yc,int x,int y,int r)
{,,,putpixel(xc+x,yc+y,r);putpixel(xc-x,yc+y,r);
putpixel(xc+x,yc-y,r); putpixel(xc-x,yc-y,r);
putpixel(xc+y,yc+x,r);putpixel(xc-y,yc+x,r);
putpixel(xc+y,yc-x,r); putpixel(xc-y,yc-x,r);}
void bres_circle(int xc,int yc,int r)
{,,,int x,y,p; x=0; y=r; p=3-2*r; while(x<y),,,{,,,
plotcircle(xc,yc,x,y,r); if(p<=0),,, p=p+4*x+6; else,,{
,,p=p+4*(x-y)+10; y=y-1; },,,x=x+1; },,if(x==y),,
plotcircle(xc,yc,x,y,r); },,,,,void main(),,{,,int rad,x,y;
int gdriver=DETECT; int gmode; //clrscr();
initgraph(&gdriver,&gmode," "); printf("\n\t Enter the
radius : "); scanf("%d",&rad); printf("\n\t Enter the
centre(x,y) : "); scanf("%d %d",&x,&y); bres_circle(x,y,rad);
getch();closegraph();}

//////line using Bresenham

///scalling vector from the user and translate the triangle accordingly.///

///convert circle using Mid-point algorithm///


//cir3.c //midpoint circle

#include<stdio.h>#include<conio.h>#include<graphics.h>
void main(),,{,,int gdriver=DETECT;
int gmode,i,j,k,a[3][3],b[3][3],c[3][3],sx,sy;
initgraph(&gdriver,&gmode," "); printf("\nEnter the
scalling vector sx: "); scanf("%d",&sx); printf("\nEnter the
scalling vector sy: "); scanf("%d",&sy); for(i=0;i<=2;i++)
{for(j=0;j<=2;j++){if(i==j),,a[i][j]=1; else,,a[i][j]=0;
a[0][0]=sx; a[1][1]=sy; }},,printf("\nEnter the coordinates
points of triangle:\n"); for(i=0;i<=2;i++),,{,,
for(j=0;j<=2;j++),,{,, if(i==2),,b[i][j]=1; else,,
scanf("%d",&b[i][j]); }},,for(i=0;i<=2;i++),,{,,
for(j=0;j<=2;j++),,{,,c[i][j]=0; for(k=0;k<=2;k++),,{
c[i][j]+=(a[i][k]*b[k][j]); }}},,,printf("\nScalling
Coordinates are:\n"); for(i=0;i<=2;i++),,{,,
for(j=0;j<=2;j++),,{,, printf("%d",c[i][j]); printf("\t");}
printf("\n"); },,line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][2],c[1][2],c[0][0],c[1][0]);line(c[0][1],c[1][1],c[0][
2],c[1][2]);getch();closegraph();}

#include<stdio.h>#include<stdlib.h>#include<graphics.h>

void drawbreline(float,float,float,float); main()


{,,,int gdriver=DETECT,gmode; float
x1,y1,x2,y2,x3,y3,x4,y4; printf("\n Enter starting point
x1,y1 of the line:"); scanf("%f%f",&x1,&y1);
printf("\n Enter end point x2,y2 of the line:");
scanf("%f%f",&x2,&y2); clrscr();
initgraph(&gdriver,&gmode,"c:\tc\bin");
drawbreline(x1,y1,x2,y2); getch(); closegraph();
return;}
void drawbreline(float x1, float y1,float x2,float y2)
{float m,dx,dy,p,x,y,xe,ye; m=((y2-y1)/(x2-x1));
//slope less than 1,,if(m<=1),,{,,dx=abs(x1-x2);
dy=abs(y1-y2);p=(2*dy)-dx; if(x1>x2),,{,
x=x2,y=y2,xe=x1; },,else ,,{,,x=x1,y=y1,xe=x2;}
putpixel(x,y,15); while(x<xe),,{,, x=x+1; if(p<0)
,,,p=p+(2*dy); else,,{,,y=y+1; p=p+(2*(dy-dx));
},,putpixel(x,y,15);,,}},,//slope greater than 1
if(m>1),,,{,,,dx=abs(x1-x2); dy=abs(y1-y2); p=(2*dx)dy; if(y1>y2),, {,,x=x2,y=y2,ye=y1; }
,,else,,{x=x1,y=y1,ye=y2; },, putpixel(x,y,15);
while(y<ye),,,{,,y=y+1; if(p<0),,, p=p+(2*dx);
else,,{,x=x+1; p=p+(2*(dx-dy)); },,putpixel(x,y,15);
}}}

#include<stdio.h>#include<conio.h>#include<graphics.h>

void plotcircle(int xc,int yc,int x,int y,int r){


putpixel(xc+x,yc+y,r);putpixel(xc-x,yc+y,r);
putpixel(xc+x,yc-y,r); putpixel(xc-x,yc-y,r);
putpixel(xc+y,yc+x,r);putpixel(xc-y,yc+x,r);
putpixel(xc+y,yc-x,r); putpixel(xc-y,yc-x,r);
}void mid_circle(int xc,int yc,int r){int x,y,p;
x=0; y=r; p=1.25-r; while(x<y) {
plotcircle(xc,yc,x,y,r); if(p<=0) { x=x+1; y=y;
p=p+(2*x)+1; }else{ x=x+1; y=y-1; p=p+(2*x)(2*y)+1;} if(x==y) ,,plotcircle(xc,yc,x,y,r); }}
void main(),,,{,,int rad,x,y; int gdriver=DETECT;
int gmode; //clrscr(); initgraph(&gdriver,&gmode," ");
printf("\n\t Enter the radius : "); scanf("%d",&rad);
printf("\n\t Enter the centre(x,y) : "); scanf("%d
%d",&x,&y); mid_circle(x,y,rad); getch();closegraph();}

You might also like