# How to project a vertex to the nearest point along a vector?

This is probably a dumb question. But how do I find the nearest point along an edge? I'm not good at vector math and can't figure this out.

So for example, I have a singular object. That object has 3 verts. That's all. Just 3 verts. Arbitrarily placed. From that, I imagine a vector defined by 2 of the verts. Then, I imagine the 3rd vert projected to the nearest point of the vector. That's the position I want. How did I get it? There are a number of explanations on other websites for this, but the math equations still go over my head. Can someone show me how it's expressed in geometry nodes?

I thought it would be as easy as subtracting point A from point B, then projecting point C onto that. But no, the projection snaps down toward the origin point. As the magnitude is relative to the origin of the object I suppose. So I guess I need to add the projection with another vector to get it back up to where it's supposed to be? IDK. I'm lost.

Given the positions of points A, B, and C, what is the position of point D on line AB such that AB is perpendicular to CD?

One way to solve this is using the dot product. The dot product of two normalized vectors is the length of the projection of one vector onto the other. If we re-orient these vectors such that one is the horizon, the dot product is the length of the shadow cast from the other by a noon-time sun.

Using our three positions, we first create two vectors, AB and AC, and normalize them. If we take the dot product of these two vectors, we get the "shadow" cast by normalized AC on AB (or vice versa.) We can scale AB by the length of that shadow to get the projection.

AC is probably not a unit-length vector. But as AC gets longer, the length of its shadow gets longer as well, in the same proportion. So we multiply the length of the shadow by the length of AC.

We have this shadow as a vector relative to A. We need to add it back to A to get its position relative to the object origin.

I'm creating a line both for CD and AB and joining them so you can see that they are perpendicular.

I do wish there was a "sample nearest edge" node to simplify problems like this (and several others.) But, alas.

• Just curious: Why don't you actually project AC onto AB? Dec 7, 2023 at 8:57
• this is the quellenform's suggestion: i.imgur.com/AzvujEI.png Dec 7, 2023 at 9:56
• This long form solution will help me understand the math behind it all I think. Given time to study it more. Though, I do see why the short solution is more expedient and tighter, I'm glad I got the long form to help me "see" it. Dec 7, 2023 at 10:43
• @quellenform Well, because I didn't know that existed :) Thanks; happy to see it was useful to Smeebit anyways. Come to think of it, doing a pair of transforms by - and + A.pos at beginning and end would probably also simplify it a bit (ie, think of it as a different coordinate space.) Dec 7, 2023 at 17:30

I would think that this is the easiest way to solve it:

Here I simply project vector $$\vec{AC}$$ onto vector $$\vec{AB}$$.

• Adding A after project here is important. Otherwise, this method only works when A is on the origin. Markus von Broady's image has it Dec 7, 2023 at 10:46
• @Smeebit Absolutely right, very well observed! ...that got lost here in a hurry and has of course just been corrected. Dec 7, 2023 at 12:28