1

I have 2D polygons, with their vertices positioned in local polygon-space. I'm trying to calculate new vertices that would form a uniform length edge around the inside of the polygon.

Currently to calculate the edge, I'm basically shrinking the original vertices. For each polygon vertex I calculate the negated unit vector and multiply this by a constant edge length factor. Then I add this to the original polygon vertex.

Pseudocode:

const float edgeLength = 0.5;
for each vertex v
    vec2 n = -v.unit();
    vec2 edgeVertex = v + n * edgeLength;

The result works fine on regular polygons:
screenshot 1

However, on other polygons, such as rectangles, the edge lengths are not uniform:
screenshot 2

I've tried many different attempts, but nothing seems to work so far, any help would be greatly appreciated, thanks! (Please ignore that the polygons are rendered in 3D, the actual polygon data is 2D).

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
Chris
  • 13
  • 2

1 Answers1

1

Move each edge a fixed distance, in a direction perpendicular to the edge (all to the left, if the edges run counterclockwise). Then calculate the intersections of the new edges-- these are the new vertices.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • Don't you need to identify the _interior_ to be able to chose one normal or the other? – Dr. belisarius Jun 15 '11 at 04:06
  • @belisarius: yes, but that might be trivial if there's a convention like, say, all the edges running counterclockwise. Just one of the things that might or might not be a pain, depending on details of existing implementation we don't know. – Beta Jun 15 '11 at 04:48
  • 1
    A solution that works with concave polygons too would be optimal. I've also found [this link](http://stackoverflow.com/questions/1109536/an-algorithm-for-inflating-deflating-offsetting-buffering-polygons) which seems to be a possible solution, though I can't find any source code outside of CGAL. – Chris Jun 15 '11 at 05:13
  • @belisarius, @Chris: my solution works with non-convex polygons. – Beta Jun 15 '11 at 05:22
  • Thanks Beta! Your solution works, I found some example code [here](http://stackoverflow.com/questions/3749678/expand-fill-of-convex-polygon) which pretty much implements your solution, and it works perfectly! – Chris Jun 15 '11 at 06:29