Formula for finding the third vertex of a right triangle
Guys. Not good at mathematical transformations. Maybe someone has a ready-made formula for finding the coordinates of the third vertex (C) of a right triangle knowing the coordinates of the other two vertices (A and B). The AC and AB catheters are also known.
From geometry, I remember that it is necessary to solve a system of two equations, but there the devil will break his leg... Maybe there are already ready-made formulas for finding the x and y points with?
4 answers
Well, in general, everything is simple.
- The vector
AB = B - A
is coordinate-wise. Divide both coordinates by the length, we get a unit vector, let it bev1
. -
Rotate the vector
v1
by 90 degrees, we get a vector along the other leg. Let be the result ofv2
. Rotation by a simple formula:v2.x = -v1.y; v2.y = v1.x;
Alternatively turn the other way:
v2.x = v1.y; v2.y = -v1.x;
Having a unit vector
v2
along the second leg, we multiply the coordinate by the length of the second leg cathet, we get the vectorAC
.- Adding the vector
AC
to the coordinatesA
, we get the pointC
.
There will be two solutions, for turning clockwise or counterclockwise.
Working code in C++ for the correct answer:
//0. Длина катета АВ (ab):
// ab = Sqrt((xa_− xb_)^2+(ya_− yb_)^2)
//1. Вектор AB = B - A, покоординатно. Делим обе координаты на длину, получаем единичный вектор (v1):
// v1.x = (B.x - A.x) / ab === v1x = (xb_ - xa_) / ab
// v1.y = (B.y - A.y) / ab === v1y = (yb_ - ya_) / ab
//2. Поворачиваем вектор v1 на 90 градусов, получаем вектор вдоль другого катета (v2). Поворот по формуле:
// v2.x = -v1.y === v2x = -v1y
// v2.y = v1.x === v2y = v1x
// Альтернативно поворот в другую сторону:
// v2.x = v1.y;
// v2.y = -v1.x;
//3. Имея единичный вектор v2 вдоль второго катета, умножаем покоординатно на длину второго катета, получаем вектор AC:
// v3.x = v2.x * bc_ === v3x = v2x * bc_
// v3.y = v2.y * bc_ === v3y = v2y * bc_
//4. Прибавляем к координатам A вектор AC, получаем точку C:
// xc_ = xa_ + v3x
// yc_ = ya_ + v3y
void __fastcall TriangleStraight3V_01(int xa_, int ya_, int xb_, int yb_, int bc_, int &xc_, int &yc_)
{
int x2x1 = xa_ - xb_;
int y2y1 = ya_ - yb_;
double ab = Sqrt(x2x1*x2x1 + y2y1*y2y1);
double v1x = (xb_ - xa_) / ab;
double v1y = (yb_ - ya_) / ab;
double v3x = (v1y > 0 ? -v1y : v1y) * bc_;
double v3y = (v1x > 0 ? v1x : -v1x) * bc_;
xc_ = xa_ + v3x;
yc_ = ya_ + v3y;
}
My implementation on Pascal of the correct answer. Main function:
function FindPointB(T: Triangle; no: Integer): Point;
var
unitVec: Point;
begin
// Единичный вектор:
unitVec.x := (T.pointA.x - T.pointC.x) / T.sideB;
unitVec.y := (T.pointA.y - T.pointC.y) / T.sideB;
if no = 1 then begin // первое решение.
Result.x := T.pointC.x + (-unitVec.y * T.sideA);
Result.y := T.pointC.y + (unitVec.x * T.sideA);
end else begin // второе решение.
Result.x := T.pointC.x + (unitVec.y * T.sideA);
Result.y := T.pointC.y + (-unitVec.x * T.sideA);
end;
end;
In this statement, the problem has two solutions. Using the example of the Egyptian triangle:
Flowchart with formulas:
Example of program execution:
Method in C#
PointF GetOrtogonalPoint(PointF a, PointF b, float bc)
{
float x2x1 = a.X - b.X;
float y2y1 = a.Y - b.Y;
float ab = (float)Math.Sqrt(x2x1 * x2x1 + y2y1 * y2y1);
float v1x = (b.X - a.X) / ab;
float v1y = (b.Y - a.Y) / ab;
float v3x = (v1y > 0 ? -v1y : v1y) * bc;
float v3y = (v1x > 0 ? v1x : -v1x) * bc;
PointF c = new PointF();
c.X = a.X + v3x;
c.Y = a.Y + v3y;
return c;
}
The direction is set by the bc sign.