Advertisement
Alexander672

Class figure

Apr 17th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. const double pi = acos(-1);
  9.  
  10. class point
  11. {
  12. private:
  13. int x, y;
  14.  
  15. public:
  16. point() {}
  17. point(int x, int y)
  18. {
  19. this->x = x;
  20. this->y = y;
  21. }
  22. int get_x()
  23. {
  24. return x;
  25. }
  26. int get_y()
  27. {
  28. return y;
  29. }
  30. double get_dist_to_O()
  31. {
  32. return sqrt(x * x + y * y);
  33. }
  34. double get_dist_to_point(point A)
  35. {
  36. return sqrt((x - A.x) * (x - A.x) + (y - A.y) * (y - A.y));
  37. }
  38. int get_dist_to_point2(point A)
  39. {
  40. return (x - A.x) * (x - A.x) + (y - A.y) * (y - A.y);
  41. }
  42. void change_x(int a)
  43. {
  44. x += a;
  45. }
  46. void change_y(int b)
  47. {
  48. y += b;
  49. }
  50. void change_x_and_y(int a, int b)
  51. {
  52. x += a;
  53. y += b;
  54. }
  55. void multiply(int a)
  56. {
  57. x *= a;
  58. y *= a;
  59. }
  60. void operator+(int a)
  61. {
  62. x += a;
  63. y += a;
  64. }
  65. void operator*(int a)
  66. {
  67. x *= a;
  68. y *= a;
  69. }
  70. void print()
  71. {
  72. cout << x << " " << y << "\n";
  73. }
  74. };
  75.  
  76. class vectr
  77. {
  78. private:
  79. int x, y;
  80.  
  81. public:
  82. vectr() {}
  83. vectr(int x, int y)
  84. {
  85. this->x = x;
  86. this->y = y;
  87. }
  88. vectr(point A, point B)
  89. {
  90. this->x = B.get_x() - A.get_x();
  91. this->y = B.get_y() - A.get_y();
  92. }
  93. int get_x()
  94. {
  95. return x;
  96. }
  97. int get_y()
  98. {
  99. return y;
  100. }
  101. int operator*(vectr a)
  102. {
  103. return x * a.get_x() + y * a.get_y();
  104. }
  105. int operator%(vectr a)
  106. {
  107. return x * a.get_y() - y * a.get_x();
  108. }
  109. };
  110.  
  111. class figure
  112. {
  113. private:
  114. int num_vertexes;
  115. vector<point> vertex;
  116.  
  117. public:
  118. figure() {}
  119. figure(vector<point> vertex)
  120. {
  121. this->vertex = vertex;
  122. num_vertexes = vertex.size();
  123. }
  124. double get_perimetr()
  125. {
  126. double p = 0;
  127. for (int i = 0; i < num_vertexes; i++)
  128. p += vertex[i].get_dist_to_point(vertex[i + 1]);
  129. p += vertex[0].get_dist_to_point(vertex[num_vertexes - 1]);
  130. return p;
  131. }
  132. double get_area()
  133. {
  134. double s = 0;
  135. point O(0, 0);
  136. for (int i = 0; i < num_vertexes; i++)
  137. s += vectr(O, vertex[i]) % vectr(O, vertex[i + 1]);
  138. s += vectr(O, vertex[num_vertexes - 1]) % vectr(O, vertex[0]);
  139. s = fabs(s / 2.0);
  140. return s;
  141. }
  142. vector<point> get_vertex()
  143. {
  144. return vertex;
  145. }
  146. void get_information()
  147. {
  148. cout << "num_vertexes = " << num_vertexes << "\n"
  149. << "vertexes :"
  150. << "\n";
  151. for (point now : vertex)
  152. now.print();
  153. }
  154. double operator+(figure a)
  155. {
  156. return this->get_area() + a.get_area();
  157. }
  158. };
  159.  
  160. class triangle : figure
  161. {
  162. private:
  163. string type;
  164.  
  165. public:
  166. triangle(vector<point> vertex) : figure(vertex)
  167. {
  168. type = "not determined";
  169. }
  170. vector<point> roll = this->get_vertex();
  171. string get_type()
  172. {
  173. if (type != "not determined")
  174. return type;
  175. int temp1 = vectr(roll[0], roll[1]) * vectr(roll[0], roll[2]);
  176. int temp2 = vectr(roll[1], roll[2]) * vectr(roll[1], roll[0]);
  177. int temp3 = vectr(roll[2], roll[0]) * vectr(roll[2], roll[1]);
  178. if (temp1 == 0 || temp2 == 0 || temp3 == 0)
  179. type = "rectangular";
  180. else if ((temp1 > 0 && temp2 > 0 && temp3 > 0) || (temp1 < 0 && temp2 < 0 && temp3 < 0))
  181. type = "acute-angled";
  182. else
  183. type = "obtuse";
  184. return type;
  185. }
  186. bool check()
  187. {
  188. int a = roll[0].get_dist_to_point2(roll[1]);
  189. int b = roll[0].get_dist_to_point2(roll[2]);
  190. int c = roll[1].get_dist_to_point2(roll[2]);
  191. if (a == b && b == c)
  192. return true;
  193. return false;
  194. }
  195. void get_information()
  196. {
  197. cout << this->get_type() << " triangle\nvertexes :\n";
  198. for (point now : this->roll)
  199. now.print();
  200. }
  201. };
  202.  
  203. class quadrangle : figure
  204. {
  205. private:
  206. string type;
  207.  
  208. public:
  209. quadrangle(vector<point> vertex) : figure(vertex)
  210. {
  211. type = "not determined";
  212. }
  213. vector<point> roll = this->get_vertex();
  214. string get_type()
  215. {
  216. if (type != "not determined")
  217. return type;
  218. vectr AB(roll[0], roll[1]), BC(roll[1], roll[2]), DC(roll[3], roll[2]), AD(roll[0], roll[3]);
  219. int temp1 = AB % DC, temp2 = BC % AD;
  220. if (temp1 == 0 && temp2 == 0)
  221. {
  222. bool rectangle = false, rhombus = false;
  223. if (AB * BC == 0)
  224. rectangle = true;
  225. if (roll[0].get_dist_to_point2(roll[1]) == roll[1].get_dist_to_point2(roll[2]))
  226. rhombus = true;
  227. if (rectangle && rhombus)
  228. type = "square";
  229. else if (rectangle)
  230. type = "rectangle";
  231. else if (rhombus)
  232. type = "rhombus";
  233. else
  234. type = "parallelogram";
  235. }
  236. else if (temp1 == 0 || temp2 == 0)
  237. type = "trapeze";
  238. else
  239. type = "no-type quadrangle";
  240. return type;
  241. }
  242. void get_information()
  243. {
  244. cout << type << "\nvertexes :\n";
  245. for (point now : roll)
  246. now.print();
  247. }
  248. };
  249.  
  250. class right_polygon : figure
  251. {
  252. private:
  253. string type;
  254.  
  255. public:
  256. right_polygon(string type, vector<point> vertex) : figure(vertex)
  257. {
  258. this->type = type;
  259. }
  260. vector<point> roll = this->get_vertex();
  261. void get_information()
  262. {
  263. cout << type << "\nvertexes :\n";
  264. for (point now : roll)
  265. now.print();
  266. }
  267. };
  268.  
  269. class circle : figure
  270. {
  271. private:
  272. point centre;
  273. int radius;
  274. public:
  275. circle(point centre, int radius) : figure()
  276. {
  277. this->centre = centre;
  278. this->radius = radius;
  279. }
  280. double get_petimetr()
  281. {
  282. return 2.0 * pi * radius;
  283. }
  284. double get_area()
  285. {
  286. return pi * radius * radius;
  287. }
  288. void get_information()
  289. {
  290. cout << "centre = (" << centre.get_x() << " " << centre.get_y() << ")\n";
  291. cout << "radius = " << radius << "\n";
  292. }
  293. };
  294.  
  295. int main()
  296. {
  297. point A(0, 0), B(2, 0), C(0, 3);
  298. vector<point> roll;
  299. roll.push_back(A);
  300. roll.push_back(B);
  301. roll.push_back(C);
  302. triangle a(roll);
  303. a.get_information();
  304. return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement