Search code examples
math2dcomputational-geometry

Calculating paralel lines coordinates with arbitrary angle


We have 2 parallel lines in the 2D space separated by 5 units:

Black - 0,0 -> 20,0

Red - 0,5 -> 20,5

The angle is 0 degrees. We always know the coordinates of the black line and the angle in degrees. The coordinates of the red line needs to be calculated given arbitrary angle.

sample

Is there any formula for such calculation?


Solution

  • Find direction vector for the first line

    dx = x2 - x1
    dy = y2 - y1
    

    Normalize it

    len = sqrt(dx*dx + dy*dy)
    dx = dx / len
    dy = dy / len
    

    Get perpendicular vector (note it is "right" normal, for "left" one change signs)

    nx = dy
    ny = -dx 
    

    Get parallel segment ends with d = parallel distance

    x3 = x1 + d * nx
    y3 = y1 + d * ny
    x4 = x2 + d * nx
    y4 = y2 + d * ny