Created
July 26, 2014 18:21
-
-
Save jackjennings/9374435cee27c81c5c10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, random | |
g = CurrentGlyph() | |
if g is not None: | |
path = g.naked().getRepresentation("defconAppKit.NSBezierPath") | |
xMin, yMin, xMax, yMax = g.box | |
xMin = int(round(xMin)) | |
yMin = int(round(yMin)) | |
xMax= int(round(xMax)) | |
yMax = int(round(yMax)) | |
area = (xMax - xMin) * (yMax - yMin) | |
print "glyph bounds: %d" % area | |
start1 = time.time() | |
a1 = 0 | |
for x in range(xMin, xMax): | |
for y in range(yMin, yMax): | |
if path.containsPoint_((x,y)): a1 += 1 | |
area1 = a1 | |
print "area 1: %d" % area1 | |
print "finished in %f seconds" % (time.time() - start1) | |
for t in range(3, 7): | |
tries = pow(10, t) | |
print "tries: %d" % tries | |
avg_off = [] | |
avg_exec = [] | |
for _ in xrange(1000): | |
start2 = time.time() | |
a2 = 0 | |
for _ in xrange(tries): | |
x = random.randint(xMin, xMax) | |
y = random.randint(yMin, yMax) | |
if path.containsPoint_((x,y)): a2 += 1 | |
area2 = int((a2 / float(tries) * area)) | |
avg_off.append(area1 - area2) | |
avg_exec.append(time.time() - start2) | |
print "average error: %d" % (sum(avg_off)/len(avg_off)) | |
print "average runtime: %f" % (sum(avg_exec)/float(len(avg_exec))) | |
#Results: | |
#glyph bounds: 440455 | |
#area 1: 150932 | |
#finished in 2.420331 seconds | |
#tries: 1000 | |
#average error: 241 | |
#average runtime: 0.012808 | |
#tries: 10000 | |
#average error: 348 | |
#average runtime: 0.132940 | |
#tries: 100000 | |
#average error: 338 | |
#average runtime: 1.300716 | |
#tries: 1000000 | |
#average error: 357 | |
#average runtime: 12.921610 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment