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

8 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Grid 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