Polygon Normal Extrusion

Started by
2 comments, last by BoxyCraft 14 years, 6 months ago
Hi. Im trying to extrude the vertexes of a polygon along their normals, so that the new lines that are created are a certain distance apart from the old ones. Please look at the following pictures to see what I mean: Vertex Extrusion Problem - I need B or the distance from A to B Vertex Extrusion Problem - What I want I have a, b and A and I want to calculate B, or the distance needed from A to B, so that the lines are a set distance apart. The reason I want this is to draw a thick border around a polygon. Thanks to anyone who can help! :):):)
Advertisement
If you know how the dot product works, it's not hard.

Let's try to find out the vector from A to B, v. First we need the normals to a and b. Let's call them Na and Nb respectively. Our vector v needs to have a length of 1 when projected along Na and a length of 1 when projected along Nb. If Na and Nb have length 1 this is

v . Na = 1
v . Nb = 1

This is a system of two linear equations with two variables (the components of v). Solve it and you'll have your answer.

In code:
struct Vector2D {  double x, y;  Vector2D(double x, double y) : x(x), y(y) {  }};// This function returns the vector from A to B.// Na and Nb are the unit-length normals to a and b in your diagram.Vector2D compute_AB(Vector2D const &Na, Vector2D const &Nb) {  double D = Na.x*Nb.y - Na.y*Nb.x;  return Vector2D((Nb.y-Na.y)/D, (Na.x-Nb.x)/D);}

One warning is that this is a very hard problem to solve in general, i.e for arbitrary amounts of inflation/deflation you really need to use a medial axis or straight-skeleton approach.

I've forgotten how I derived this, but I'm doing the following:
n = Na + Nb;AB = n * (2/(n.x*n.x + n.y*n.y));


I have no idea how this works, but it does; if anyone can shed any light on this I'd be greatful -- I completely forget how I arrived at it! :(

Alvaro's method seems to be based on line intersection, which will break when the normals are parallel; the above will handle Na == Nb (i.e parallel and pointing in same direction) but will still break when Na == -Nb (i.e parallel and opposite direction).
Hi thanks a lot! I used raigans solution becuase I need parallel lines, but thanks both of you for helping! :D

It works fine for outlining shapes, but I also want to use it to draw ropes, so Il have to fix the problem with opposite directions. Also if the lines are almost opposite it stretches the normal way too far, so I might find another solution for that anyway.

Cheers, Thanks! :) :)

This topic is closed to new replies.

Advertisement