Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find the intersection rectangle of two given rectangles.
- # Mike Kerry - April 2021 - [email protected]
- def FindIntersection(recta, rectb):
- # Compute bottom left point of intersection rectangle
- cx1 = max(recta[0][0], rectb[0][0])
- cy1 = max(recta[0][1], rectb[0][1])
- # Compute top right point of intersection rectangle
- cx2 = min(recta[1][0], rectb[1][0])
- cy2 = min(recta[1][1], rectb[1][1])
- # Is there any intersection?
- if cx1 > cx2 or cy1 > cy2:
- print("No intersection")
- return None
- return [(cx1, cy1), (cx2, cy2)] # Bottom Left and Top Right coords of intersection
- # Mainline code
- r1 = [(0, 0), (5, 5)] # coordinates of bottom left and top right of 1st rectangle
- r2 = [(3, 3), (7, 7)] # coordinates of bottom left and top right of 2nd rectangle
- intersect = FindIntersection(r1, r2)
- if intersect:
- print("Intersection Rectangle is:", intersect)
- lenx = abs(intersect[0][0] - intersect[1][0])
- leny = abs(intersect[0][1] - intersect[1][1])
- perim = 2 * (lenx + leny)
- print("Perimeter of Intersection Rectangle is:", perim, "units")
- area = lenx * leny
- print("Area of Intersection Rectangle is:", area, "square units")
- else:
- print("There is no intersection")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement