C/C++ program to draw aline in computer graphics
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"");
cleardevice();
gotoxy(1,2);
printf("\n\t\tDIGITAL DIFFERENTIAL ALGORITHM FOR LINE DRAWING");
gotoxy(5,5);
printf("enter co-rodinates of the x1,y1:");
scanf("%d%d",&x1,&y1);
gotoxy(5,7);
printf("enter co-ordinates of x2,y2:");
scanf("%d%d",&x2,&y2);
cleardevice();
DDA(x1,y1,x2,y2);
getch();
closegraph();
}
DDA(int X1,int Y1,int X2,int Y2)
{
int len,i;
float xin,yin,x,y;
x=X1+0.8 ;
y=Y1+0.8;
len=abs(Y2-Y1);
if(len<abs(X2-X1))
len=abs(X2-X1);
xin=(float)(X2-X1)/len;
yin=(float)(Y2-Y1)/len;
for(i=1;i<=len;i++)
{
putpixel(abs(x),abs(y),987);
x+=xin;
y+=yin;
}
}
#include<graphics.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"");
cleardevice();
gotoxy(1,2);
printf("\n\t\tDIGITAL DIFFERENTIAL ALGORITHM FOR LINE DRAWING");
gotoxy(5,5);
printf("enter co-rodinates of the x1,y1:");
scanf("%d%d",&x1,&y1);
gotoxy(5,7);
printf("enter co-ordinates of x2,y2:");
scanf("%d%d",&x2,&y2);
cleardevice();
DDA(x1,y1,x2,y2);
getch();
closegraph();
}
DDA(int X1,int Y1,int X2,int Y2)
{
int len,i;
float xin,yin,x,y;
x=X1+0.8 ;
y=Y1+0.8;
len=abs(Y2-Y1);
if(len<abs(X2-X1))
len=abs(X2-X1);
xin=(float)(X2-X1)/len;
yin=(float)(Y2-Y1)/len;
for(i=1;i<=len;i++)
{
putpixel(abs(x),abs(y),987);
x+=xin;
y+=yin;
}
}
0 comments:
Post a Comment