There is a triangle. Points are A, B and C. Given all distances between them(AB, AC and BC) , the angle of B between A and C, and coordinates of point A and B, how can I find coordinate of C.
Below is my current code. It is working as expected in example case.
interface Point {
x: number;
y: number;
}
function calculatePointC(A: Point, B: Point, AB: number, angleABC: number, AC: number, BC: number): Point {
// Convert angle from degrees to radians
const angleRad = angleABC * (Math.PI / 180);
// Calculate the vector from A to B
const ABVector = { x: B.x - A.x, y: B.y - A.y };
// Normalize the AB vector
const ABLength = Math.sqrt(ABVector.x * ABVector.x + ABVector.y * ABVector.y);
const normalizedAB = { x: ABVector.x / ABLength, y: ABVector.y / ABLength };
// Calculate the coordinates of point C relative to point B
const Cx_relB = AC * Math.cos(angleRad);
const Cy_relB = AC * Math.sin(angleRad);
// Convert relative coordinates to absolute coordinates
const Cx = B.x + normalizedAB.x * Cx_relB - normalizedAB.y * BC;
const Cy = B.y + normalizedAB.y * Cx_relB + normalizedAB.x * BC;
return { x: Cx, y: Cy };
}
const pointA: Point = { x: 0, y: 0 };
const pointB: Point = { x: 4, y: 0 };
const distanceAB = 4;
const angleABC = 90; // in degrees
const distanceAC = 5;
const distanceBC = 3;
const pointC = calculatePointC(pointA, pointB, distanceAB, angleABC, distanceAC, distanceBC);
console.log("Coordinates of point C:", pointC);
But in the below case, the result is wrong.
const pointA: Point = { x: 0, y: 0 };
const pointB: Point = { x: 4, y: 0 };
const distanceAB = 4;
const angleABC = 36.87; // in degrees
const distanceAC = 3;
const distanceBC = 5;
the result should be { x: 0, y: 3 } but I am getting {x: 6.399996784445525,y: 5}.