Vous êtes sur la page 1sur 10

cc 

 c   



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void dda(float x1,float y1,float x2,float y2)
{
float dx,dy,x=x1,y=y1,m;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
m=abs(dx);
else m=abs(dy);
putpixel((int)x,(int)y,15);
for(i=1;i<=m;i++)
{
x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
}
void bress(float x1,float y1,float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0;
if(dx>dy)
{
p=2*dy-dx;
if(x1<x2)
{

x=x1;y=y1;end=x2;
if(y1<y2)inc=1;
if(y1>y2)inc=-1;
}
else
{
x=x2;y=y2;end=x1;
if(y2<y1)inc=1;
if(y2>y1)inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);

if(p<0) p=p+2*dy;
else
{
y=y+inc;p=p+2*(dy-dx);
}
x++;
if(current==0&&c==10)
{
current=1;c=-1;
}
if(current==1&&c==6)
{
current=0;c=-1;
}
c++;
}

}
else
{
p=2*dx-dy;
if(y1<y2)
{
y=y1;x=x1;end=y2;
if(x1<x2)inc=1;
if(x1>x2)inc=-1;
}
else
{
y=y2;x=x2;end=y1;
if(x2<x1)inc=1;
if(x2>x1)inc=-1;
}
while(y<=end)
{
putpixel(x,y,15);

if(p<0)p=p+2*dx;
else
{
x=x+inc;p=p+2*(dx-dy);
}
y++;
if(current==0&&c==10)
{
current=1;c=-1;
}

if(current==1&&c==6)
{
current=0;c=-1;
}
c++;
}
}
}

void main()
{
float x1,x2,y1,y2;
int ch;
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"");
printf("Enter end points of line(x1,y1,x2,y2)");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Choose Algorithm(1-DDA 2-BRESENHAM)");
scanf("%d",&ch);
if(ch==1)
dda(x1,y1,x2,y2);
if(ch==2)
bress(x1,y1,x2,y2);
getch();
closegraph();
}

cc  
`  

` 


`   
`   
`  
`    

        

 

 
   !"#"$#


  % %
&'(( (( '&
  )*("  )
  
 '(&
)*++&%%
  )*("  )
 
 '(&
)*++&%%
  )*(# ,  

'(&

  
-

        

)
   
 .
  
 /
 /
) 
  

  
  0 
  0 
 
 
  
)
. .  .11

 1 
 1 
  
-
-

Arogram to implement DDA Line Drawing Algorithm

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class lines
{
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines();

//Constructor

void showline();
int sign(int);
};

int lines::sign(int xx)


{
if(xx<0)
return -1;
if(xx==0)
return 0;
if(xx>0)
return 1;

return 0;
}
lines::lines()
{
x=0;y=0;
cout<<"
"Enter The Co-Ordinates (x1,y1) ":=";
cin>>x1>>y1;
cout<<"
"Enter The Co-Ordinates (x2,y2) ":=";
cin>>x2>>y2;
cout<<"
"Enter The Width Of The Line ":=";
cin>>width;
}

void lines::showline()
{
char *s,*s1;
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
w=width;
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=(x2-x1)/length;

dy=(y2-y1)/length;
if(dy>dx)
wy=wx;
x=x1+0.5*sign(dx);
y=y1+0.5*sign(dy);
int i=1;
setcolor(0);
while(i<=length)
{
for(int j=0;j<wy;j++)
putpixel((x+320),480-(y+240+j),6);
for(j=0;j<wy;j++)
putpixel((x+320),480-(y+240-j),6);
putpixel((x+320),480-(y+240),6);
x+=dx;
y+=dy;
i++;
}
setcolor(15);
outtextxy(40,10,"The Aoints Are:=");
sprintf(s,"A(%d,%d)",x1,y1);
outtextxy(40,20,s);
sprintf(s,"B(%d,%d)",x2,y2);
outtextxy(40,30,s);
getch();
}
void main()

{
int gd=DETECT,gm,i,j,xx=240,xxx=380;
clrscr();
lines a;
char *mess[]={"D","D","A"," ","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=12;i<8,j>=6;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");

}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
}

Vous aimerez peut-être aussi