Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Point
- {
- double x, y;
- Point(){x=y=0;}
- Point(double x, double y)
- {
- this->x=x;
- this->y=y;
- }
- };
- struct Rectangle
- {
- Point topLeft;
- double width, height;
- Rectangle(Point topLeft, double width, double height)
- {
- this->topLeft = topLeft;
- this->width = width;
- this->height = height;
- }
- };
- //Връща дължината на общата част на отсечките (a1, b1) и (a2, b2)
- double segmentIntersectionLength(double a1, double b1, double a2, double b2)
- {
- return max(min(b1, b2) - max(a1, a2),0.0);
- }
- double getIntersectionArea(const Rectangle &r1, const Rectangle &r2)
- {
- double horizontalIntersection =
- segmentIntersectionLength(r1.topLeft.x, r1.topLeft.x + r1.width, r2.topLeft.x, r2.topLeft.x + r2.width);
- double verticalIntersection =
- segmentIntersectionLength(r1.topLeft.y-r1.height, r1.topLeft.y, r2.topLeft.y-r2.height, r2.topLeft.y);
- double rectangleIntersectionArea = horizontalIntersection * verticalIntersection;
- return rectangleIntersectionArea;
- }
- int main()
- {
- Point a(1,7);
- Point b(3,5);
- Rectangle r1(a,5,7);
- Rectangle r2(b,4,3);
- cout<<getIntersectionArea(r1,r2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement