Search code examples
javamathlibgdxdrawing

libGDX - How can I draw polygon as a line (Miter join type)?


If I have known vertices (RED) like this image:

enter image description here

How can I determine these below unknown vertices from above known vertices which join lines as a miter joining with each other?


Solution

  • First of all, look at this Q/A. Your task is the same, only a bit simpler, since line thickness is the same for all segments.enter image description here

    Basically, you need to calculate coordinates of D point, when coordinates of A, B, C and line thickness d are known.

    1) Calculate angle α, using Law of Cosines, like here How to calculate an angle from three points?

    2) Then you can find length L of vectors u and v:

    L =|u| =|v| = d / sin α;
    

    3) Calculate vector components for vectors u and v:

    ux = L * (Ax – Bx) / |AB|;
    uy = L * (Ay – By) / |AB|;
    

    |AB| - length of vector AB, can easily be found from coordinates of points A and B.

    vx = L * (Cx – Bx) / |BC|;
    vy = L * (Cy – By) / |BC|;
    

    4) Finally find coordinates of point D:

    Dx = Bx + ux + vx;
    Dy = By + uy + vy;
    

    5) Do that for all points of you line.