File Exchange

image thumbnail

Draw a circle in a matrix / image

version (824 Bytes) by Peter Bone
Draw a circle in a 2D matrix using the integer midpoint circle algorithm


Updated 31 Mar 2016

View License

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 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

ruijin cang

how could i define 'i'? Thanks


David Kelly

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

for ii = xc-int16(radius):xc+(int16(radius))
for jj = yc-int16(radius):yc+(int16(radius))
tempR = sqrt((double(ii) - double(xc)).^2 + (double(jj) - double(yc)).^2);
if(tempR <= double(int16(radius)))

Chean Lee

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)


for j=1:150;

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

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

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

got it working.

i = the pixel value for coordinate (1,1).
radius = radius of the circle.
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.


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

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?

Ellen Blinka

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

Tom Canty

Comments in your code isnt just for coding class, it helps others understand your code

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

dave martin



no updates. Resubnmitting for license

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux