Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. class Point {
  7. private:
  8. int x1;
  9. int y1;
  10. public:
  11. Point() {
  12. x1 = 0;
  13. y1 = 0;
  14. }
  15. Point(int _x, int _y) {
  16. x1 = _x;
  17. y1 = _y;
  18. }
  19. int getX() {
  20. return x1;
  21. }
  22. int getY() {
  23. return y1;
  24. }
  25. void setX(int _x) {
  26. x1 = _x;
  27. }
  28. void setY(int _y) {
  29. y1 = _y;
  30. }
  31. };
  32.  
  33. class Vector {
  34. private:
  35. int x1;
  36. int y1;
  37. public:
  38. Vector(Point p1, Point p2) {
  39. x1 = p2.getX() - p1.getX();
  40. y1 = p2.getY() - p1.getY();
  41. }
  42. int getX() {
  43. return x1;
  44. }
  45. int getY() {
  46. return y1;
  47. }
  48. bool IsPerpend(Vector v1) {
  49. if (x1 * v1.getX() + y1 * v1.getY() == 0) {
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool operator==(Vector v1) {
  55. if (pow(this->x1, 2.0) + pow(this->y1, 2.0) == pow(v1.getX(), 2.0) + pow(v1.getY(), 2.0)) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. double lenght() {
  61. return sqrt(x1*x1 + y1 * y1);
  62. }
  63. };
  64.  
  65. class Shape {
  66. protected:
  67. Point p1;
  68. public:
  69. Shape() {
  70. p1 = Point(0, 0);
  71. }
  72. Shape(int x1, int y1) {
  73. p1 = Point(x1, y1);
  74. }
  75. virtual double Area() {
  76. return 0;
  77. }
  78. };
  79.  
  80. class Circle : public Shape {
  81. private:
  82. double r;
  83. public:
  84. Circle() {
  85. r = 1;
  86. }
  87. Circle(double _r) {
  88. r = _r;
  89. }
  90. Circle(int _x, int _y) {
  91. p1.setX(_x);
  92. p1.setY(_y);
  93. r = 1;
  94. }
  95. Circle(int _x, int _y, double _r) {
  96. p1.setX(_x);
  97. p1.setY(_y);
  98. r = _r;
  99. }
  100. double Area() override {
  101. return 3.14*r*r;
  102. }
  103. void Print() {
  104. cout << p1.getX() << " " << p1.getX() << " " << r << endl;
  105. }
  106. };
  107.  
  108. class Rectangle : public Shape {
  109. protected:
  110. Point p2;
  111. Point p3;
  112. Point p4;
  113. public:
  114. Rectangle() {
  115. p2 = Point(0, 1);
  116. p3 = Point(1, 1);
  117. p4 = Point(1, 0);
  118. }
  119. Rectangle(Point _p2, Point _p3, Point _p4) {
  120. p2 = _p2;
  121. p3 = _p3;
  122. p4 = _p4;
  123. Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
  124. if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
  125.  
  126. }
  127. else {
  128. cout << "Error rectangle isn't rectangular" << endl;
  129. abort();
  130. }
  131. }
  132. Rectangle(Point _p1, Point _p2, Point _p3, Point _p4) {
  133. p1 = _p1;
  134. p2 = _p2;
  135. p3 = _p3;
  136. p4 = _p4;
  137. Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
  138. if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
  139.  
  140. }
  141. else {
  142. cout << "Error rectangle isn't rectangular" << endl;
  143. abort();
  144. }
  145. }
  146. double Area() override {
  147. Vector v1(p1, p2), v2(p2, p3);
  148. return v1.lenght() * v2.lenght();
  149. }
  150. void Print() {
  151. cout << p1.getX() << " " << p1.getY() << endl;
  152. cout << p2.getX() << " " << p2.getY() << endl;
  153. cout << p3.getX() << " " << p3.getY() << endl;
  154. cout << p4.getX() << " " << p4.getY() << endl << endl;
  155. }
  156. };
  157.  
  158. class Square : public Rectangle {
  159. public:
  160. Square() {
  161.  
  162. }
  163. Square(Point _p2, Point _p3, Point _p4) {
  164. p2 = _p2;
  165. p3 = _p3;
  166. p4 = _p4;
  167. Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
  168. if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
  169.  
  170. }
  171. else {
  172. cout << "Error square isn't square" << endl;
  173. abort();
  174. }
  175. if (v1 == v2 && v2 == v3 && v3 == v4) {
  176.  
  177. }
  178. else {
  179. cout << "The sides of the square are not equal" << endl;
  180. abort();
  181. }
  182. }
  183. Square(Point _p1, Point _p2, Point _p3, Point _p4) {
  184. p1 = _p1;
  185. p2 = _p2;
  186. p3 = _p3;
  187. p4 = _p4;
  188. Vector v1(p1, p2), v2(p2, p3), v3(p3, p4), v4(p4, p1);
  189. if (v1.IsPerpend(v2) && v2.IsPerpend(v3) && v3.IsPerpend(v4) && v4.IsPerpend(v1)) {
  190.  
  191. }
  192. else {
  193. cout << "Error square isn't square" << endl;
  194. abort();
  195. }
  196. }
  197. double Area() override {
  198. Vector v1(p1, p2);
  199. return v1.lenght() * v1.lenght();
  200. }
  201. void Print() {
  202. cout << p1.getX() << " " << p1.getY() << endl;
  203. cout << p2.getX() << " " << p2.getY() << endl;
  204. cout << p3.getX() << " " << p3.getY() << endl;
  205. cout << p4.getX() << " " << p4.getY() << endl << endl;
  206. }
  207. };
  208.  
  209. int main() {
  210. Circle c1;
  211. c1.Print();
  212. Circle c2(2);
  213. c2.Print();
  214. Circle c3(0, 0);
  215. c3.Print();
  216. Circle c4(0, 0, 5);
  217. c4.Print();
  218. cout << c4.Area() << endl;
  219. Rectangle r1;
  220. r1.Print();
  221. Rectangle r2(Point(1, 0), Point(1, 2), Point(0, 2));
  222. r2.Print();
  223. Rectangle r3(Point(0, 0), Point(1, 0), Point(1, 2), Point(0, 2));
  224. r3.Print();
  225. cout << r3.Area() << endl;
  226. Square s1;
  227. s1.Print();
  228. Square s2(Point(2, 0), Point(2, 2), Point(0, 2));
  229. s2.Print();
  230. Square s3(Point(0, 0), Point(2, 0), Point(2, 2), Point(0, 2));
  231. s3.Print();
  232. cout << s3.Area() << endl;
  233. system("pause");
  234. return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement