Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework task
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * Winter semester 2017/2018
  7. *
  8. * @author NATALIJA STOJANOVA
  9. * @idnumber 855301
  10. * @task 7
  11. * @compiler GCC
  12. *
  13. */
  14. #include<iostream>
  15. #include<cmath>
  16. using namespace std;
  17.  
  18. int orientation(int ax, int ay, int bx, int by, int cx, int cy)
  19. {
  20. int dx1=bx-ax;
  21. int dy1=by-ay;
  22. int dx2=cx-ax;
  23. int dy2=cy-ay;
  24. return (dx1*dy2-dy1*dx2);
  25. }
  26. // intersect function finds if the side of first and second triangle intersect
  27. bool intersect(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy)
  28. {
  29. return ((orientation(ax, ay, cx, cy, dx, dy)*orientation(bx, by, cx, cy, dx, dy)<0) && (orientation(cx, cy, ax, ay, bx, by)*orientation(dx, dy, ax, ay, bx, by)<0));
  30. }
  31.  
  32. int main ()
  33. {
  34. cout<<"THIS PROGRAM DEFINES THE KIND OF TRIANGLES AND MEASURE THE AREA OF CROSS SECTION OF THE TRIANGLES "<<endl;
  35. float a1x, a1y, b1x, b1y, c1x, c1y, a2x, a2y, b2x, b2y, c2x, c2y;
  36. //a1x and a1y defines the first vertex in first triangle
  37. //b1x and b1y defines the second vertex in first triangle
  38. //c1x and c1y defines the third vertex in first triangle
  39. //a2x and a2y defines the first vertex in second triangle
  40. //b2x and b2y defines the second vertex in second triangle
  41. //c2x and c2y defines the third vertex in second triangle
  42.  
  43. cout<<"Enter the vertex of first triangle. Vertex A1"<<endl;
  44. cin>>a1x>>a1y;
  45. cout<<"Enter the vertex of first triangle Vertex B1"<<endl;
  46. cin>>b1x>>b1y;
  47. cout<<"Enter the vertex of first triangle Vertex C1"<<endl;
  48. cin>>c1x>>c1y;
  49. cout<<"Enter the vertex of second triangle. Vertex A2"<<endl;
  50. cin>>a2x>>a2y;
  51. cout<<"Enter the vertex of second triangle. Vertex B2"<<endl;
  52. cin>>b2x>>b2y;
  53. cout<<"Enter the vertex of second triangle. Vertex C2"<<endl;
  54. cin>>c2x>>c2y;
  55. bool flag11=0, flag12=0, flag21=0, flag22=0;
  56. // calculates the size of sides (a,b and c) with formula for distance between two points (first triangle)
  57. float a=sqrt((b1x-c1x)*(b1x-c1x)+(b1y-c1y)*(b1y-c1y)), b=sqrt((a1x-c1x)*(a1x-c1x)+(a1y-c1y)*(a1y-c1y)), c=sqrt((b1x-a1x)*(b1x-a1x)+(b1y-a1y)*(b1y-a1y));
  58. if (a==b && b==c)flag11=1; //Checks whether the triangle is equilateral
  59. // calculates the size of sides (a,b and c) with formula for distance between two points (second triangle)
  60. a=sqrt((b2x-c2x)*(b2x-c2x)+(b2y-c2y)*(b2y-c2y)), b=sqrt((a2x-c2x)*(a2x-c2x)+(a2y-c2y)*(a2y-c2y)), c=sqrt((b2x-a2x)*(b2x-a2x)+(b2y-a2y)*(b2y-a2y));
  61. if (a==b && b==c)flag12=1; //Checks whether the triangle is equilateral
  62. if(a1y==b1y){
  63. if(c1y>a1y)flag21=1; //Checks whether the triangle is upright
  64. if(a1x>b1x)swap(a1x, b1x); //sorts the vertexes
  65. }
  66. else if(a1y==c1y){
  67. if(b1y>a1y)flag21=1; //Checks whether the triangle is upright
  68. if(a1x>c1x)swap(a1x, c1x); //sorts the vertexes
  69. }
  70. else if(c1y==b1y){
  71. if(a1y>b1y)flag21=1; //Checks whether the triangle is upright
  72. if(b1x>c1x)swap(b1x, c1x); //sorts the vertexes
  73. }
  74. if(a2y==b2y){
  75. if(c2y>a2y)flag22=1; //Checks whether the triangle is upright
  76. if(a2x>b2x)swap(a2x, b2x); //sorts the vertexes
  77. }
  78. else if(a2y==c2y){
  79. if(b2y>a2y)flag22=1; //Checks whether the triangle is upright
  80. if(a2x>c2x)swap(a2x, c2x); //sorts the vertexes
  81. }
  82. else if(c2y==b2y){
  83. if(a2y>b2y)flag22=1; //Checks whether the triangle is upright
  84. if(b2x>c2x)swap(b2x, c2x); //sorts the vertexes
  85. }
  86. if (flag11&&flag21)
  87. cout<<"First triangle is upright and equilateral"<<endl;
  88. else cout<<"First triangle is not upright or equilateral"<<endl;
  89. if (flag12 && flag22)
  90. cout<<"Second triangle is upright and equilateral"<<endl;
  91. else cout<<"Second triangle is not upright or equilateral"<<endl;
  92. float x1, y1, x2, y2; //x1 and y1 defines the first vertex of new triangle. x2 and y2 defines the second vertex of new triangle
  93. //The cross section of the triangles is also a equilateral triangle
  94. float a1, b1, c1;
  95. float a2, a3, b2, b3, c2, c3;
  96. if(flag11 && flag12 && flag21 && flag22)
  97. {
  98. float dx1, dx2, dy2;
  99. if(a1y==b1y)
  100. {
  101. if(a2y==b2y && intersect(a1x, a1y, c1x, c1y, a2x, a2y, b2x, b2y))
  102. {
  103. a1=a1y-c1y, b1=c1x-a1x, c1=a1x*c1y-c1x*a1y, a2=a2y-b2y, b2=b2x-a2x, c2=a2x*b2y-b2x*a2y;
  104. x1=(-c1*b2+c2*b1)/(a1*b2-a2*b1);
  105. y1=(-a1*c2+a2*c1)/(a1*b2-a2*b1);
  106. if(intersect(b1x, b1y, c1x, c1y, a2x, a2y, b2x, b2y))
  107. {
  108. a3=a2y-b2y, b3=b2x-a2x, c3=a2x*b2y-b2x*a2y;
  109. x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
  110. y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
  111. }
  112. else if(intersect(b1x, b1y, c1x, c1y, a2x, a2y, c2x, c2y))
  113. {
  114. a3=b2y-c2y, b3=c2x-b2x, c3=b2x*c2y-c2x*b2y;
  115. x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
  116. y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
  117. }
  118. else if(intersect(a1x, a1y, c1x, c1y, b2x, b2y, c2x, c2y))
  119. {
  120. a3=b2y-c2y, b3=c2x-b2x, c3=b2x*c2y-c2x*b2y;
  121. x2=(-c1*b3+c3*b1)/(a1*b3-a3*b1);
  122. y2=(-a1*c3+a3*c1)/(a1*b3-a3*b1);
  123. }
  124. }
  125. }
  126. float Area;
  127. float Side_newTriagle = sqrt((x2-x1)*(x2-x1)-(y2-y1)*(y2-y1));
  128. Area = (sqrt(3)*Side_newTriagle*Side_newTriagle)/4;
  129. cout<<"The area of cross section of the triangles is: "<<Area;
  130.  
  131. }
  132. else
  133. cout<<"The cross section of the triangles can not be measured because they are not uprighted and equilateral";
  134.  
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement