Added DDA for circle, line and triangle

This commit is contained in:
K 2023-09-29 23:42:20 +05:30
parent 56db44aa01
commit f3b3b54f64
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
3 changed files with 165 additions and 0 deletions

50
DDA-circle.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <graphics.h>
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<y);
outtext("Designed and Engineered by KSHITIJ");
delay(50000);
closegraph();
return 0;
}

47
DDA-line.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<graphics.h>
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);
}

68
DDA-triangle.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include <graphics.h>
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;
}