# Calculating the geometry of a thick 3-way miter joint

Following a very helpful post on another forum I've found a simple algorithm that takes an array of 2D vectors and creates a triangle strip. I've modified the source code for my purposes. I haven't tested it yet, but it should do the trick.

public static void PolyLineToTriangleStrip(Vector2[] pts, bool closed, float thickness, List<Vector2> vertices, List<int> indices)
{
var numPts = pts.Length;
vertices.Clear();
indices.Clear();
for (int i = 0; i < numPts; ++i) {
int a = ((i - 1) < 0) ? 0 : (i - 1);
int b = i;
int c = ((i + 1) >= numPts) ? numPts - 1 : (i + 1);
int d = ((i + 2) >= numPts) ? numPts - 1 : (i + 2);
var p0 = pts[a];
var p1 = pts[b];
var p2 = pts[c];
var p3 = pts[d];

if (p1 == p2)
continue;

// 1) define the line between the two points
var line = (p2 - p1).normalized;

// 2) find the normal vector of this line
var normal = new Vector2(-line.y, line.x).normalized;

// 3) find the tangent vector at both the end points:
//      -if there are no segments before or after this one, use the line itself
//      -otherwise, add the two normalized lines and average them by normalizing again
var tangent1 = (p0 == p1) ? line : ((p1 - p0).normalized + line).normalized;
var tangent2 = (p2 == p3) ? line : ((p3 - p2).normalized + line).normalized;

// 4) find the miter line, which is the normal of the tangent
var miter1 = new Vector2(-tangent1.y, tangent1.x);
var miter2 = new Vector2(-tangent2.y, tangent2.x);

// find length of miter by projecting the miter onto the normal,
// take the length of the projection, invert it and multiply it by the thickness:
//      length = thickness * ( 1 / |normal|.|miter| )
float length1 = thickness / Vector2.Dot(normal, miter1);
float length2 = thickness / Vector2.Dot(normal, miter2);

if (i == 0 && !closed) {
vertices.Add(p1 - length1 * miter1);
vertices.Add(p1 + length1 * miter1);
}

vertices.Add(p2 - length2 * miter2);
vertices.Add(p2 + length2 * miter2);

if (closed && i == numPts-1) {
indices.Add(i * 2 + 1);
} else {
indices.Add(i * 2 + 1);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 2);
indices.Add(i * 2 + 3);
}
}
}


Feeding it a sequence of vectors would produce something a bit like this:

However, I want to complicate matters by introducing another problem - 3 way joints (or rather n-way).

It must be possible to do this by building on the algorithm, but I'm not sure how I would modify the above algorithm to draw an n-way miter joint with perfect angle distribution like so:

The highlighted lines represent the source lines that would be fattened up by the algorithm.

I calculated and drew these in blender, unfortunately I'm at a bit of a loss to do it programmatically.

Obviously this cannot be achieved with simple line sequences, but that's beside the problem. All that' needed is a central point and n-ammount of points that represent the ends of outcropping lines, and then an algorithm to calculate the outer edges of the required shape.