Draw a line between two points

Hi. I have three points [5.2 0 0 0 9.9 0 0 12.1]. How to draw a line through the first two points and find is the third point (12.1) above/below this line.

댓글 수: 5

Evan
Evan 2013년 7월 17일
How are your points arranged in the vector you provide? Is this a 3d line? Are they arranged [x1 y1 z1 x2 y2 z2...]?
Artyom
Artyom 2013년 7월 17일
편집: Artyom 2013년 7월 17일
They are arranged like this:
[1 5.2;
2 0;
3 0;
4 0;
5 9.9;
... ]
Evan
Evan 2013년 7월 17일
편집: Evan 2013년 7월 17일
Okay. You say you have three points, but your array seems to contain eight. Are you saying you only want to consider points where your y value is not zero? That is, you would plot a line through (1,5.2) and (5,9.9) then see if (9,12.1) is above or below the line?
I'll go ahead and submit a solution under this assumption. If I'm mistaken, just let me know any I'll modify it as needed.
Actually I have an array like this:
[1 5.2
2 4.3
3 5.1
4 7.8
5 9.9
...];
I just want to show that we take only 2 points (1 and 5) and draw a line through them.
Evan
Evan 2013년 7월 17일
편집: Evan 2013년 7월 17일
Ah, okay. I suppose you've been successful in modifying my below example code to be more in line with your needs?

댓글을 달려면 로그인하십시오.

 채택된 답변

Evan
Evan 2013년 7월 17일
편집: Evan 2013년 7월 17일

0 개 추천

P = [1 5.2; 2 0; 3 0; 4 0; 5 9.9; 6 0; 7 0; 8 12.1];
posP = find(P(:,2)); %find where y values aren't zero.
C = polyfit(P(posP(1:2),1),P(posP(1:2),2),1); %fit line to first and second nonzero
Y = polyval(C,P(posP(3),1)); %find value on line at x-value of third nonzero
if Y == P(posP(3),2)
disp('The third point falls on the line')
elseif Y > P(posP(3),1)
disp('The third point falls above the line')
elseif Y < P(posP(3),1)
disp('The third point falls below the line')
end

추가 답변 (3개)

Iain
Iain 2013년 7월 17일

0 개 추천

This is the raw maths...
point1 = [x1 y1];
point2 = [x2 y2];
point3 = [x3 y3];
m = (y2 - y1) / (x2 - x1);
c = y2 - m*x2;
y3_est = m*x3 + c;
if y3_est > y3
disp('point below line')
elseif y3_est == y3
disp('colinear')
else
disp('point above line')
end
Kiran Sagar
Kiran Sagar 2013년 7월 17일

0 개 추천

Frame an equation between two of the points of the form y=mx+c. then replace x with the x-cordinate of the point to be checked, compare the resultant y with the point's y cordinate. that's it. supposing your two points forming the line are the first and last rows of the matrix A, and the test point as one of the in between points, here is a probable code:
A = [1 5.2;
2 0;
3 0;
4 0;
5 9.9;];
m = (A(5,2)-A(1,2))/(A(5,1)-A(1,1));
c = A(1,2)-m*A(1,1);
y_test = m*A(2,1)+c;
if y_test<A(2,2)
%%%%%your statements %%%%%
else
%%%%%your statements%%%
end
Artyom
Artyom 2013년 7월 17일

0 개 추천

Thank you guys.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

질문:

2013년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by