scriptdiddy
scriptdiddy

Reputation: 1265

convert four x,y coordinates into centroid, width height and orientation (py)

If i have a list of coordinates coords = [(7, 354), (307, 339), (304, 296), (4, 311)] for an angled rectangle. I would like to be able to convert these four points into x,y,w,h,o format.

How can I convert these for x,y pairs into a centroid, width, height and orientation? The centroid is easy to calculate as is the width and height. How about the orientation?

I'm ideally looking for an easy way to switch between the two. ex. convert_to_xywho() and convert_to_xy_list()

p1, p2 = end[:2]
p3, p4 = end[2:]
w = math.hypot(p2[0] - p1[0], p2[1] - p1[1])
h = math.hypot(p3[0] - p2[0], p3[1] - p2[1])
c = [[p1[0], p2[0], p3[0], p4[0]], [p1[1], p2[1], p3[1], p4[1]]]
centroide = (sum(c[0])/len(c[0]),sum(c[1])/len(c[1]))

Any suggestions on how to find the orientation. And apply such orientation to x,y,w,h,o to get a list of x,y pairs

Note: i'm using the PyGame coordinate system where the origin is in the top left.

Upvotes: 0

Views: 1210

Answers (1)

Prune
Prune

Reputation: 77857

The orientation comes from the tilt of one line segment.

tan(theta) = (y2-y1) / (x2-x1)

I suspect you can finish from there.

Upvotes: 1

Related Questions