[Tutor] Find Integer co-ordinates lying on a circle

Shashwat Anand anand.shashwat at gmail.com
Sun Nov 15 02:35:38 CET 2009


How to find all possible integer co-ordinates lying on a circle of given
radius 'r'.
If given the upper bound of 'r', I want to calculate all given co-ordinates
lying for 0 <= r <= n

Let's say the upper bound of radius is 5
All possible results are:
radius 'r' - (x, y)
0 - (0, 0)
1 - (1, 0), (0, 1), (-1, 0), (0, -1)
2 - (2, 0), (0, 2), (-2, 0), (0, -2)
3 - (3, 0), (0, 3), (-3, 0), (0, -3)
4 - (4, 0), (0, 4), (-4, 0), (0, -4)
5 - (5, 0), (0, 5), (-5, 0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3,
4), (-4, 3), (-3, -4), (-4, -3)

Also for a particular radius, lots of possible combination of (x, y) is
possible so best datastructure could be defaultdict for further operations
IMHO.
So my desired output is:
defaultdict(<type 'list'>, {0 : [(0, 0)], 1: [(1, 0), (0, 1), (-1, 0), (0,
-1)], 2: [(2, 0), (0, 2), (-2, 0), (0, -2)], 3: [(3, 0), (0, 3), (-3, 0),
(0, -3)], 4: [(4, 0), (0, 4), (-4, 0), (0, -4)], 5: [(5, 0), (0, 5), (-5,
0), (0, -5), (3, 4), (4,3), (3, -4), (4, -3), (-3, 4), (-4, 3), (-3, -4),
(-4, -3)]})

My approach using pythagorean triplets:
>>> d = collections.defaultdict(list)
>>> s = list(set([((u*u + v*v), (v*v - u*u, 2*u*v)) for u in range(10) for v
in range(u+1, 10)]))
>>> [d[k].append(v) for k,v in s]
However it sure is wrong and fails in my case.

Any suggestions as to how can I reach my desired goal, or are there any
other options to tackle this problem?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091115/4cecac70/attachment-0001.htm>


More information about the Tutor mailing list