Find the third set of coordaintes (x3 y3) of right angle triangle - Matlab

Viewed 132

How can I find the third set of coordinates (X3 Y3) of right angle triangle based on the distances between all coordinates and the other two set of coordinates (X1 Y1; X2 Y2)

Triangle example scheme

Code:

clc;
clear all;
close all;
axis on;
 
%Example of initial parameters
X1 = 0
Y1 = 2
X2 = 6
Y2 = 7
DX2Y2X3Y3 = 10 

%Draw line
line([X1, X2], [Y1, Y2])
text(X1,Y1-0.05,strcat(string(X1),",",string(Y1)))
text(X2,Y2-0.05,strcat(string(X2),",",string(Y2)))

%Calculations
DX1Y1X2Y2 = sqrt((X2-X1)^2+(Y2-Y1)^2) %distance x1,y1 x2,y2
DX1Y1X3Y3 =  sqrt(DX2Y2X3Y3+DX1Y1X2Y2)%distance x1,y1 x3,y3 based on Pythagoras 
1 Answers

So assuming you have

  1. P1 = [X1 Y1]
  2. P2 = [X2 Y2]
  3. Assertion that the angle at P2 is 90
  4. Length DX2Y2X3Y3

Then we can do the following:

vP1P2 = [X2; Y2] - [X1; Y1];           % Direction vector
vP2P3 = [1; -1] .* flipud(vP1P2);      % Negative inverse for rightangle
vP2P3 = vP2P3 ./ sqrt(vP2P3(1)^2 + vP2P3(2)^2); % Scale to unit length
P3 = [X2; Y2] - DX2Y2X3Y3 .* vP2P3;    % Calc P3 from P2 + vP2P3
X3 = P3(1); Y3 = P3(2);                % Break down into coordinates 

line([X2, X3], [Y2, Y3])
line([X1, X3], [Y1, Y3])

axis equal % to make the plot clearer

Note that there are two valid solutions to this problem, reflected in the line from P1 to P2. The choice of direction (+ve / -ve) for vP2P3 will dictate which solution you get.

This code gives the following output:

triangle

Related