Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #pragma once
  2. class Rectangle
  3. {
  4. int _x1;
  5. int _x2;
  6. int _y1;
  7. int _y2;
  8. public:
  9. Rectangle();
  10. Rectangle(int, int, int, int);
  11. void SetRectangle(); // ввод числа
  12. void ShowRectangle(); //вывод на консоль
  13. int getLong(); //вычисление длины
  14. int getShort(); //вычисление ширины
  15. void moveRectangle(); // перемещение
  16. void sizeRectangle(); // изменение размеров
  17. ~Rectangle();
  18. Rectangle Intersection( Rectangle&);
  19. };
  20.  
  21. =============================================
  22. #include "Rectangle.h"
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. Rectangle::Rectangle() { }
  27.  
  28. Rectangle::Rectangle(int x1, int x2, int y1, int y2)
  29. {
  30. this->_x1 = x1; this->_x2 = x2; this->_y1 = y1; this->_y2 = y2;
  31. }
  32. Rectangle::~Rectangle() { }
  33.  
  34. void Rectangle::SetRectangle()
  35. {
  36. cout << "Введите координаты x1 и y1 для первой точки: "; cin >> _x1 >> _y1;
  37. cout << "Введите координаты x2 и y2 для второй точки: "; cin >> _x2 >> _y2;
  38. if (_x1 == _x2 || _y1 == _y2)
  39. {
  40. throw exception("Ошибка! Не удалось создать прямоугольник(Прямая линия)");
  41. }
  42. }
  43. void Rectangle::ShowRectangle()
  44. {
  45. cout << "{" <<this->_x1 << ";" << this->_y1 << "}" << " " << "{" << this->_x2 << ";" << this->_y2 << "}" << endl;
  46. }
  47. int Rectangle::getLong()
  48. {
  49. double longer;
  50. longer = sqrt(pow(this->_x2 - this->_x1, 2));
  51. return longer;
  52. }
  53. int Rectangle::getShort()
  54. {
  55. double shorter;
  56. shorter = sqrt(pow(this->_y2 - this->_y1, 2));
  57. return shorter;
  58. }
  59. void Rectangle::moveRectangle()
  60. {
  61. int deltax, deltay;
  62. cout << "Введите на какое расстояние по x передвинуть прямоугольник: "; cin >> deltax;
  63. cout << "Введите на какое расстояние по y передвинуть прямоугольник: "; cin >> deltay;
  64. this->_x1 += deltax; this->_x2 += deltax; this->_y1 += deltay; this->_y2 += deltay;
  65. }
  66. void Rectangle::sizeRectangle()
  67. {
  68. double deltax, deltay;
  69. cout << "Изначальная длина: " << getLong() << endl;
  70. cout << "Изначальная ширина: " << getShort() << endl;
  71. cout << "Во сколько раз увеличить длину: "; cin >> deltax;
  72. cout << "Во сколько раз увеличить ширину: "; cin >> deltay;
  73. cout << "Измененная длина: " << getLong() * deltax <<endl;
  74. cout << "Измененная ширина:" << getShort() * deltay << endl;
  75. }
  76. Rectangle Rectangle::Intersection(Rectangle& R2)
  77. {
  78. Rectangle R3;
  79. int temp;
  80. if (this->_x2 < this->_x1)
  81. {
  82. temp = this->_x2;
  83. this->_x2 = this->_x1;
  84. this->_x1 = temp;
  85. }
  86. if (R2._x2 < R2._x1)
  87. {
  88. temp = R2._x2;
  89. R2._x2 = R2._x1;
  90. R2._x1 = temp;
  91. }
  92. if (this->_y2 < this->_y1)
  93. {
  94. temp = this->_y2;
  95. this->_y2 = this->_y1;
  96. this->_y1 = temp;
  97. }
  98. if (R2._y2 < R2._y1)
  99. {
  100. temp = R2._y2;
  101. R2._y2 = R2._y1;
  102. R2._y1 = temp;
  103. }
  104. R3._x1 = (this->_x1 > R2._x2) ? this->_x1 : R2._x1;
  105. R3._y1 = (this->_y1 > R2._y2) ? this->_y1 : R2._y1;
  106. R3._x2 = (this->_x2 < R2._x2) ? this->_x2 : R2._x2;
  107. R3._y2 = (this->_y2 < R2._y2) ? this->_y2 : R2._y2;
  108. //return ((this->_x2) > (this->_x1) && (this->_y2) > (this->_y1));
  109. if ((R3._x2) > (R3._x1) && (R3._y2) > (R3._y1))
  110. return R3;
  111. else
  112. throw exception("Ошибка!");
  113. }
  114. ===============================================
  115. #include "Rectangle.h"
  116. #include <iostream>
  117. using namespace std;
  118.  
  119. int main()
  120. {
  121. setlocale(0, "Rus");
  122. Rectangle R1, R2, R3;
  123. try
  124. {
  125. R1.SetRectangle();
  126. R1.ShowRectangle();
  127. R2.SetRectangle();
  128. R2.ShowRectangle();
  129. //R1.moveRectangle();
  130. //R1.ShowRectangle();
  131. //R1.sizeRectangle();
  132. R3 = R1.Intersection(R2);
  133. R3.ShowRectangle();
  134. }
  135. catch (const exception& ex)
  136. {
  137. cout << ex.what() << endl;
  138. }
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement