Hi I have a two [X,Y,Z] coordinates [0 3500 115] and [900 4000 113.5] ,which will help me to create a line. Now I have two coordinate, A= [700,2100,185] and [1500,3300,210]. so I have find the find,out of A or B, which point is near to line segment, and co-ordinates at the line segment which is closest to either A or B
Thanks

 채택된 답변

Wan Ji
Wan Ji 2021년 8월 21일
편집: Wan Ji 2021년 8월 22일

1 개 추천

Using point2segmentDistance function
function [distance, nearestPointOnSeg ]= point2segmentDistance(point, segmentPoint1, segmentPoint2)
point = point(:);
segmentPoint1 = segmentPoint1(:);
segmentPoint2 = segmentPoint2(:);
V = segmentPoint2 - segmentPoint1;
V1 = point - segmentPoint1;
V2 = point - segmentPoint2;
L1 = norm(V1);
L2 = norm(V2);
L = norm(V);
theta1 = acos(V'*V1/(L*L1));
theta2 = acos(-V'*V2/(L*L2));
if(theta1>=pi/2)
distance = L1; nearestPointOnSeg = segmentPoint1;
elseif(theta2>=pi/2)
distance = L2; nearestPointOnSeg = segmentPoint2;
else
distance = L1*sin(theta1);
nearestPointOnSeg = segmentPoint1 + V/L * L1*cos(theta1);
end
end
Then calculate in the command line
P1 = [0 3500 115];
P2 = [900 4000 113.5];
PA= [700,2100,185];
PB = [1500,3300,210];
[disA2Seg, A_nearestPseg ] = point2segmentDistance(PA, P1, P2)
[disB2Seg, B_nearestPseg ] = point2segmentDistance(PB, P1, P2)
plot3([P1(1),P2(1)], [P1(2),P2(2)],[P1(3),P2(3)],'r-o','markerfacecolor','r','linewidth', 2)
hold on
text(P1(1), P1(2), P1(3), 'P_1');
text(P2(1), P2(2), P2(3), 'P_2');
text(PA(1), PA(2), PA(3), 'P_A');
text(PB(1), PB(2), PB(3), 'P_B');
text(A_nearestPseg(1), A_nearestPseg(2), A_nearestPseg(3), '\_\_\_P(Nearest to A)');
text(B_nearestPseg(1), B_nearestPseg(2), B_nearestPseg(3)+eps, '\_\_\_P(Nearest to B)');
plot3([PA(1),A_nearestPseg(1)], [PA(2),A_nearestPseg(2)],[PA(3),A_nearestPseg(3)],...
'g-h','markerfacecolor','g','linewidth', 2)
plot3([PB(1),B_nearestPseg(1)], [PB(2),B_nearestPseg(2)],[PB(3),B_nearestPseg(3)],...
'b-s','markerfacecolor','b','linewidth', 2)
With answer
disA2Seg =
1.566812049991957e+03
A_nearestPseg =
0
3500
115
disB2Seg =
9.269909654360176e+02
B_nearestPseg =
1.0e+03 *
0.900000000000000
4.000000000000000
0.113500000000000
So point B is closer to the segment. And the nearest points on segment to Point A or B are found.

댓글 수: 7

Bijay Sangat
Bijay Sangat 2021년 8월 21일
편집: Bijay Sangat 2021년 8월 21일
Thanks for your reply and code. But how to interpolate coordiante in the the line segment (from p1 and p2) nearest to B ? I am thinking about linear interapolation but I don't know how to use ?
Wan Ji
Wan Ji 2021년 8월 21일
if you need to get the coordinate in the segment that is the nearest to point a or b, i'ill help you tomorrow
Bijay Sangat
Bijay Sangat 2021년 8월 22일
thanks :)
Wan Ji
Wan Ji 2021년 8월 22일
편집: Wan Ji 2021년 8월 22일
OK, now the coordinates on the segment that is the nearest to point a or b were found @Bijay Sangat
Bijay Sangat
Bijay Sangat 2021년 8월 22일
편집: Bijay Sangat 2021년 8월 22일
Thanks for the code, If we linearly extrapolate P1 and p2, do we get the same answer ? beacuse I think I can extrapolate lines segment as well to get the nearest point to PA or Pb
Wan Ji
Wan Ji 2021년 8월 22일
편집: Wan Ji 2021년 8월 22일
The answer is uncertain, because you elongate this segment, the nearest point on the elongated segment to PA or PB mybe go out of the original segment. You can do any test or experiment on the function i offered.
% P1 = [1, 0, 0]; % after elongation ( extrapolation)
P1 = [-1, 0, 0]; % before elongation ( extrapolation)
P2 = [4, 0, 0];
PA = [0, 1, 0];
PB = [3, 1, 0];
[disA2Seg, A_nearestPseg ] = point2segmentDistance(PA, P1, P2)
[disB2Seg, B_nearestPseg ] = point2segmentDistance(PB, P1, P2)
plot3([P1(1),P2(1)], [P1(2),P2(2)],[P1(3),P2(3)],'r-o','markerfacecolor','r','linewidth', 2)
hold on
text(P1(1), P1(2), P1(3), 'P_1');
text(P2(1), P2(2), P2(3), 'P_2');
text(PA(1), PA(2), PA(3), 'P_A');
text(PB(1), PB(2), PB(3), 'P_B');
text(A_nearestPseg(1), A_nearestPseg(2), A_nearestPseg(3), 'to A','color','c');
text(B_nearestPseg(1), B_nearestPseg(2), B_nearestPseg(3)+eps, 'to B','color','c');
view(30,67)
plot3([PA(1),A_nearestPseg(1)], [PA(2),A_nearestPseg(2)],[PA(3),A_nearestPseg(3)],...
'g-h','markerfacecolor','g','linewidth', 2)
plot3([PB(1),B_nearestPseg(1)], [PB(2),B_nearestPseg(2)],[PB(3),B_nearestPseg(3)],...
'b-s','markerfacecolor','b','linewidth', 2)
before elongation
after elongation
So you can see from the figures that the elongation (extrapolation) of segment may change the nearest point position
Bijay Sangat
Bijay Sangat 2021년 8월 22일
Thanks :)

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

추가 답변 (0개)

카테고리

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

태그

질문:

2021년 8월 21일

댓글:

2021년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by