how to use plot to draw a minor arc in matlab without calculating the angle range?

8 ビュー (過去 30 日間)
marvellous
marvellous 2023 年 11 月 15 日
コメント済み: Bruno Luong 2023 年 11 月 17 日
In a two-dimensional surface, for a circle C, if I know its center coordinates, the radius, and two point coordinates on C, then the minor arc between the two points is uniquely determined. How can I draw it using plot or other drawing commands in matlab without calculating the angle range? Or is there any easy way to calculate the angle range?

採用された回答

Voss
Voss 2023 年 11 月 15 日
c = [-1,2]; % center
r = 3; % radius
p = [-1,5; -3.121,-0.121]; % two points on the circle: (-1,5) and (-3.121,-0.121)
th = atan2(p(:,2)-c(2),p(:,1)-c(1)); % angles from center c to points p
if abs(diff(th)) > pi % enforce getting the minor arc:
[~,idx] = min(th); % if the angles are more than pi apart,
th(idx) = th(idx)+2*pi; % increment the smaller one by 2*pi
end
% calculate points along the arc
th = linspace(th(1),th(2),100);
x = c(1)+r*cos(th);
y = c(2)+r*sin(th);
% plot
plot(x,y)
axis equal padded

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2023 年 11 月 15 日
編集済み: Bruno Luong 2023 年 11 月 16 日
% Generate A, B, C, C is the center and A B are two points on circle
r = rand();
C = randn(2,1);
phi1 = 2*pi*rand;
A = C + r * [cos(phi1); sin(phi1)];
phi2 = 2*pi*rand;
B = C + r * [cos(phi2); sin(phi2)];
% Compute the minor arc that link A and B
% never call trig function (or even sqrt) to compute angle
CA=A-C;
CB=B-C;
N=[CA(2);-CA(1)];
x = [CA+CB,N]\CB;
tmax=x(2);
n=max(ceil(abs(tmax)/deg2rad(1)),5);
t=linspace(0,tmax,n);
t2 = t.*t;
P=C + ((1-t2).*CA+t.*(2*N))./(1+t2); % points on arc
% Plot the arc
close all
hold on
plot(C(1),C(2),'+b','Markersize', 10);
text(A(1),A(2),'A')
text(B(1),B(2),'B')
text(C(1),C(2),'C')
plot(P(1,:),P(2,:),'r','linewidth',1)
axis equal
  3 件のコメント
Bruno Luong
Bruno Luong 2023 年 11 月 16 日
編集済み: Bruno Luong 2023 年 11 月 16 日
Instead of
n=max(ceil(abs(tmax)/deg2rad(1)),5);
t=linspace(0,tmax,n);
(which can be big when A and B almost opposite, since tmax goes to infinity) a more carefully discretization of the arc is
resdeg = 2; % approx resolution of the arc, in degree
phimax = pi-2/(abs(tmax)+2/pi);
n = max(ceil(phimax/deg2rad(resdeg)),3);
phi = linspace(0,phimax,n);
t = sign(tmax)*2*(1./(pi-phi)-1/pi);
Bruno Luong
Bruno Luong 2023 年 11 月 17 日
I pack the method in a function GetPointOnArc.m
% Generate A, B, C
r = rand();
m = 3; % dimension
C = randn(m,1);
A = randn(m,1); A=C+A*r/norm(A);
B = randn(m,1); B=C+B*r/norm(B);
P = GetPointOnArc(A, B, C);
% Plot the arc
close all
hold on
if m == 2
plot(C(1),C(2),'+b','Markersize', 10);
text(A(1),A(2),'A')
text(B(1),B(2),'B')
text(C(1),C(2),'C')
plot(P(1,:),P(2,:),'.r-','linewidth',1)
axis equal
else
plot3(C(1),C(2),C(3),'+b','Markersize', 10);
text(A(1),A(2),A(3),'A')
text(B(1),B(2),B(3),'B')
text(C(1),C(2),C(3),'C')
plot3(P(1,:),P(2,:),P(3,:),'.r-','linewidth',1)
axis equal
view(3)
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by