Assign 3
Assign 3
#include<graphics.h>
#include<math.h>
#define ROUND(a)(int(a+0.5))
using namespace std;
class pattern{
private:
int x1,y1,x2,y2,x3,y3;
public:
void read();
void draw_triangle();
void dda(int xa,int ya,int xb,int yb);
void circle(int x,int y,int rad);
void display(int x1,int x2,int xa,int xb);
void incircle();
void excircle();
void normal();
};
void pattern::read(){
cout<<"Enter the co-ordinates of the triangle : "<<endl;
cout<<"\nEnter X1 : ";
cin>>x1;
cout<<"\nEnter Y1 : ";
cin>>y1;
cout<<"\nEnter X2 : ";
cin>>x2;
cout<<"\nEnter Y2 : ";
cin>>y2;
cout<<"\nEnter X3 : ";
cin>>x3;
cout<<"\nEnter Y3 : ";
cin>>y3;
}
void pattern::draw_triangle(){
dda(x1,y1,x2,y2);
dda(x2,y2,x3,y3);
dda(x3,y3,x1,y1);
}
void pattern::incircle(){
float a,b,c,area,rad,p;
int xc,yc,mul;
a=sqrt(pow((y2-y1),2)+pow((x2-x1),2));
b=sqrt(pow((y3-y1),2)+pow((x3-x1),2));
c=sqrt(pow((y3-y2),2)+pow((x3-x2),2));
p=(a+b+c)/2;
mul=p*(p-a)*(p-b)*(p-c);
area=sqrt(mul);
rad=area/p;
xc=((c*x1)+(b*x2)+(a*x3))/(2*p);
yc=((c*y1)+(b*y2)+(a*y3))/(2*p);
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<"p="<<p<<endl;
cout<<"area="<<area<<endl;
cout<<"rad="<<rad<<endl;
cout<<"xc="<<xc<<endl;
cout<<"yc="<<yc<<endl;
circle(xc,yc,abs(rad));
}
void pattern::excircle(){
float a,b,c;
int xec,yec,exrad,A,B,C,D,E,F,G;
a=sqrt(pow((y2-y1),2)+pow((x2-x1),2));
b=sqrt(pow((y3-y1),2)+pow((x3-x1),2));
c=sqrt(pow((y3-y2),2)+pow((x3-x2),2));
exrad=(a*b*c)/(sqrt(a+b+c)*sqrt(a+b-c)*sqrt(a+c-b)*sqrt(b+c-a));
A=x2-x1;
B=y2-y1;
C=x3-x1;
D=y3-y1;
E=(A*(x1+x2))+(B*(y1+y2));
F=(C*(x3+x1))+(D*(y1+y3));
G=2*((A*D)-(B*C));
if(G==0)
return;
else
{
xec=(D*E-B*F)/G;
yec=(A*F-C*E)/G;
cout<<xec<<endl;
cout<<yec;
}
circle(xec,yec,exrad);
}
void pattern::normal(){
draw_triangle();
incircle();
excircle();
}
int main(){
pattern p;
p.read();
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
p.normal();
getch();
closegraph();
return 0;
}
/*
Test Case :
Enter X1 : 200
Enter Y1 : 200
Enter X2 : 100
Enter Y2 : 400
Enter X3 : 300
Enter Y3 : 400
*/