Search code examples
cmathvectorvector-graphicscad

Algorithm to find the inner side of a polygon


Within my CAD-like application I have different kinds of 2D polygons. They can be concave or convex, and they can be drawn clockwise or counter-clockwise.

Now I want to find out where the inner side of such a polygon is, means at its beginning I want to add a a vector which has a 90 degree angle to the following one but which points to the inner side of the polygon.

So: any idea how I can calculate this and find the inner side for this additional vector?

Thanks!


Solution

  • Given the n edge vectors v_1, ..., v_n of the polygon you can find the inner side as follows:

    1. for i from 1 to n calculate the angle between v_i, v_(i+1) (for i=n use v_n and v_1 instead)
    2. sum up these angles

    If the sum is 2pi/-2pi (or 360/-360 degrees) the inner side lies "to the right/left"