Skip to content

Instantly share code, notes, and snippets.

@jcchurch
Created June 30, 2012 20:56
Show Gist options
  • Save jcchurch/3025482 to your computer and use it in GitHub Desktop.
Save jcchurch/3025482 to your computer and use it in GitHub Desktop.
Drawing Circles in Matlab
function drawCircles(center,radius,NOP,style)
%------------------------------------------------------------------------------
% CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%
% Usage Examples,
%
% circle([1,3],3,1000,':');
% circle([2,4],2,1000,'--');
%
% Zhenhai Wang <[email protected]>
% Version 1.00
% December, 2002
%
% Modified by James Church
%------------------------------------------------------------------------------
if size(center, 1) ~= size(radius, 1)
error('Length of center and radius should be equal.')
end
if nargin == 2
NOP = 1000;
end
if nargin < 2
error('Please see help for INPUT DATA.');
elseif (nargin < 3)
style='b-';
end;
n = length(radius);
THETA=linspace(0,2*pi,NOP);
hold on;
for i = 1:n
RHO=ones(1,NOP)*radius(i);
[X,Y] = pol2cart(THETA,RHO);
X=X+center(i, 1);
Y=Y+center(i, 2);
plot(X,Y,style);
end
end
@equaeghe
Copy link

You can draw circles using the rectangle function; there is no need for plotting:

https://mathworks.com/help/matlab/ref/rectangle.html#busqhee-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment