How to draw an Elliptical Arc by given radius (rx and ry) and start/end points ?

조회 수: 26 (최근 30일)
Hello Everybody
I tried to draw an Elliptical Arc on a graph by giving the radius (rx and ry) and start/end points.
-- start point [11,56], and end point [-11, -18], and Center of arc [11, 18]. --
But the arc I made is away from the start/end points. not meet the points. (this below code is only valid for circular arc)
(I used the code from "matlabcentral/answers/367126-plot-an-arc-on-a-2d-grid-by-given-radius-and-end-points")
Please let me know how to make the Elliptical Arc on a graph by giving the radius (rx and ry) and start/end points.
A = [11, 56]; % start point [x,y] of Arc
B = [-11, -18]; % end point [x,y] of Arc
C = [11, 18]; % x, y-radius of arc [rx,ry]
d = norm(B-A);
r = d/2; % Choose R radius = d/2
a = atan2(A(2)-C(2),A(1)-C(1));
b = atan2(B(2)-C(2),B(1)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,50);
x = C(1)+r*cos(t);
y = C(2)+r*sin(t);
plot(x,y,'k-',A(1),A(2),'k*',B(1),B(2),'k*')
axis equal
  댓글 수: 3
Smithy
Smithy 2022년 12월 15일
Yes, you are right. I need the Elliptical Arc to meet the start/end points. Could you let me know how to modity the code?
Sam Chak
Sam Chak 2022년 12월 15일
편집: Sam Chak 2022년 12월 15일
@Smithy, are you looking for the Parametric Equation of an Elliptic Arc?
If you have the math, then you can replace Parametric Equation of a Circle below:
x = C(1) + r*cos(t);
y = C(2) + r*sin(t);
with
x = C(1) + a*cos(t);
y = C(2) + b*sin(t);
where a is the length of the semi-major axis and b is the length of the semi-minor axis of an ellipse.

댓글을 달려면 로그인하십시오.

채택된 답변

Fabio Freschi
Fabio Freschi 2022년 12월 15일
편집: Fabio Freschi 2022년 12월 15일
I am a little lazy to do the math, so I have found this reference for the two parameters a and b that describe an ellipse.
As alternative you can try to solve a nonlinear system of equations using fsolve to calculate a, b, alpha1, alpha2 (I did it: same results, of course)
Another correction to the code is the addition of the coefficient a/b for the calculation of the minimum and maximum angles of the ellipse arc (check the calculation of alpha1 and alpha2).
clear variables, close all
% data
A = [11, 56]; % start point [x,y] of Arc
B = [-11, -18]; % end point [x,y] of Arc
C = [11, 18]; % Center of arc [x,y]
% params according to https://www.geeksforgeeks.org/how-to-find-the-equation-of-an-ellipse-given-the-center-and-two-points/
p = A(1);
q = A(2);
m = B(1);
n = B(2);
h = C(1);
k = C(2);
% ellipse equation params
b = sqrt((p-h)^2*(n-q)*(n-q-2*k)/((p-m)*(p+m-2*h))+(q-k)^2);
a = b*sqrt((p-m)*(p+m-2*h)/((n-q)*(n+q-2*k)));
% start and end angles
alpha1 = atan2(a/b*(q-k),(p-h));
alpha2 = atan2(a/b*(n-k),(m-h));
alpha2 = mod(alpha2-alpha1,2*pi)+alpha1; % Ensure that arc moves counterclockwise
% elliptic arc
alpha = linspace(alpha1,alpha2,50);
x = h+a*cos(alpha);
y = k+b*sin(alpha);
% plot
figure, hold on, grid on, axis equal
plot(x,y)
plot(A(1),A(2),'k*')
plot(B(1),B(2),'b*')
plot(C(1),C(2), 'ro')
  댓글 수: 1
Smithy
Smithy 2022년 12월 19일
Wow, wonderful. Thank you very much, It works really well. I really appreciate with it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by