MATLAB Answers

How to plot a circle centered on a line and passing through a point?

조회 수: 22(최근 30일)
Emma Williston
Emma Williston 11 Mar 2018
Edited: Roger Stafford 12 Mar 2018
Hello there. I have x-values and y-values in arrays called x and y. I also have a line that goes through points (a1,a2) and (b1,b2). I need to create a circle that is centered on the line and that passes through the point (x(n),y(n)), where n is an arbitrary index variable. I am not incredibly familiar with Matlab and am not sure how to get this done. I've found ways to get the distance between the point and the line, but not how to either find the point on the line closest to my given point, or draw a circle centered on a line and passing through a point. Please help!

  댓글 수: 0

로그인 to comment.

답변 수 (1)

Roger Stafford
Roger Stafford 12 Mar 2018
Roger Stafford 님이 편집함. 12 Mar 2018
There are infinitely many ways a circle can be centered on a line and pass through a given point, except for the case when a line segment from the center to the given point is required to be orthogonal to that line. In that case the circle would be unique. Therefore I will suppose that is what you actually require. Let the line run through points Pa = [a1;a2] and Pb = [b1;b2] and let the circle have its center on that line and pass through the point Q = [x;y], along with the above orthogonality requirement.
t = dot(Pb-Pa,Pb-Q)/dot(Pb-Pa,Pb-Pa);
C = t*Pa+(1-t)*Pb; % Center at orthogonal projection of Q onto line
R = norm(Q-C); % Circle's radius
p = linspace(0,2*pi,1000); % Parameter for generating circular plot
plot(C(1)+R*cos(p),C(2)+R*sin(p),'b-') % Plot the circle
(Of course you will also want the line to appear on the plot. I leave that to you.)
(Also I leave to you the need to do all the above for several points in an array and a like number of circles instead of a single Q, for which you will presumably need an appropriate for-loop.)

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

제품


Translated by