HOT C PROGRAMS BLOGSPOT

HOT C PROGRAMS BLOGSPOT

Click On Related Results For More Information

Thursday 11 October 2012

Graphics DAA Line Drawing C Program

/* .......Works on Win XP */

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

float abs(float);
float round(float);
int main() {
int gd=DETECT,gm,steps,k,dx,dy,x1,y1,x2,y2;
float xincr,yincr,x,y;
clrscr();
printf("Enter the co-ordinates of the starting point : ");
scanf("%d%d",&x1,&y1);
printf("Enter the co-ordinates of the end point : ");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy)) {
steps=abs(dx);
}
else {
steps=abs(dy);
}
xincr=(float)dx/steps;
yincr=(float)dy/steps;
 initgraph(&gd,&gm, "C:\\TC\\BGI");   /*Sometimes it may be "c:\\tc\\bgi" , It depends machine to mac..*/
putpixel(round(x),round(y),RED);
for(k=0;k<steps;k++) {
x=x+xincr;
y=y+yincr;
putpixel(round(x),round(y),RED);
}
getch();
closegraph();
return 0;
}

float abs(float x) {
if(x>0) {
return x;
}
else {
return (-1*x);
}
}

float round(float x) {
int y;
y=(int)x;
if(x<(y+0.5)) {
return y;
}
else {
return (y+1);
}
}

No comments:

Post a Comment