linear interpolation in 3D space
5 ビュー (過去 30 日間)
古いコメントを表示
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
2021 年 8 月 21 日
編集済み: Wan Ji
2021 年 8 月 22 日
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 件のコメント
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Time Series についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!