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
A reflection over line is a transformation in which each point of the original figure (the pre-image) has an image that is the same distance from the reflection line as the original point, but is on the opposite side of the line. In a reflection, the image is the same size and shape as the pre-image.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With