c++ - Algorithm for finding number of squares in a given circle -


here drawing: click

i need write program, find number of squares(1x1), can draw circle of given radius.the squares can drawn , placed lego blocks- 1 on another. in cases, vertexes of squares can lie on circle.

examples: 1- makes 0, 2- gives four, 3- 16 squares, 4-32, 5-52.

i have written something, doesn't work fine 5+ (i mean- radius bigger 5). here goes: click. in code- r radius of circle, sum sum of squares , height height of triangles try "draw" circle (using pythagorean theorem).

now- help? algorithm correct? should change something?

there gauss's circle problem gives formula count integer points inside circle of given radius. may use logic count squares lie in circle.

n = 4 * sum[i=1..r] (floor(sqrt((r^2-i^2))) 

example:

r = 3 i=1   n1 = floor(sqrt(9-1))~floor(2.8)=2 i=2   n2 = floor(sqrt(9-4))~floor(2.2)=2 i=3   n2 = floor(sqrt(9-9))=0 n=4*(n1+n2+n3)=16 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -