Search code examples
geometrylinemirroring

Mirror a line segment


I have a line segment (x1, y1), (x2, y2) that i need "mirrored" so the new line becomes perpendicular to the first one and passes through its middle.

The line segment (0,0),(2,2) should return a new line segment (0,2),(2,0)

Can anyone help me with a function/formula to handle this?


Solution

  • The midpoint is (mx,my) = ((x1+x2)/2,(y1+y2)/2).

    To rotate the endpoints 90 degrees about the middle, first compute a vector:

    (dx,dy) = (x1-mx),(y1-my)
    

    then rotate it 90 degrees:

    dx1 = -dy
    dy1 = dx
    

    Then the new point becomes:

    x1 = mx+dx1
    y1 = my+dy1
    

    Repeat for x2,y2.

    You may also combine steps if you're careful.