More pythonic circle?

John Machin sjmachin at lexicon.net
Sun Apr 9 19:09:03 EDT 2006


It's also possible to write microprocessor assembly language in any
other language.
The following code generates the OP's list of points with nothing more
complicated than integer addition/subtraction inside the loop. It also
does the right thing if the radius is not an integer, and avoids the
OP's  "0.71 approx == sin(45 deg) aka 1/sqrt(2)" caper.

Wrt your Pythonic suggestion: "I'll bet this does the trick for you and
runs faster than what you've got": You lose on "does the trick" (should
be <= radsq). As for the second clause, a prerequisite to testing that
is to get the OP to say what his typical radius and typical enclosing
box size are (and get your point that they should not be the same).

Cheers,
John

# def octant(radius):
#     assert radius >= 0
#     filllist = []
#     dx = int(radius)
#     dy = 0
#     trigger = dx * dx - int(radius * radius)
#     dx_squared_delta = dx + dx - 1
#     dy_squared_delta = 1
#     while dy <= dx:
#         if trigger <= 0:
#             for x in range(dy, dx+1):
#                 filllist.append((x, dy))
#             dy += 1
#             trigger += dy_squared_delta
#             dy_squared_delta += 2
#         else:
#             dx -= 1
#             trigger -= dx_squared_delta
#             dx_squared_delta -= 2
#     filllist.sort()
#     print "%.2f %r" % (radius, filllist)

# if __name__ == "__main__":
#     octant(3.99)
#     octant(4)
#     octant(4.01)
#     octant(3.60)
#     octant(3.61)
#     octant(0.01)
#     octant(0)




More information about the Python-list mailing list