6

For a collision algorithm I am developing, I need to find out how to reflect a line over another.

Line 1:

y=ax+b 

Line 2:

y=cx+d 

Line 3:

(a result of reflecting line 1 over line 2) y=ex+f

Is there any algebraic way to determine e and f in terms of a, b, c, and d?

double-beep
  • 5,031
  • 17
  • 33
  • 41
A Parikh
  • 146
  • 1
  • 1
  • 10
  • keep in mind that you might run into y = infinity (perpendicular to y=0) if you are following this method, so switch between x=ay+b and y=ax+b when you need to, in order to avoid the problem :) – CME64 Jul 01 '13 at 01:26
  • This has been bothering me, sorry. The above is misleading. You CANNOT simply exchange the a and b values. x = (y-b) / a. Also, 99% of your problems with vertical slopes can be solved by saving the slope as a structure that contains a numerator and a denominator. Thus, you can know that if the denominator is 0, the line is vertical. – BrainSteel Jul 03 '13 at 02:38

3 Answers3

15

I have run over this exact same problem before. Stay with me here...

This problem involves two parts:

1. Find the point at which they intersect

to find where two lines intersect, we use the two equations of the lines:

y = M1x + B1
y = M2x + B2

Using substitution:

M1x + B1 = M2x + B2
M1x - M2x = B2 - B1
x(M1 - M2) = B2 - B1
x = (B2 - B1) / (M1 - M2)

To find the y value, just plug it in:

y = M1x + B1

2. Find the slope of the line from the other two slopes.

The second is far trickier. Using trigonometry, it is not impossible.

Let L1 be the "base line." (With a slope of M1)

Let L2 be the line that is to be reflected over the "base line." (With a slope of M2)

Let L3 be our resulting line. (With a slope of M3)

The equation I used is as follows:

double M3 = ((2 * M1) + (M2 * pow(M1, 2)) - M2) / (2 * M1 * M2 - pow(M1, 2) + 1);

Straight from my C code. It is important to note that both slopes should be defined. You can use L'Hopital's rule to get an equation when one of the slopes is approaching infinity.

ONWARD WITH THE EXPLANATION!

Line Diagram

Here is a crude drawing of three lines. L2 is reflected over L1, resulting in L3. Drawing is not exact. The angle between L1 and L2, as well as L2 and L3, is labelled as R.\ Here are the facts:

M1 = tan(A1) 
M2 = tan(A2) 
M3 = tan(A3) 

This comes from the definition of tangent.

A3 = R + A1

This is a little trickier to see, but if you draw a horizontal line at the point of intersection it becomes obvious.

Thus, our goal is to find tan(A3). To accomplish this, we need to find R. As we can see, R can be found in a triangle with A2 and the supplement of A1 as the other angles. Thus, we know:

R + (180 - A1) + A2 = 180
R - A1 + A2 = 0
R = A1 - A2

Let's take the tangent of both sides:

tan(R) = tan(A1 - A2)

From trigonometry, we know:

tan(R) =  (tan(A1) - tan(A2)) / (1 + tan(A1)tan(A2))
R = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2))

Arctan being inverse tangent. From our earlier formula, A3 = R + A1, we get:

A3 = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2)) + A1
A3 = arctan((M1 - M2) / (1 + M1*M2)) + A1

But we don't want A3. We want tan(A3). So again, we take the tangent of both sides.

tan(A3) = M3 = tan(arctan((M1 - M2) / (1 + M1*M2)) + A1)
M3 = tan(arctan((M1 - M2) / (1 + M1*M2))) + tan(A1) / (1 - tan(arctan((M1 - M2) / (1 + M1*M2))) * tan(A1))

Unfortunately, that's disgustingly hideous. Replacing tangents with slopes and simplifying, we get

M3 = ((M1 - M2) / (1 + M1*M2)) + M1 / (1 - ((M1 - M2)/(1 + M1*M2)) * M1)
M3 = (M1 - M2 + M1*(1 + M1*M2)) / (1 + M1*M2 - M1*M1 + M1*M2)
M3 = (M1^2 * M2 + 2*M1 - M2) / (1 + 2*M1*M2 - M1^2)

Which is the exact same as the formula above. Sorry about all the ugly math. When M2 is completely vertical, you can use L'Hopital's rule to get

M3 = (M1^2 - 1) / 2*M1

If anyone is so inclined, check my math. But I'm tired right about now.

BrainSteel
  • 669
  • 5
  • 17
0
  1. Assuming the two lines are not parallel to each other

Step 1:

First find the intersection of the line y = ax + b with line y = cx + d , that is by solving comes out to be

m = (d - b) / (c - a)

Step 2:

The final line has point of the form (x , ex + f) , so wjat we know is the line joining the point and the corresponding image is perpendicular to the mirror line AND the the midpoint of the first point and its image lies on the mirror line. Solving for first requirement ....

(Slope of line joining point and its image) * (Slope of the mirror line) = -1

we get ...

c * (e*pt + f - a*n - b)/( pt - n ) = -1 -----> The first equation .

Then the midpoint of the point and its image lie on the central line , i.e.

Y coordinate of the midpoint - ( c* x coordinate of midpoint + d) = 0

y coordinate of midpoint = (a*n + e*pt) / 2 and x coordinate = ( pt + n) / 2

putting it above we get...

(a*n + e*pt)c - c( pt + n) - 2d =0 ----> second equation

3.

now the point and its image make equal angles from the intersection point .... a simple way of saying that angle between mirror line , point line and image line , point line being equal ... therefore ... the tangent of angle between lines mI and mM is equal to that of mM and mP

equating we get

( mM + mP ) / ( 1 + mp*mM) =( mI - mM )/ (1 + mI*mM)

where mM = c , mI = e, and mP = a -----> third equation

put it in their respective

places you get three equations in three unknowns , pt , e and f and solve ... just x in place of n earlier and there you have your e , f in terms of a , b , c , d.

Solve it yourselves ....

However if they are parallel its simple , you have two equations in two variables use the midpoint method

0

Yet another method:

Matrix of affine transformation for reflection relatively to line y=ax+b (works for non-vertical lines!). Let's pa = 1+a^2, ma = 1-a^2, then matrix is (from Nikulin's Computer Geometry book)

 ma/pa    2a/pa     0
 2a/pa   -ma/pa     0
-2ab/pa   2b/pa     1  

So we can get two arbitrary points at second line, apply this transform, and calculate new line equation

MBo
  • 77,366
  • 5
  • 53
  • 86