algorithm - Fast Integer Coordinates Inside/along a Circle Centered at origin with radius r -


consider have circle centered @ 0,0 radius r.

i'd integer points available inside circle. problem easy solve.

one may iterate on square x = -r +r , y =-r +r , see if x * x + y * y <= r * r, if so, add point result.

however, what's quickest way this? feel there should type of hack can take calculations (2r)^2 4/3 r^2

more particularly, have feeling can calculate length of inscribed square, add outer remaining components. i'm unsure of how though. math little dense. i'm refraining posting code because i'd general algorithm response, if 1 has preference, may state final benchmark used in should use jvm language.

any help?

note: similar gauss's circle problem, instead of counting number of points, want know points are.

you can values directly computing maximum y (the second coordinate of point on circle @ vertical of (x,0)) each value of x that:

for x in [-floor(r), floor(r)]     y_max = floor(sqrt(r^2 - x^2))    # pythagora's theorem     y in [-y_max, y_max]         # (x, y) ! 

i don't think can better (maybe can compute y_max faster won't big win) because anyway have these points in result.

edit:

this in pi*r^2 time, least can since it's number of points. can maybe save few computations doing quarter circle , getting other ones symmetry, i'm not sure it's faster, , it's longer write.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -