Draw a circle with a certain number of pixels.

I am working on a project that requires me to precisely control the number of pixels that are used to draw (roughly) circular stimuli, and although Breshenem's algorithms are large, they do not draw circles of an arbitrary area (to my knowledge). I tried scripts that interrupt the Breshenem algorithm when the desired area was built, but the results are clearly hit or miss. Does anyone know a way to build a “better” circle (somewhat subjective, I know) using a given number of pixels? Many thanks!

+5
source share
6 answers

A rough way to do this, for example:

The radius of the circle is 1000 square meters. inches - sqrt (1000 / pi) = 17.8 ... This circle should then fit into the 35x35 matrix. If you make “indexes” for this matrix, where the center pixel is (0,0), you can easily check if the pixel falls into the circle or not by substituting x ^ 2 + y ^ 2 = r ^ 2 in the equation of the circle. Or you can use an alternative equation for a circle centered at (a, b). If it evaluates to TRUE, it does, if not, outside the circle.

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

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 (actual circle generated 99988.0): circle

, , , , .

+5

A = Pi * r 2. (-) , Pi, : r 2= A/pi. , : r=sqrt(A/pi). , .

+1

( ) , , , , .

+1

- . , , , , . , , , . , , . - , .

3d- . , 255. IIRC, r * r = 15, 240 . , 255 .

0

2000 , . , . 2Pi * R = , , , . R = 2000/2 * Pi, . , 2000 . , , .

0

/.

,

= Pi * ^ 2

,

= Pi * (/2) ^ 2

,

, (.. ) = *

,

= / = (Pi * (/2) ^ 2)/( * ) = Pi/4

, , , . , (, 10 000 , , ). :

= ( ) * ( )

, .

- . , , ( ) . d. , , :

= (d * d) * (Pi/4)

, d

d = Sqrt (4 * ( )/Pi)

, , d - . , . , , :

= Sqrt (4 * ( )/Pi)

Now, obviously, you need to do some rounding options, etc. (there is no such thing as a fractional pixel), but you get the point. In addition, this formula is more accurate, as the desired number of pixels for the area of ​​the circle increases. For small numbers of pixels, a rounding error may not give you the exact number of pixels.

0
source

All Articles