views:

105

answers:

4

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: 

This link contains an algorithm that is similar to what you are trying to do:

That is reflect a ray off a normal.

alt text

C Johnson
-1: re-read the question - he wants the reflection of a *point* across a *line*
Paul R
re-read my response: That's is why I said it is SIMILAR. In English that means related to, or pertaining to. The Normal has the same internal data members as a line. And a point also has the same internal data members as a vector. That ultimately is why I said it is a SIMILAR problem.
C Johnson
+8  A: 

Ok, I'm going to give you a cookbook method to do this. If you're interested in how I derived it, tell me and I'll explain it.

Given (x,y) and a line y = ax + c we want the point (x', y') reflected on the line.

Set d:= (x + (y - c)*a)/(1 + a^2)

Then x' = 2*d - x

and y' = 2*d*a - y + 2c

Il-Bhima
Would be interested to see the derivation too
McGin
Ok, as soon as I get home tonight I'll write it out with some images.
Il-Bhima
Carlos explained it very elegantly, much better than I could.
Il-Bhima
+2  A: 

This is a simple explanation of Il-Bhima's solution. The trick is to notice that what you want is to project that point orthogonally on the line, move it by that much, and then move it once again, in the same direction.

For these types of problems, it's easier to work with a slightly more redundant representation for a line. Instead of y = m x + b, let's represent the line by a point p on that is on the line and a vector d in the line's direction. Let's call this point p = (0, b), the vector d = (1, m), and your input point will be p1. The projected point on the line will be pl and your output point p2, then, is p1 + 2 * (pl - p1) = 2 * pl - p1

The formula you need here is the projection of a vector v onto a line which goes through the origin in direction d. It is given by d * <v, d> / <d, d> where <a, b> is the dot product between two vectors.

To find pl, we have to move the whole problem so that the line goes through the origin by subtracting p from p1, using the above formula, and moving it back. Then, pl = p + (d * <p - p1, d> / <d, d>), so pl_x = p_x + (b * p1_x) / (1 + m * m), pl_y = p_y + (m * p1_x) / (1 + m * m), and then use p2 = 2 * pl - p1 to get the final values.

Carlos Scheidegger
+1 That's a great explanation!
Il-Bhima
A: 

Here's a simple way to get the answer which IB/CS have explained, if you don't have the internet at hand and have forgotten the formula!

On a piece of paper scribble out a cartesian graph, shove a dot somewhere (for example, roughly at 3,4) and draw an example line (perhaps with something like a,c as 1/2,5)

Now just MOVE everything down and across until your point, is on the origin.

Look at the triangle you just made .. you'll see it's now easy to get the right-angled line you need, later just add to move back to where you started.

Joe Blow