« Earlier2 items total Later »

Draw a circle

I track visitors to my site using google analytics and I noticed I had 8 visitors to my site last week looking for how to draw a circle. However I didn't have any circle drawing snippets. So I have borrowed some code from the Mathworks file exchange.

This code is courtesy of Zhenhai Wang.

function H=circle(center,radius,NOP,style)
%-----------------------------------------------------
% H=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 <zhenhai@ieee.org>
%   Version 1.00
%   December, 2002
%-----------------------------------------------------

if (nargin <3),
 error('Please see help for INPUT DATA.');
elseif (nargin==3)
    style='b-';
end;
THETA=linspace(0,2*pi,NOP);
RHO=ones(1,NOP)*radius;
[X,Y] = pol2cart(THETA,RHO);
X=X+center(1);
Y=Y+center(2);
H=plot(X,Y,style);
axis square;

Draw a circle

The code contains two features. One is the use of the 'rectangle' function to draw a circle The other is the use of complex variables to represent coordinates in a 2D space like an image, which I have found quite useful.

zcenter = complex(256,139); % x + iy

radius = 20;

zc = zcenter - complex(radius,radius) % corner position

rh = rectangle('Position', [real(zc) imag(zc) 2*radius 2*radius],'Curvature', [1 1], ...
               'linewidth',1.5,'edgecolor','g');

« Earlier2 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.