Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a circle with a specific number of pixels

I'm working on a project that requires me to accurately control the number of pixels that are used to draw (roughly) circular stimuli, and although Bresenham's algorithms are great, they don't draw circles of an arbitrary area (to my knowledge). I've tried scripts that interrupt Bresenham's algorithm when the desired area has been plotted, but the results are decidedly hit-or-miss. Does anyone know of a way to plot the "best" circle (somewhat subjective, I know) using a given number of pixels? Many thanks!

like image 259
dewarrn1 Avatar asked Apr 21 '11 18:04

dewarrn1


People also ask

How do you make a circle in C++?

Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

How do you find the area of a circle in a picture?

C = pi * d calculates the circumference (distance around the outside of the circle). D in the formula refers to the diameter which is the width of the circle. The formula for the area of a circle is A = pi * r * r where r is the radius (diameter / 2).


3 Answers

A rough way of doing it, for example:

The radius of a circle of area 1000 sq px is sqrt(1000/pi) = 17.8... That circle should then fit in a 35x35 matrix. If you make "indices" for that matrix where the central pixel is (0,0), you can check easily if the pixel falls in the circle or not by substitution into the equation of a circle x^2 + y^2 = r^2. Or you can use the alternative equation for a circle centered at (a,b). If it evaluates to TRUE, it does, if not, it's outside the circle.

As a pseudocode/example, in Python I would do an optimized version of:

import numpy, math

target_area = 1000.0

r = (target_area / math.pi) ** 0.5
m = numpy.zeros((2*r+2,2*r+2))

a, b = r, r

for row in range(0, m.shape[0]):
    for col in range(0, m.shape[1]):
        if (col-a)**2 + (row-b)**2 <= r**2:
            m[row,col] = 1

numpy.sum(m)
#>>> 999

Here is the result when the target area is 100,000 pixels (the actual circle generated is 99988.0): circle

You could also write a routine to find which areas can be matched more closely than others with this algorithm, and select those values to ensure conformity.

like image 57
Benjamin Avatar answered Oct 11 '22 21:10

Benjamin


The area of a circle is A=Pi*r2. You're starting from the area and (apparently) want the radius, so we divide both sides by Pi to get: r2=A/pi. Taking the square root of both sides then gives us: r=sqrt(A/pi). Once you have the radius, drawing with most of the normal algorithms should be straightforward.

like image 44
Jerry Coffin Avatar answered Oct 11 '22 23:10

Jerry Coffin


A simple (but somewhat naive approach) would be to simply count the number of pixels drawn by Bresenham's algorithm for a given radius, and then use binary search to find the radius that produces the desired number of pixels.

like image 1
hammar Avatar answered Oct 11 '22 21:10

hammar