Filter löschen
Filter löschen

How to find the distance between one slope and several points?

1 Ansicht (letzte 30 Tage)
I have slope AB, where
A=[x1,y1], B=[x2,y2], and point C=[x0,y0].
In order to find the distance between the point C and slope AB, I have used the formula below:
d=(abs(((x2-x1)*(y1-y0))-((x1-x0)*(y2-y1)))/(sqrt((x2-x1)^2+(y2-y1)^2)));
I can find the distance between slope and one point, but If I have several points, I have a problem in result.
For example:
I have a matrix A as below, where the first row represents A=[x1,y1], and the last row represents B=[x2,y2]:
A =
2 2
3 1
5 4
7 8
10 2
In matrix A, rows 2,3,4 represent 3 different points.
So I need to find a distance between slope and 3 points.
Actually, I did this code, but the result shows 4 values instead of 3 values.
l=length(A);
x1=A(1,1); y1=A(1,2);
x2=A(l,1); y2=A(l,2);
for i=2:l-1
x(i)=A(i,1);
y(i)=A(i,2);
d(i)=(abs(((x2-x1)*(y1-y(i))-((x1-x(i))*(y2-y1)))/(sqrt((x2-x1)^2+(y2-y1)^2))));
end
Could anyone check my code or help to get necessary answer?

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Jul. 2016
x0=A(2:4,1)
y0=A(2:4,2)
d=(abs(((x2-x1).*(y1-y0))-((x1-x0).*(y2-y1)))./(sqrt((x2-x1)^2+(y2-y1)^2)));

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by