CG/DDA-triangle.cpp

69 lines
1.1 KiB
C++

#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;
}