Question

I am writing a program (.net) to create a stadium style layout and need to determine the angle of rotation for each polygon compared to the horizontal.

This is so i can construct the contents of the polygon and also rotate this correctly to fit inside.

Given the below image as an example to simulate each variant of the facing direction (indicated by the red line) how could i determine the the rotation angle needed to get the shape to have the red line on top as is already shown by shape 5.

http://i40.tinypic.com/16ifhoo.gif alt text

I have found logic to determine the angle of the points that make up the red line, but I also need to know the rotation to get it back to horizontal.

I'm not sure if i need some central reference point for all polygons to help.

How could I best solve this?

Was it helpful?

Solution

If you know the angle of the red line for some polygon (a, say), then the polygon is on one side or other of that line. So:

  • Use the average colour of some pixels near the line on both sides to determine which is the case.
    • If the polygon is above the line, the rotation angle is 180+a.
    • If the polygon is below the line, the rotation is a.

where above and below correspond to the smaller-angle side and larger-angle sides of the line according to how you measure a.

OTHER TIPS

I would try to calculate the normal vectors on each red line (eg. 0 degrees for polygon 5, 45 degrees for 4, 90 degrees for 3, etc.) and then the angle you need to rotate that normal - and thus the matching polygon - so that the normal "points up" should be very simple.

Unfortunately I don't have the needed formulae available for you off the top of my head, but Googling "normal vector" and/or searching for it on Wikipedia should get you started just fine, I think. Possibly in the direction of the so called 'cross product'.

No central reference point for all polygons should be needed for this (normal direction is not related to absolute coordinates).

sin, cos, tan functions allow you to convert from triangle edge ratio to degrees.

Imagine, one end of red line is at (x1,y1) and other end is at (x2,y2). You can treat red line as hipotenuse of rectangular triangle and use arctan to get degrees.

Ratio between catheti is (x2-x1) / (y2 - y1). Rotation of red line then is arctan((x2-x1) / (y2 - y1)). Watch out for situations when y1-y1 is 0!

Let's try one example from your picture, polygon 6 with coords (55, 65) and (65, 55). Type in google: "arctan((65-55)/(55-65)) in degrees"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top