# How to calculate the x/y coordinate of F in this diagram (geometry)

In the diagram, I've provided, how do I calculate the $x$, $y$ coordinates 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.

-

## migrated from stackoverflow.comAug 31 '11 at 17:32

This question came from our site for professional and enthusiast programmers.

Do you know anything about the angles or lengths of the sides of the triangles? –  Dan W Aug 31 '11 at 17:26
@Dan The X/Y coords for A,B,C is known so I could find the lengths of any side. –  Emperorlou Aug 31 '11 at 17:28
@Dan: I can't find x or Y until I know F. And if I knew that, I'd be good anyway. I'll try creating the question in math as you suggested. Thanks –  Emperorlou Aug 31 '11 at 17:31

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}$.

-
This answer was a lot easier for me to piece together in my head since it used the symbols from my diagram. I appreciate it! I did however find a more exact formula for what I'm trying to do elsewhere on the web and I've posted it. –  Emperorlou Sep 1 '11 at 2:33
@Emperorlou If it helped in some way up-voting is appreciated (or maybe even accepting, as it's exactly the solution you came up with, in a format more appropriate to math.stackexchange). –  Christian Rau Sep 1 '11 at 15:17
Christian, @Emperorlou has yet to acquire the rep necessary to upvote your answer... –  Guess who it is. Sep 1 '11 at 15:21

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*}

-
Thanks for the answer, I'm sure its right but its hard for me to translate that into something I can use with pure x/y co-ords as my variables. I've posted another answer I found on the net that is a bit closer to my world. But thanks anyway! –  Emperorlou Sep 1 '11 at 2:31

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:

p1.x-p2.x;
p1.y-p2.y;
p1.z-p2.z;


This code seems to be working for me though

The important code bits are as follows (in pseudo code):

function getX(Vector p1, Vector p2, Vector p3):float
{
Vector e = p2 - p1;
return p1.x + e.x * dot(e, p3 - p1) / len2(e);
}

function len2(v):float
{
return v.x*v.x + v.y*v.y;
}

-
This formula is not more exact than mine, it is exactly mine, transformed from vector algebra into programming (guess how dot and $\cdot$ and len2 and $\|\cdot\|^2$ are related to each other?). But I have to admit I had a little error in my solution so you might not have seen the similarity at first (corrected it already). Even if you searched for a programming solution, it is not really hard to transform vector algebra into programming, especially when you already have functions for the dot product and the vector norm and as we're on math.se, your answer is not really neccessary. –  Christian Rau Sep 1 '11 at 15:11