'draw a rectangle using points using mid points of opposite sides [closed]
It's quite common to draw a rectangle using the upper_left and the bottom_right but I only have the midpoints of the width side.

This looks doable since it's perfectly aligned to the x axis but I'm not sure how to achieve when these two coordinate are at an angle.
I tried to get the upper_left and bottom_right coordinates but calculating them at an angle is posing to be a challenge

Solution 1:[1]
heres my approach on getting the four corners:
def get_corners(point1,point2,width):
# width /= 2
m1 = (point1[1]-point2[1])/(point1[0]-point2[0])
m2 = -1/m1
cor_x = math.sqrt((width/2)**2 / (m2**2 + 1))
cor_y = math.sqrt((width/2)**2 / (m2**-2 + 1))
if m2 >= 0:
corner1 = (point1[0] + cor_x, point1[1] + cor_y)
corner2 = (point1[0] - cor_x, point1[1] - cor_y)
corner3 = (point2[0] + cor_x, point2[1] + cor_y)
corner4 = (point2[0] - cor_x, point2[1] - cor_y)
else:
corner1 = (point1[0] - cor_x, point1[1] + cor_y)
corner2 = (point1[0] + cor_x, point1[1] - cor_y)
corner3 = (point2[0] - cor_x, point2[1] + cor_y)
corner4 = (point2[0] + cor_x, point2[1] - cor_y)
return corner1, corner2, corner3, corner4
print(get_corners((4,8),(-4,-8),math.sqrt(80)))
results:
((0.0, 10.0), (8.0, 6.0), (-8.0, -6.0), (0.0, -10.0))
which corresponds to:
rectangle used
After that just plot all the lines as usual based on your upper_left/ bottom_right method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
