
In the diagram I've provided, how do I calculate the x,y coordinate of F if the points A,B,C are arbitrary points on a grid?
I'm looking for a formula to solve F's X axis and another formula to solve F's Y axis.
|
In the diagram I've provided, how do I calculate the x,y coordinate of F if the points A,B,C are arbitrary points on a grid? I'm looking for a formula to solve F's X axis and another formula to solve F's Y axis. |
|||||||||
|
|
I hope you are fit in simple vector algebra: First you compute the vectors $\mathbf{c}=A-B$ $\mathbf{a}=C-B$. By projecting $\mathbf{a}$ onto $\mathbf{c}$ you get the vector $\mathbf{x}$ $\mathbf{x}=\frac{\mathbf{a}\cdot\mathbf{c}}{\|\mathbf{c}\|^2}\mathbf{c}$ from which you can easily obtain the vector $\mathbf{y}=\mathbf{c}-\mathbf{x}$ and the point $F=B+\mathbf{x}$. |
|||||||
|
|
All you need do is to project the point C onto the line connecting A and B. In general, the projection of a point $(c,d)$ onto a line $y=mx+b$ is $$\begin{align*} x&=\frac{md + c - mb}{m^2 + 1}\\ y&=\frac{m^2 d + mc + b}{m^2 + 1} \end{align*}$$ |
||||
|
|
I guess my question was moved to math.stackexchange.com a bit prematurely since I'm actually looking for an answer in "computer" rather than in "math" (since I'm not fluent in math :p). I managed to find a website that broke down the answer in a way I was able to easily digest and here is a link to the answer was the best fit for me: http://forums.tigsource.com/index.php?topic=16501.0 In this pseudo code, p1, p2 and p3 are all vectors (eg p1.x, p1.y, p1.z). It should work with a 2D or 3D vector. For those unfamiliar with dealing with vectors, when I write p1-p2, literally it means:
This code seems to be working for me though The important code bits are as follows (in pseudo code):
|
|||
|