File Exchange Pick of the Week

Drawing a Circle 14

Posted by Brett Shoelson,

You may have noticed some recent changes in the format of this blog. Here’s what to expect on a regular basis – two topics per week.

  • On Tuesdays Doug will provide MATLAB tutorials.
  • On Fridays guest bloggers Jiro, Brett and Bob will highlight File Exchange submissions.

In last week's Pick of the Week, I talked about fitting a circle to a collection of points. With a small example, I showed how Izhak Bucher's CircFit makes short work of that task. But to visualize the circle I fit through my faked data, I used MATLAB's rectangle command! If that seems odd to you, then this week's pick is for you: Zhenhai Wang's Draw a Circle submission lets you visualize your circle with (to me) a much more natural syntax, specifying the coordinates of the center, the radius of the circle, and the number of points to plot. Optionally, the file also lets you specify the style parameters for the plotted curve.

For instance:

R = 10;
Center = [5,8];
circle(Center,R,1000,'b-');
hold on
plot(Center(1),Center(2),'g.')

Okay, so you may be thinking that plotting a circle with this syntax isn't the most difficult thing you've ever tried to do. Still, nearly 24,000 people have downloaded Zhenhai's file! This suggests that it might not be trivially easy, either. Or at least, that it's awfully convenient to be just be the beneficiary of someone else's work. And "Draw a Circle" is pretty well vetted, too; it has an average of 4.29 (out of 5) stars, from 42 reviews. That means a lot of people are happy with Zhenhai's effort. In fact, years after its submission, "Draw a Circle" continues to get hundreds of downloads every month (661 in the past 30 days). Moreover, two other files cite this week's selection as an inspiration. Nice work, Zhenhai!

Got a favorite plotting routine that makes your life easier? Let us know about it!


Get the MATLAB code

Published with MATLAB® 7.6

14 CommentsOldest to Newest

if only this dear author had replaced the PLOT with a LINE command – or – at least used the possibilities of HOLD-ing…
us

Complex coordinates are easier: Eg.
R = 10;
Center = [5,8];
plot(R/2*exp(i*linspace(0,2*pi,1000))+Center*[1 i].’,’b-‘)
axis square

hi Doug,

Why not have a look at “imPixelLine and imCircle” in FileExchange…

Do the same job, with more functionalities.

Sometimes we may focus on a submission because it is beautifully written…other times, simply because it does something useful, and does it well. Zhenhai’s “Circle” certainly fits that latter bill.

Urs, why would you necessarily think that HOLD-ing should be incorporated in the CIRCLE command? Many useful functions use PLOT commands–and in all cases it is up to the user to issue (or not, –depending on his or her desired behavior) the HOLD command.

Terry, I’m not sure there’s anything inherently easier about using complex coordinates than the very compact pol2cart that this function uses.

Sunpeng, I haven’t really played with your code, and it certainly appears that you’ve added some nice functionality. But I will note that your file requires the Image Processing Toolbox. I could argue that generic functionality like plotting a circle should be independent of that (or any) Toolbox.

Does nobody knows the builtin MATLAB command

rectangle(‘Position’,[x,y,dx,dy],’Curvature’,[1,1])

which allows for all the builtin MATLAB options including hold? The main advantage of which is to do not use any discretization.

Ralf, of course we know (and love) the RECTANGLE command. (Follow the link in this post to “last week’s Pick of the Week.”) But options are always nice.

Hello there

I need a matlab function to paint and store in an array all the poits INSIDE the circle.

Please, anybody could help me?

Thanks in advance

Jose,
You might try representing your circle(s) as polygons, then using the MATLAB function INPOLYGON. Just a thought.
Cheers,
Brett

This allows all the functionality of the rectangle command (linewidth, colors, etc.):

function circle (x,y,radius,varargin)

rectangle('position',[x-radius, y-radius, 2*radius, 2*radius],'curvature',[1 1],varargin{:});

@Maarten,
Yep, _rectangle_ will do it. (You might want to re-read the first few sentences of my post above.) Sometimes, though, it is nice to be able to position circles parametrically, or with different syntax–one that gives you pointwise control over the plot, for instance.

% circle
theta=linspace(0,2*pi,100);
x=cos(theta);
y=sin(theta);
r=input(‘enter the radius of the circle’);

plot(x,y,0,0,’+’);
axis(‘equal’);
title(‘circle’);

I want by using kp_harislpaplace.m exteracting any intersted points and they should be centers of any circle
with a radius that depended to sigma parameters .

LFR is the name of local featuers regoin on this paper A Feature based robust digital image watermarking against geometric attacks by Xiang-yang Wang
“Based on scale space theory and an image normalization technique, a new feature-based image watermarking scheme robust to general
geometric attacks is proposed in this paper. First, the Harris–Laplace detector is utilized to extract steady feature points from the
host image; then, the local feature regions (LFR) are ascertained adaptively according to the characteristic scale theory, and they are
normalized by an image normalization technique; finally, according to the predistortion compensation theory, several copies of the digital
watermark are embedded into the nonoverlapped normalized LFR by comparing the DFT mid-frequency magnitudes.

These postings are the author's and don't necessarily represent the opinions of MathWorks.