簡體   English   中英

如何從平面中的 3 個點(x,y,z)創建弧路徑?

[英]how to create an arc path from 3 points(x, y, z) in plane?

我想創建一個跨越 n=3 點 P(n)=(x, y, z) 的弧形軌跡,我決定在平面上的 3 個點上畫一個圓。 所以我有中心、半徑、theta(x、y 平面中的角度)和 phi(圍繞 z 軸的角度),並且我知道 3 個點(x、y、z)的位置,如何在 p1 之間提取弧線,這個圈子的p2和p3? 我在 MATLAB 中實現了這個程序。非常感謝。

這個關於 math.stackexchange 的答案給出了一個很好的簡單公式來找到圓心(因此是半徑)

給定圓上三個點的圓心的 3D 坐標。 (@Sergio G.)

從這個其他有用的 math.stackexchange 答案中,我們可以根據中心和原始 3 中的兩個(非共線)點來定義該圓上的任何點。

給定中心和圓上兩個點的3D圓的參數方程? (@米爾布蘭特)

最后,我們需要您的 3 個點的 3 個角度來定義弧,這可以使用atan2和在其他步驟中創建的分量向量來完成。

完整的注釋代碼如下,它產生了這個圖,並計算任何 3D 點的圓角,然后計算任何角度的圓周上的值。

圓形圖 3D

% 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM