Generate Coordinates In Grid That Lie Within A Circle
I've found this answer, which seems to be somewhat related to this question, but I'm wondering if it's possible to generate the coordinates one by one without the additional ~22% (
Solution 1:
What about simply something like this (for a circle at origin)?
X = int(R) # R is the radiusfor x inrange(-X,X+1):
Y = int((R*R-x*x)**0.5) # bound for y given xfor y inrange(-Y,Y+1):
yield (x,y)
This can easily be adapted to the general case when the circle is not centred at the origin.
Solution 2:
You might want to consider my algorithm for Gauss circle problem (with some Java source code and an ugly but handy illustration): https://stackoverflow.com/a/42373448/5298879
It's around 3.4x faster than counting points in one of the quarters, plus the center, plus the ones on the axis, that you're doing now, while taking just one more line of code.
You simply imagine an inscribed square and count only one-eighth of what's outside that square inside that circle.
Post a Comment for "Generate Coordinates In Grid That Lie Within A Circle"