File Exchange

## Draw a circle in a matrix / image

version 1.0.0.0 (824 Bytes) by Peter Bone

### Peter Bone (view profile)

Draw a circle in a 2D matrix using the integer midpoint circle algorithm

Updated 31 Mar 2016

This is a good example of the integer midpoint circle algorithm. No pixels are missed or repeated.

### Cite As

Peter Bone (2019). Draw a circle in a matrix / image (https://www.mathworks.com/matlabcentral/fileexchange/14331-draw-a-circle-in-a-matrix-image), MATLAB Central File Exchange. Retrieved .

ruijin cang

### ruijin cang (view profile)

how could i define 'i'? Thanks

Surya

David Kelly

### David Kelly (view profile)

Add the following at the bottom of the MidPoint Circle function to created filled circles

tempR = sqrt((double(ii) - double(xc)).^2 + (double(jj) - double(yc)).^2);
i(ii,jj)=value;
end
end
end

Chean Lee

### Chean Lee (view profile)

This code is extremely useful.
I'm not particularly good in using matlab but i had no trouble understanding how to use it (although it took me a good FIVE minutes).

So Five stars for sure for doing EXACTLY what it says on the box, nothing more and nothing less. You guys should be ashamed for rating less then 5 stars.

For others, to get rid of the gaps I used

FilteredResult=medfilt2(Result, [3 3])

This is how I used this code
(you will now notice i'm no good at matlab)

GGG(1:355,1:355)=0;
xxx(1:355,1:355)=0;
y=rand(1,150);
y=y*50

jj=1;
for j=1:150;
AAA=MidpointCircle(xxx,j,177,177,y(1,jj));
GGG=AAA+GGG;
jj=jj+1;
end;

GGG=medfilt2(GGG, [3 3])
image (GGG)

Ibtissem Labed

### Ibtissem Labed (view profile)

i

William Lau Kai Wei

### William Lau Kai Wei (view profile)

this is a coding to draw a circle.
i using the same code but i looping it with reduce the radius.

let say, my radius is 7, I try looping it by draw radius with 7, and then 6, and then 5 and so on until it become 0. It really fill in a lot of space but the problem is still got some space is empty.

My problem now is I want to fill in all the area inside the circle. So, any idea to solve this problem? Thank you

chitra s

### chitra s (view profile)

what value i have to give for the parameter 'value'?

Nor Muzakkir

### Nor Muzakkir (view profile)

got it working.

i = the pixel value for coordinate (1,1).
xc and yc = center coordinate of the circle.
value = the pixel value for the circle.

extra note: the value for xc and yc must be larger than radius.

thanks author, i've been searching for this kind of function.

Matthew

### Matthew (view profile)

Can someone fill me in as to how this code works. Also, can it be altered to make an annulus as opposed to just a circle?

Andrew Harward

It is simple to modify this function to allow circles of both odd & even size (currently the algorithm cannot produce even sized e.g. 8x8 circles). Maybe I'll post up a modified version if that's OK with you?

John Doe

Excellent, clean implementation. This is the first of over a dozen submissions I've seen that actually uses this algorithm (and they all should). Very nice. Commenting your code would be a nice touch, however...

Sheila Geva

Hi,
I need to draw circles in each cell in an intensity matrix that I'm printing. How do I use your code to do so?
Also, I don't understand the parameters that need to be put in the function. Please explain?

When I ran the code, there were some skipped pixels in the drawn circle.

Tom Canty

sorry sorry

i hope if one till me how to use it, can one tell us i, value stand for what....

Mustafa Sami

Huan Tan

agreed! every little tool like this can find its place. very helpful, just what i was looking for

John Jorsett

Just what I was looking for and not "boring" at all.

Urs (us) Schwarz

the fact that the reviewer below finds the submission boring is not substantial and certainly does not warrant a poor rating by itself. please, be more considerate.
to the author: the function could be spruced up a bit, eg, an x/y-offset option
us

dave martin

Boring.