Skip to content

Instantly share code, notes, and snippets.

@khafatech
Created June 3, 2009 03:48
Show Gist options
  • Save khafatech/122778 to your computer and use it in GitHub Desktop.
Save khafatech/122778 to your computer and use it in GitHub Desktop.
import sys
import math
if len(sys.argv) > 1:
radius = int(sys.argv[1])
else:
radius = 5
# The points of interest are on the right of the circle.
# Only half the points on the y axis are taken to preserve symmetry
points = []
for x in xrange(radius):
for y in xrange(-radius+1, radius):
if math.sqrt(x**2 + y**2) < radius:
points.append((x,y))
points.remove((0,0))
polar_points = {}
for p in points:
if p[0] == 0:
if p[1] > 0:
theta = math.radians(90)
else:
# we only need half the points on y-axis
# theta = math.radians(-90)
continue
else:
theta = math.atan(float(p[1])/p[0])
if theta in polar_points:
polar_points[theta] += 1
else:
polar_points[theta] = 1
# print polar_points
print "Number of polar points:", len(polar_points)
# sorted polar points
thetas = sorted(polar_points.keys())
# --- Hardcore processing begins here ---
def num_in_interval(a, b):
"Returns # of points between theta=a and theta=b (both exclusive)"
num_points = 0
# correct order
if a > b:
a, b = b, a
for theta in thetas[thetas.index(a)+1:thetas.index(b)]:
num_points += polar_points[theta]
return num_points
def xuniqueCombinations(items, n):
"""Just what it says. From http://code.activestate.com/recipes/190465/"""
if n==0: yield []
else:
for i in xrange(len(items)):
for cc in xuniqueCombinations(items[i+1:],n-1):
yield [items[i]]+cc
np = 0
for pair in xuniqueCombinations(thetas, 2):
# keys
np += (polar_points[pair[0]] * polar_points[pair[1]]) * num_in_interval(pair[0], pair[1])
# print pair[0], pair[1], ":", num_in_interval(pair[0], pair[1])
print "Triangles with center:", np * 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment