필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Beibit Sautbek
Beibit Sautbek 2016년 7월 21일
답변: Azzi Abdelmalek 2016년 7월 21일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 21일
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)));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by