From f3b3b54f640cc91c5fe19b5e12d2e0f2196ff7a3 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Fri, 29 Sep 2023 23:42:20 +0530 Subject: [PATCH] Added DDA for circle, line and triangle --- DDA-circle.cpp | 50 +++++++++++++++++++++++++++++++++++ DDA-line.cpp | 47 +++++++++++++++++++++++++++++++++ DDA-triangle.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 DDA-circle.cpp create mode 100644 DDA-line.cpp create mode 100644 DDA-triangle.cpp diff --git a/DDA-circle.cpp b/DDA-circle.cpp new file mode 100644 index 0000000..9016521 --- /dev/null +++ b/DDA-circle.cpp @@ -0,0 +1,50 @@ +#include +#include +using namespace std; + +int main() { + + // Taking input for X, Y coordinate and radius of the circle + int cordx, cordy, rad; + cout<<"Enter X coordinate for the circle:\t"; + cin>>cordx; + cout<<"Enter Y coordinate for the circle:\t"; + cin>>cordy; + cout<<"Enter radius for the circle:\t"; + cin>>rad; + + // Logic for octent + int gd=DETECT,gm; + initgraph(&gd, &gm, NULL); + + int x,y,p; + x=0; + y=rad; + + p=3-(2*rad); + + do { + putpixel(cordx+x, cordy-y, WHITE); + putpixel(cordx-x, cordy-y, GREEN); + putpixel(cordx+x, cordy+y, YELLOW); + putpixel(cordx-x, cordy+y, RED); + putpixel(cordx+y, cordy+x, WHITE); + putpixel(cordx+y, cordy-x, GREEN); + putpixel(cordx-y, cordy+x, YELLOW); + putpixel(cordx-y, cordy-x, RED); + if (p<0) { + p=p+(4*x)+6; + } + else { + p=p+(4*(x-y))+10; + y=y-1; + } + x=x+1; + } while(x +#include +using namespace std; + +int main() +{ +float x1,x2,y1,y2,dx,dy,xin,yin,x,y,len; + cout << "Enter the value of x1:\t"; + cin >> x1; + + cout << "Enter the value of x2:\t"; + cin >> x2; + + cout <<"Enter the value of y1:\t"; + cin >> y1; + + cout << "Enter the value of y2:\t"; + cin >> y2; + + int gd=DETECT,gm; + initgraph(&gd,&gm,NULL); + + dx=x2-x1; + dy=y2-y1; + if (abs(dx)>=abs(dy)) + len=abs(dx); + else + len=abs(dy); + + xin=dx/len; + yin=dy/len; + + x = x1+0.5; + y = y1+0.5; + int i = 0; + while(i<=len) + { + if(i%2==0) + putpixel(x,y,GREEN); + x = x+xin; + y = y+yin; + i++; + } +delay(50000); +closegraph(); +return (0); +} diff --git a/DDA-triangle.cpp b/DDA-triangle.cpp new file mode 100644 index 0000000..e0a86fa --- /dev/null +++ b/DDA-triangle.cpp @@ -0,0 +1,68 @@ +#include +#include +using namespace std; + +class triangle_maker { + + public: + + int gd=DETECT, gm; + float x1, y1, x2, y2, x, y; + float len, dx, dy, xin, yin; + + // Function to make edges of triangle + void edge(float x1, float y1, float x2, float y2){ + + initgraph(&gd, &gm, NULL); + + dx=x2-x1; + dy=y2-y1; + + if (abs(dx)>=abs(dy)) { + len=abs(dx); + } + else { + len=abs(dy); + } + + xin=dx/len; + yin=dy/len; + + x=x1; + y=y1; + + int i=0; + while (i<=len) { + putpixel(round(x),round(y),WHITE); + x=x+xin; + y=y+yin; + i++; + } + } +}; + +int main(){ + + float X1,X2,X3,Y1,Y2,Y3; + triangle_maker obj_triangle; + + cout<<"Enter X coordinate for first edge:\t"; + cin>>X1; + cout<<"Enter Y coordinate for first edge:\t"; + cin>>Y1; + cout<<"Enter X coordinate for second edge:\t"; + cin>>X2; + cout<<"Enter Y coordinate for second edge:\t"; + cin>>Y2; + cout<<"Enter X coordinate for third edge:\t"; + cin>>X3; + cout<<"Enter Y coordinate for third edge:\t"; + cin>>Y3; + + obj_triangle.edge(X1,Y1,X2,Y2); + obj_triangle.edge(X2,Y2,X3,Y3); + obj_triangle.edge(X3,Y3,X1,Y1); + delay(50000); + closegraph(); + return 0; +}