algorithmgeometry

Algorithm for reflecting a point across a line


Given a point (x1, y1) and an equation for a line (y=mx+c), I need some pseudocode for determining the point (x2, y2) that is a reflection of the first point across the line. Spent about an hour trying to figure it out with no luck!

See here for a visualization - http://www.analyzemath.com/Geometry/Reflection/Reflection.html


Solution

  • Ok, I'm going to give you a cookbook method to do this. If you're interested in how I derived it, see http://www.sdmath.com/math/geometry/reflection_across_line.html#formulasmb

    Given point (x1, y1) and a line that passes through (x2,y2) and (x3,y3), we can first define the line as y = mx + c, where:

    slope m is (y3-y2)/(x3-x2)

    y-intercept c is (x3*y2-x2*y3)/(x3-x2)

    If we want the point (x1,y1) reflected through that line, as (x4, y4), then:

    set d = (x1 + (y1 - c)*m)/(1 + m^2) and then:

    x4 = 2*d - x1
    y4 = 2*d*m - y1 + 2*c