Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct Point
  5. {
  6. double x, y;
  7.  
  8. Point(){x=y=0;}
  9. Point(double x, double y)
  10. {
  11. this->x=x;
  12. this->y=y;
  13. }
  14. };
  15.  
  16. struct Rectangle
  17. {
  18. Point topLeft;
  19. double width, height;
  20.  
  21. Rectangle(Point topLeft, double width, double height)
  22. {
  23. this->topLeft = topLeft;
  24. this->width = width;
  25. this->height = height;
  26. }
  27. };
  28.  
  29. //Връща дължината на общата част на отсечките (a1, b1) и (a2, b2)
  30. double segmentIntersectionLength(double a1, double b1, double a2, double b2)
  31. {
  32. return max(min(b1, b2) - max(a1, a2),0.0);
  33. }
  34.  
  35. double getIntersectionArea(const Rectangle &r1, const Rectangle &r2)
  36. {
  37. double horizontalIntersection =
  38. segmentIntersectionLength(r1.topLeft.x, r1.topLeft.x + r1.width, r2.topLeft.x, r2.topLeft.x + r2.width);
  39.  
  40. double verticalIntersection =
  41. segmentIntersectionLength(r1.topLeft.y-r1.height, r1.topLeft.y, r2.topLeft.y-r2.height, r2.topLeft.y);
  42.  
  43. double rectangleIntersectionArea = horizontalIntersection * verticalIntersection;
  44.  
  45. return rectangleIntersectionArea;
  46. }
  47.  
  48. int main()
  49. {
  50. Point a(1,7);
  51. Point b(3,5);
  52. Rectangle r1(a,5,7);
  53. Rectangle r2(b,4,3);
  54. cout<<getIntersectionArea(r1,r2);
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement