Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<graphics.h>
  3. #include<math.h>
  4. #define ROUND(a)(int(a+0.5))
  5. using namespace std;
  6. class pattern{
  7. private:
  8. int x1,y1,x2,y2,x3,y3;
  9. public:
  10. void read();
  11. void draw_triangle();
  12. void dda(int xa,int ya,int xb,int yb);
  13. void circle(int x,int y,int rad);
  14. void display(int x1,int x2,int xa,int xb);
  15. void incircle();
  16. void excircle();
  17. void normal();
  18. };
  19. void pattern::read(){
  20. cout<<"Enter the co-ordinates of the triangle"<<endl;
  21. cin>>x1;
  22. cin>>y1;
  23. cin>>x2;
  24. cin>>y2;
  25. cin>>x3;
  26. cin>>y3;
  27. }
  28.  
  29. void pattern::draw_triangle(){
  30. dda(x1,y1,x2,y2);
  31. dda(x2,y2,x3,y3);
  32. dda(x3,y3,x1,y1);
  33. }
  34.  
  35.  
  36. void pattern::dda(int xa,int ya,int xb,int yb){
  37. int dx,dy,steps;
  38. dx=xb-xa;
  39. dy=yb-ya;
  40. if(abs(dx)>=abs(dy))
  41. steps=abs(dx);
  42. else
  43. steps=abs(dy);
  44. float x=xa;
  45. float y=ya;
  46. float xinc,yinc;
  47. xinc=dx/float(steps);
  48. yinc=dy/float(steps);
  49. putpixel(ROUND(x),ROUND(y),3);
  50. for(int i=0;i<steps;i++){
  51. x=x+xinc;
  52. y=y+yinc;
  53. putpixel(ROUND(x),ROUND(y),3);
  54. }
  55. }
  56.  
  57. void pattern::circle(int x,int y,int rad){
  58. int x1=0;
  59. int y1=rad;
  60. putpixel(x1,y1,6);
  61. int S=3-2*rad;
  62. while(x1<=y1){
  63. if(S<=0){
  64. S=S+(4*x1)+6;
  65. }
  66. else{
  67. S=S+4*(x1-y1)+10;
  68. y1--;
  69. }
  70. x1++;
  71. putpixel(x+x1,y+y1,1);
  72. putpixel(x-x1,y+y1,2);
  73. putpixel(x+x1,y-y1,3);
  74. putpixel(x-x1,y-y1,4);
  75. putpixel(x+y1,y+x1,5);
  76. putpixel(x-y1,y+x1,6);
  77. putpixel(x+y1,y-x1,7);
  78. putpixel(x-y1,y-x1,8);
  79. }
  80. }
  81.  
  82. void pattern::incircle(){
  83. float a,b,c,area,rad,p;
  84. int xc,yc,mul;
  85. a=sqrt(pow((y2-y1),2)+pow((x2-x1),2));
  86. b=sqrt(pow((y3-y1),2)+pow((x3-x1),2));
  87. c=sqrt(pow((y3-y2),2)+pow((x3-x2),2));
  88. p=(a+b+c)/2;
  89. mul=p*(p-a)*(p-b)*(p-c);
  90. area=sqrt(mul);
  91. rad=area/p;
  92. xc=((c*x1)+(b*x2)+(a*x3))/(2*p);
  93. yc=((c*y1)+(b*y2)+(a*y3))/(2*p);
  94. cout<<"a="<<a<<endl;
  95. cout<<"b="<<b<<endl;
  96. cout<<"c="<<c<<endl;
  97. cout<<"p="<<p<<endl;
  98. cout<<"area="<<area<<endl;
  99. cout<<"rad="<<rad<<endl;
  100. cout<<"xc="<<xc<<endl;
  101. cout<<"yc="<<yc<<endl;
  102. circle(xc,yc,abs(rad));
  103. }
  104.  
  105. void pattern::excircle(){
  106. float a,b,c;
  107. int xec,yec,exrad,A,B,C,D,E,F,G;
  108. a=sqrt(pow((y2-y1),2)+pow((x2-x1),2));
  109. b=sqrt(pow((y3-y1),2)+pow((x3-x1),2));
  110. c=sqrt(pow((y3-y2),2)+pow((x3-x2),2));
  111. exrad=(a*b*c)/(sqrt(a+b+c)*sqrt(a+b-c)*sqrt(a+c-b)*sqrt(b+c-a));
  112. A=x2-x1;
  113. B=y2-y1;
  114. C=x3-x1;
  115. D=y3-y1;
  116. E=(A*(x1+x2))+(B*(y1+y2));
  117. F=(C*(x3+x1))+(D*(y1+y3));
  118. G=2*((A*D)-(B*C));
  119. if(G==0)
  120. return;
  121. else
  122. {
  123. xec=(D*E-B*F)/G;
  124. yec=(A*F-C*E)/G;
  125. cout<<xec<<endl;
  126. cout<<yec;
  127. }
  128. circle(xec,yec,exrad);
  129. }
  130.  
  131. void pattern::normal(){
  132. draw_triangle();
  133. incircle();
  134. excircle();
  135. }
  136.  
  137.  
  138. int main(){
  139. pattern p;
  140. p.read();
  141. int gd=DETECT,gm;
  142. initgraph(&gd,&gm,NULL);
  143. p.normal();
  144. getch();
  145. closegraph();
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement