Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Width and Height from 4 points of a polygon

Tags:

c#

.net

point

gdi

I have four points which form a rectangle, and I am allowing the user to move any point and to rotate the rectangle by an angle (which rotates each point around the center point). It stays in near-perfect Rectangle shape (as far as PointF precision allows). Here's an example of my "rectangle" drawn 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 is not rotated, but once I rotate it my math returns the width and height shown by the red outline here:

enter image description here

Assuming I know the order of the points (clockwise from top-left for example), how do I retrieve the width and the height of the rectangle they represent?

like image 347
Trevor Elliott Avatar asked Sep 19 '11 17:09

Trevor Elliott


People also ask

How do I find the width of a polygon?

Adding the two distances (furthest distance to the left and furthest to the right) gives the overall width of the polygon, square to the longest dimension line.

What is the width of a polygon?

In terms of this tool, the polygon width is the sum of shortest distances from the center line nodes to the polygon borders.

How do you calculate the width of an object?

Measure one of the two shorter sides. Because a rectangle has a two sets of congruent sides, it does not matter which side you measure. This is the width. If you know the area and the length but not the width, you can also find the width by dividing the area by the width.

How do you find the width of a base?

If you have the area A and length h , its width w is w = A/h . If you have the perimeter P and length h , its width is w = P/2−h . If you have the diagonal d and length h , it's width can be found with w = √(d²−h²) .


3 Answers

If by "width" and "height", you just mean the edge lengths, and you have your 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));
like image 161
Reed Copsey Avatar answered Sep 30 '22 00:09

Reed Copsey


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)

Pick one to be your width and one to be your height.

like image 45
Alain Avatar answered Sep 30 '22 00:09

Alain


Let's say top-most corner is A. Then name other edges anti-clockwise as ABCD

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

Formula to find distance between two points say A(x1,y1) and B(x2,y2) is:

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

where d is distance.

like image 27
Arslan Ahson Avatar answered Sep 30 '22 02:09

Arslan Ahson