Calculating geometry for a polyline of a given thickness

323 Views Asked by At

Given points, p1, p2, p3, p4 ... pn representing a polyline; how do I calculate the geometry (specially vertices - for drawing in graphics applications) assuming a given thickness d. (As in the diagram below)

enter image description here

Specifically need to find a, b, and c. Note that line p1 a is perpendicular to line p1 p2. Line p2 b is not perpendicular to line p1 p2 and also not to line p2 p3.

b is the intersection of the line b c and line a b which are parallel to line p2 p3 and line p1 p2 respectively.

d' is not important, but may prove useful.

Also since all points are represented on the screen as 2 tuples - (x, y); it would be helpful to know a, b and c in the 2 tuple form.

I am thoroughly blanking on how to do this. Any pointers at all will be helpful.

1

There are 1 best solutions below

6
On BEST ANSWER

Consider the 2D vector $\vec P_{12}$ from $P_1$ to $P_2$. You normalize it to unit length by dividing by its components by its length

$$L_{12}=\sqrt{(P_{2x}-P_{1x})^2+(P_{2y}-P_{1y})^2}$$ and get $\vec p_{12}=(p_{12x},p_{12y})$.

The vector $\vec n_{12}=(-p_{12y},p_{12x})$ is perpendicular to it and also has unit length.

Then $$a=P_1+d\,\vec n_{12}.$$ Similarly, $$c=P_3+d\,\vec n_{23}.$$

$b$ is a little harder. It lies on the bissectrix of $P_{12}$ and $P_{23}$, with a direction vector obtained by the sum $\vec n_{12}+\vec n_{23}$, which you need to normalize to get $\vec n_{123}$.

The distance $d'$ is the same as $d$ with a correction factor obtained by trigonometry from the dot product of $\vec n_{12}$ and $\vec n_{123}$.

Then,

$$b=P_2+d\frac{\vec n_{123}}{\vec n_{12}\cdot \vec n_{123}}.$$