# Polygon Normal Extrusion

This topic is 3342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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: 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! :):):)

##### Share on other sites
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);}

##### Share on other sites
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).

##### Share on other sites
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! :) :)

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013741
×