我想創建一個跨越 n=3 點 P(n)=(x, y, z) 的弧形軌跡,我決定在平面上的 3 個點上畫一個圓。所以我有中心、半徑、theta(x、y 平面中的角度)和 phi(圍繞 z 軸的角度),并且我知道 3 個點(x、y、z)的位置,如何在 p1 之間提取弧線,這個圈子的p2和p3?我在 MATLAB 中實作了這個程式。非常感謝。
uj5u.com熱心網友回復:
這個關于 math.stackexchange 的答案給出了一個很好的簡單公式來找到圓心(因此是半徑)

% Original points
p1 = [1;0;2];
p2 = [0;0;0];
p3 = [1;2;2];
P = [p1,p2,p3];
% Get circle definition and 3D planar normal to it
p0 = getCentre(p1,p2,p3);
r = norm( p0 - p1 );
[n0,idx] = getNormal(p0,p1,p2,p3);
% Vectors to describe the plane
q1 = P(:,idx(1));
q2 = p0 cross(n0,(p1-p0).').';
% Function to compute circle point at given angle
fc = @(a) p0 cos(a).*(q1-p0) sin(a).*(q2-p0);
% Get angles of the original points for the circle formula
a1 = angleFromPoint(p0,p1,q1,q2);
a2 = angleFromPoint(p0,p2,q1,q2);
a3 = angleFromPoint(p0,p3,q1,q2);
% Plot
figure(1); clf; hold on;
args = {'markersize',20,'displayname'};
plot3( P(1,:), P(2,:), P(3,:), '.', args{:}, 'Original Points' );
plot3( p0(1), p0(2), p0(3), '.k', args{:}, 'Centre' );
plotArc(fc,a1,a2); % plot arc from p1 to p2
plotArc(fc,a2,a3); % plot arc from p2 to p3
plotArc(fc,a3,a1); % plot arc from p3 to p1
grid on; legend show; view(-50,40);
function ang = angleFromPoint(p0,p,q1,q2)
% Get the circle angle for point 'p'
comp = @(a,b) dot(a,b)/norm(b);
ang = atan2( comp(p-p0,q2-p0), comp(p-p0,q1-p0) );
end
function plotArc(fc,a,b)
% Plot circle arc between angles 'a' and 'b' for circle function 'fc'
while a > b
a = a - 2*pi; % ensure we always go from a to b
end
aa = linspace( a, b, 100 );
c = fc(aa);
plot3( c(1,:), c(2,:), c(3,:), '.r', 'markersize', 5, 'handlevisibility', 'off' );
end
function p0 = getCentre(p1,p2,p3)
% Get centre of circle defined by 3D points 'p1','p2','p3'
v1 = p2 - p1;
v2 = p3 - p1;
v11 = dot( v1.', v1 );
v22 = dot( v2.', v2 );
v12 = dot( v1.', v2 );
b = 1/(2*(v11*v22-v12^2));
k1 = b * v22 * (v11-v12);
k2 = b * v11 * (v22-v12);
p0 = p1 k1*v1 k2*v2;
end
function [n0,idx] = getNormal(p0,p1,p2,p3)
% compute all 3 normals in case two points are colinear with centre
n12 = cross((p1 - p0),(p2 - p0));
n23 = cross((p3 - p0),(p2 - p0));
n13 = cross((p3 - p0),(p1 - p0));
n = [n12,n23,n13];
n = n./sign(n(1,:));
idx = find(~all(isnan(n)),2);
n = n(:,idx(1));
n0 = n / norm(n);
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/479873.html
上一篇:在MATLAB中生成矩形脈沖
下一篇:Matlab設定輸入范圍和步長
