How to plot a circle centered on a line and passing through a point?
조회 수: 35(최근 30일)
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!
Roger Stafford 2018년 3월 12일
편집: Roger Stafford 2018년 3월 12일
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.)