Given a line and a point, how do you calculate the point on the line that is exactly 45 degrees?

1.9k Views Asked by At

Given a line connected by two points, (x1, y1) & (x2, y2).

And given a point not on the line, (x3, y3).

How do you calculate the point on the line that creates a line at a 45 degree angle. We'll call this point (x4, y4).

Picture Representation Here.

Thank you!

EDIT: *Question from the comments

If the (x3, y3) point is "far" from the line-segment created by (x1,y1), (x2,y2). It wouldn't be possible to create a 45 degree angle like describe in this picture. How can I check for this?

2

There are 2 best solutions below

12
On BEST ANSWER

One way to do it, could be a more elegant way:

First, we find the equation of a line through points $(x_{1},y_{1})$ and $(x_{2},y_{2})$. After that, we find the line through the point $(x_{3},y_{3})$ which is perpendicular to the line through points $(x_{1},y_{1})$ and $(x_{2},y_{2})$. After we do that, we find the intersecting point of those two lines, lets call it point $(x_{5},y_{5})$. Now we have one leg of the right triangle. We find the length of that leg, and we know that the angle being 45 degrees, both legs are the same length. Now we just add that length of the leg to the point $(x_{5},y_{5})$, and solve for point $(x_{4},y_{4})$. You will get two points by doing this.

Hope this helps, cheers!

0
On

The following is fundamentally the same as Shocky2’s answer, but uses homogeneous coordinates to crank out the solutions via a direct computation.

Let the three given points be called, respectively, $P_1$, $P_2$ and $P_3$, and let $O$ be the intersection of the line $\overline{P_1P_2}$ with its perpendicular through $P_3$. The two lines through $P_3$ that make a 45° angle with $\overline{P_1P_2}$ intersect it at points that are the same distance from $O$ as is $P_3$, namely, the perpendicular distance from $P_3$ to $\overline{P_1P_2}$.

We have, in homogeneous coordinates, $P_1\sim[x_1,y_1,1]$, $P_2\sim[x_2,y_2,1]$ and $P_3\sim[x_3,y_3,1]$. The line $\overline{P_1P_2}$ is given by the cross product $\mathscr L\sim P_1\times P_2$. We then have $$|\mathscr L\cdot P_3|=d\|P_2-P_1\|\tag1$$ where $d$ is the distance from $P_3$ to $\overline{P_1P_2}$. (The two sides of this equation are twice the area of $\triangle{P_1P_2P_3}$ computed in different ways.) This means that the two 45° points are $$P\sim O\pm{\mathscr L\cdot P_3\over\|P_2-P_1\|}{P_2-P_1\over\|P_2-P_1\|}=O\pm{(P_1\times P_2)\cdot P_3\over(P_2-P_1)\cdot(P_2-P_1)}(P_2-P_1).\tag2$$

The starting point $O$ can be computed in a couple of ways. One is to use the fact that it’s the orthogonal projection of $P_3$ onto $\overline{P_1P_2}$, from which we have $$O\sim P_3-{\mathscr L\cdot P_3\over(P_2-P_1)\cdot(P_2-P_1)}N\tag3$$ where $N$ is obtained from $\mathscr L$ by setting its last component to zero. This is convenient because it shares a coefficient with (2), so we can combine the two formulas to obtain $$P\sim P_3-{(P_1\times P_2)\cdot P_3\over(P_2-P_1)\cdot(P_2-P_1)}(N \pm (P_2-P_1)). \tag4$$ Usually one must now divide through by the third component to convert from homogeneous to Cartesian coordinates, but since the last component of $P_3$ is $1$ and those of $P_2-P_1$ and $N$ are zero, we can drop them everywhere except the scalar triple product in the numerator of (4) and produce Cartesian coordinates directly.

The only thing left to do is to see which, if any, of these points actually lies on the line segment $P_1P_2$, but that’s a simple matter of checking that their coordinates fall between those of the endpoints $P_1$ and $P_2$.