Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // implement DDA algorithm
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<graphics.h>
- int sign(float x)
- {
- int signa;
- if(x>=0.0)
- signa=1;
- else
- signa=-1;
- return signa;
- }
- void main()
- {
- //step 1
- int gd=DETECT,gm;
- float x1,y1,x2,y2,dx,dy,length;
- int i=1;
- float dx1,dy1,x,y;
- int sign(float x);
- initgraph(&gd,&gm,"c:/tc1/tc/tc/bgi");
- printf("enter point 1 \n");
- scanf("%f",&x1);
- scanf("%f",&y1);
- printf("coordinates are %f %f \n",x1,y1);
- printf("enter point 2 \n");
- scanf("%f",&x2);
- scanf("%f",&y2);
- printf("coordinates are %f %f \n",x2,y2);
- //step 2
- if(x2>x1)
- dx=x2-x1;
- else
- dx=x1-x2;
- if(y2>y1)
- dy=y2-y1;
- else
- dy=y1-y2;
- //step3
- if(dx>=dy)
- length=dx;
- else
- length=dy;
- //step 4
- dx1=(x2-x1)/length;
- dy1=(y2-y1)/length;
- //step5
- x=x1+0.5*sign(dx1);
- y=y1+0.5*sign(dy1);
- //step 6
- while(i<=length)
- {
- putpixel((int)x,(int)y,RED);
- x=x+dx1;
- y=y+dy1;
- i=i+1;
- }
- getche();
- closegraph();
- clrscr();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement