Calculate width and height from 4 points of a polygon

I have four points that form a rectangle, and I allow the user to move any point and rotate the rectangle by an angle (which rotates each point around a center point). It remains in the almost perfect shape of the rectangle (as far as the precision of PointF allows). Here is an example of my "rectangle", taken from four points:

enter image description here

However, I need to be able to get the width and height between the points. This is easy when the rectangle does not rotate, but after rotating it, my math returns the width and height indicated by the red outline:

enter image description here

Assuming I know the order of the points (e.g. clockwise, e.g. top to bottom), how do I get the width and height of the rectangle that they represent?

+4
source share
3 answers

If by "width" and "height" you simply mean the length of the edges, and you have 4 PointF structures in a list or array, you can do:

 double width = Math.Sqrt( Math.Pow(point[1].X - point[0].X, 2) + Math.Pow(point[1].Y - point[0].Y, 2)); double height = Math.Sqrt( Math.Pow(point[2].X - point[1].X, 2) + Math.Pow(point[2].Y - point[1].Y, 2)); 
+6
source

Just use the algorithm for the distance between two points. If you have points A, B, C, D, you will get two distances.

sqrt((Bx-Ax)^2 + (By-Ay)^2) will be equal to sqrt((Dx-Cx)^2 + (Dy-Cy)^2)

sqrt((Cx-Bx)^2 + (Cy-By)^2) will be equal to sqrt((Ax-Dx)^2 + (Ay-Dy)^2)

Choose one to be your width and the other to be your height.

+5
source

Let say that the uppermost corner is A. Then name the other edges counterclockwise as ABCD

rectangle width = distance between A and B
rectangle height = distance between B and C

The formula for finding the distance between two points means that A (x1, y1) and B (x2, y2):

 d = sqrt( (x2 - x1)^2 + ( y2 - y1)^2 ) 

where d is the distance.

+1
source

All Articles