In a right-angled triangle, given Vector2D A, B and an angle, what's the Vector2D C?

Hello,
Please see my attached picture, it’s a 2D game, I have the Vectors of PointA and PointB, and the degree of the AngleA, and the angle-C is a right angle.
I’m wondering how can I get the Vector of PointC?

It would be nice if there are existing APIs in Unity that could solve this, otherwise, higher performance of culculation would be cool.

Thank you very much for checking this out.

Pythagoras theorem incoming! Also remember that angle B = 180 - angleA - 90

https://lh3.googleusercontent.com/proxy/NsgeHvZ7r8O85VmuiCHMhyaNRy29rPFh9bhCSsr1FMI93FtLnnlWXq6xPy_wS2ZCbbENSKMIaOq7JikB8SnnFuCzHdU09rypt3yWG0N2-dbCaMmcwKSNlHp5qg2rnhO3Vkfpc6lFcSOoNkpVeaAczEEDZg

Thank you so much for your promp reply, but I still don’t get it, even I can get the length of all line segments, but how I can get the X, Y coordinates of VectorC? Can I have more clues?

you need some trig

So I’m going to refer to angle A as ‘theta’ just to avoid confusion with point A.

(B - A).magnitude = hypot
sin(theta) * hypot = len(BC)
cos(theta) * hypot = len(AC)

With this we now know the length of either leg. Next is to find the direction of either leg. We could solve for angle(B) and solve from that… or we already have theta=angle(A), so might as well just use that. Which means, really, all we need from above is the cos(theta)*hypot

Thing is theta doesn’t necessarily denote direction and we actually have 2 right triangles here that resolve all this information we have. We could easily get the mirror of C over the line AB. Arithmetically/Geometrically they’re both valid values (just like how +/-2 are both valid for sqrt(4)).

So we either need to assume some information… like “C is counter-clockwise around point A from B”. But it’d also be valid to assume “C is clockwise around A from B”. A way we can do this is to just apply a sign to theta, negative meaning one direciton, positive meaning the other.

With that said… there’s tons of ways to easily rotate a Vector2, like this method:

/// <summary>
/// Rotate Vector2 counter-clockwise by 'a'
/// </summary>
/// <param name="v"></param>
/// <param name="a"></param>
/// <returns></returns>
public static Vector2 RotateBy(Vector2 v, float a)
{
a *= Mathf.deg2Rad;
var ca = System.Math.Cos(a);
var sa = System.Math.Sin(a);
var rx = v.x * ca - v.y * sa;
return new Vector2((float)rx, (float)(v.x * sa + v.y * ca));
}

Or you could just exploit Vector3 and Quaternion and do:

var AB = (B - A);
Vector2 v = (Vector2)(Quaternion.Euler(0f, 0f, theta) * (Vector3)AB);

Then from there just normalize and multiply by the length of that leg:

float hypot = AB.magnitude;
v = v.normalize * (Mathf.Cos(theta) * hypot);

Of course this is a vector from A to C, to get C just add A back on:

var C = A + v;

I believe this is the magic sauce you’re looking for:

If you have enough of the parts, you can determine all the rest of the parts based on those equations.

I think you have angle A, length AB, angle C, and that gives you angle B by the sum of the angles of a triangle, so I think you’re good.

Although I din’t get exactly the output I expected when I tried to work out the VectorC coordinates I described, but yes, I can work around to get my job done by rotating the AB vector for now, but I’ll continue to work out the math I described earlier since I’ll still need it later on. Thank you so much for all the tips!

Yes, I really need to revisit all these, thank you for the sharing.

1 Like