# Constructing a line with a known line, intersection point and angle.

I am creating a Java game with collisions. I found myself stuck on the following problem.

I have got two known lines: $y$ and $i.$ $i$ is the inbound direction and $o$ the outbound direction, therefore both angles are the same. From $y$ and $i,$ I have calculated $\alpha.$ I also know the coordinates of the intersection point $(x,y).$ $y$ is not the horizontal axis. $y$ and $i$ can be any line of the format $y = ax + b.$

Could anyone help me out on getting the equation $y = ax + b$ for line $o$?

-
Draw a horizontal line through the intersection point of the lines. Call this line $\ell$. If the angle between $\ell$ and $y$ is $\theta$ (which you can find knowing the slope of $y$), then the angle between $\ell$ and $o$ is $\theta+\alpha$. –  David Mitra Mar 18 '12 at 22:06

I'll use capital letters for the lines to keep the notation a bit clearer. (You used the same variable name, lowercase $y$, for one of the lines and for one of the coordinates; that's a bad idea.)

As I understand the question, you have lines $Y$ and $I$ given by $y=a_Yx+b_Y$ and $y=a_Ix+b_I$, respectively, and you want to find the line $O$ in the form $y=a_Ox+b_O$. I suspect that this is a suboptimal formulation of the problem in two respects: You can't represent vertical lines this way, and you can't reconstruct a direction from a line. (I take the arrows in the diagram and your mention of "inbound direction" and "outbound direction" as indications that what you're actually interested in is a direction and not just a line; that would make sense if you're writing a game with collisions.)

So I'll answer a different question instead, which is likely to be of more use to you; it's straightforward to transform the answer to the $y=ax+b$ format if that really is what you want. Suppose you have unit vectors $\vec r$ and $\vec s$ specifying the directions of $Y$ and $I$, respectively, and you want to find the unit vector $\vec t$ specifying the direction of $O$. Then

$$\vec t=2(\vec s\cdot \vec y)\vec y-\vec s\;.$$

-

This is an approach using standard methods from analytic geometry and trigonometry.

For convenience instead of $(x,y)$ I call the coordinates of the point of intersection $(h,k)$. Since the line $o$ passes through $(h,k)$ its equation is of the form $$y=a(x-h)+k=ax-ah+k=ax+b,\tag{0}$$ where $a$ is the slope and $b=k-ah$. We need only to find $a$. By the same reason the equation of the lines $y$ and $i$ are of a similar form. Let the equation of $y$ be $$y=m(x-h)+k=mx-mh+k=mx+b_{y},\quad b_y=k-mh\tag{A}$$

and the equation of $i$

$$y=m'(x-h)+k=m'x-m'h+k=m'x+b_{i},\quad b_i=k-m'h.\tag{B}$$

Let $\theta _{y}$ and $\theta _{i}$ be the angles between a horizontal line $\ell$ and the lines, respectivelly, $y$ and $i$. The least angle $\alpha$ between lines $y$ and $i$ is $\alpha =\theta _{y}-\theta _{i}$ or $\alpha =\pi -\left( \theta _{y}-\theta _{i}\right)$ (see picture). Therefore $\tan \alpha =\tan \left( \theta _{y}-\theta _{i}\right)$ or $\tan \alpha =-\tan \left( \theta _{y}-\theta _{i}\right)$. Applying the formula of tangent of the difference of angles $$$$\tan \left( \theta _{y}-\theta _{i}\right) =\frac{\tan \theta _{y}-\tan \theta _{i}}{1+\tan \theta _{y}\cdot\tan\theta _{i}}$$\tag{1}$$ we get $$$$\tan \alpha =\left\vert \frac{m-m'}{1+mm'}\right\vert ,$$\tag{2}$$ where $m=\tan \theta _{y}$ and $m'=\tan \theta _{i}$. The least angle between $y$ and $o$ is equal to $\alpha$. Similarly it verifies the equation $$$$\tan \alpha=\left\vert \frac{m-a}{1+ma}\right\vert ,$$\tag{3}$$ where $a=\tan \theta _{o}$ is the slope of line $o$. From $(2)$ and $(3)$ we have $$$$\frac{m-m'}{1+mm'} =\pm \frac{m-a}{1+ma}.$$\tag{4}$$ Assuming that $\alpha \leq \pi /2$ and solving for $a$ we get two possible solutions, depending on the sign. The positive sign is excluded because the respective solution is $a=m'$. For the negative sign we get

$$$$a=\frac{2m-m^{\prime }+m^{2}m^{\prime }}{1-m^{2}+2mm^{\prime }}.$$\tag{5}$$

Consequently the equation of the line $o$ is given by $$$$y=\frac{2m-m^{\prime }+m^{2}m^{\prime }}{1-m^{2}+2mm^{\prime }}x-\frac{2m-m^{\prime }+m^{2}m^{\prime }}{1-m^{2}+2mm^{\prime }}h+k.$$\tag{6}$$

-

You should generally try to avoid dealing directly with angles and lengths: use vector arithmetic when possible, or matrix transformations describing geometric operations.

In this case, you're really doing is computing a reflection across the line $Y$, right? The matrix that computes this is surely something you could look up, but it's easy enough to compute ourselves: we can (usually) solve for the matrix if we have two equations, and for that we just need to know how it acts on two points.

Since $Y$ is given by the equation $y = ax+b$, we know

• A parallel vector to $Y$ is $(1, a)$
• A perpendicular vector to $Y$ is $(a, -1)$

The matrix that reflects vectors has to send $(1,a)$ to $(1,a)$ and $(a,-1)$ to $-(a,-1)$. We could set up a system of equations and solve, or set up a matrix equation and solve, but here we know the eigenvectors and eigenvalues of the matrix so we can write it as

$$R = \left( \begin{array}{cc} 1 & a \\ a & -1 \end{array} \right) \left( \begin{array}{cc}1 & 0 \\ 0 & -1 \end{array} \right) \left( \begin{array}{cc}1 & a \\ a & -1 \end{array} \right)^{-1} = \frac{1}{a^2 + 1} \left( \begin{array}{cc}1 - a^2 & 2a \\ 2a & a^2 - 1 \end{array} \right)$$

So if $I$ is given by the equation $y = a_I x + b_I$, then a parallel vector to it is $(1,a_I)$. Use the matrix to reflect it

$$R \left( \begin{array}{c} 1 \\ a_I \end{array} \right) = \left( \begin{array}{c} s \\ t \end{array} \right)$$

Since you already know that the intersection point $(x_P, y_P)$ lies in $O$, its equation is given by $s(y-y_P) = t(x-x_P)$, and you can wrap it up from there.

One nice thing about this is that it works naturally on vectors. This is very useful if, for example, you didn't just have the equation of $I$, but actually had a velocity vector you wanted to reflect. Then, applying the matrix $R$ immediately gives the reflected velocity vector with the same magnitude.

-