-1
Chat with our AI personalities
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<ctype.h> #include<math.h> #include<stdlib.h> void draw(int x1,int y1,int x2,int y2); void main() { int x1,y1,x2,y2; int gdriver=DETECT,gmode,gerror; initgraph(&gdriver,&gmode,"c:\\tc\\bgi:"); printf("\n Enter the x and y value for starting point:\n"); scanf("%d%d",&x1,&y1); printf("\n Enter the x and y value for ending point:\n"); scanf("%d%d",&x2,&y2); printf("\n The Line is shown below: \n"); draw(x1,y1,x2,y2); getch(); } void draw(int x1,int y1,int x2,int y2) { float x,y,xinc,yinc,dx,dy; int k; int step; dx=x2-x1; dy=y2-y1; if(abs(dx)>abs(dy)) step=abs(dx); else step=abs(dy); xinc=dx/step; yinc=dy/step; x=x1; y=y1; putpixel(x,y,1); for(k=1;k<=step;k++) { x=x+xinc; y=y+yinc; putpixel(x,y,2); } }
# include <graphics.h> # include <math.h> # include <conio.h> # include <iostream.h> void DDALine(int x1,int y1,int x2,int y2,int iColor); void main() { int gDriver=DETECT,gMode; int x1,x2,y1,y2,iColor; initgraph(&gDriver,&gMode,"c:\\tc\\bgi"); cleardevice(); cout<<endl<<"Enter x1 : "; cin>>x1; cout<<"Enter y1 : "; cin>>y1; cout<<endl<<"Enter x2 : "; cin>>x2; cout<<"Enter y2 : "; cin>>y2; cout<<endl<<"Enter COLOR : "; cin>>iColor; cleardevice(); DDALine(320,1,320,480,12); DDALine(1,240,640,240,12); circle(320,240,2); DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16); getch(); } void DDALine(int x1,int y1,int x2,int y2,int iColor) { float dX,dY,iSteps; float xInc,yInc,iCount,x,y; dX = x1 - x2; dY = y1 - y2; if (fabs(dX) > fabs(dY)) { iSteps = fabs(dX); } else { iSteps = fabs(dY); } xInc = dX/iSteps; yInc = dY/iSteps; x = x1; y = y1; circle(x,y,1); for (iCount=1; iCount<=iSteps; iCount++) { putpixel(floor(x),floor(y),iColor); x -= xInc; y -= yInc; } circle(x,y,1); return; }
Here is the C code for DDA line drawing... #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd, gm; float x, y, dx, dy, len; int x1, y1, x2, y2, i; detectgraph(&gd, &gm); initgraph(&gd, &gm,""); printf("\n Enter the coordinates of line : "); scanf("%d %d %d %d", &x1, &y1, &x2, &y2); dx = abs(x2-x1); dy = abs(y2-y1); if (dx >= dy) len = dx; else len = dy; dx = (x2-x1)/len; dy = (y2-y1)/len; x = x1 + 0.5; y = y1 + 0.5; i = 1; while (i <= len) { putpixel(x, y, 6); x = x+dx; y = y+dy; i++; } getch(); } -Suraj.
Let (xc,yc) be the centre of the circle Let r be the radius of the circle Let d be 3-2*r (d for decision) Let x be 0 Let y be r Repeat while x is less than y: Increment x If d is less than zero Let d be d + ( 4 * x ) + 6 Else Decrement y Let d be d + (( x - y ) * 4 ) + 10 End if Plot 8 points: ( xc+x, yc+y ) ( xc-x, yc+y ) ( xc+x, yc-y ) ( xc-x, yc-y ) ( xc+y, yc+x ) ( xc-y, yc+x ) ( xc+y, yc-x ) ( xc-y, yc-x ) End repeat
Definition. Example: extern int x1; /* declaration */ int x2; /* definition */ int x3= 2; /* definition with initialization */