'Calculating the tangent of a line with an arbitrary slope
I have a pygame grid with two points, x1 y2 and x2 y2. I would like to calculate the points x3 y3 which correspond to a point on a tangent orthogonal to x1 y1 and x2 y2 and starts from x2 y2.
So far, I have thought about calculating the slope between x1 y1 and x2 y2 and then using the formula
tangent_slope = 1/((x1-x2)/(y1-y2))
to receive the slope of the tangent. However, this approach fails when y1 and y2 are the same value, as the slope would then approach infinity. I would like to avoid making the explicit distinction for this case and prefer a solution that includes every case and simply returns a dot that would be on the tangent orthogonal to the line between x1 y2 and x2 y2, with x2 y2 being the other dot that defines the tangent.
Solution 1:[1]
Use
X3 = X2 + (Y2 - Y1)
Y3 = X2 - (X2 - X1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yves Daoust |
