Last active
August 29, 2015 14:04
-
-
Save jackjennings/ade094e709cf875a8d2b 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 | |
class Descartes(object): | |
tries = 10000 | |
def __init__(self, glyph): | |
self.glyph = glyph | |
def estimate(self): | |
if self.glyph is not None: | |
self._getPath() | |
points = 0 | |
for _ in xrange(self.tries): | |
x = random.randint(self.xMin, self.xMax) | |
y = random.randint(self.yMin, self.yMax) | |
if self.path.containsPoint_((x,y)): | |
points += 1 | |
return int((points / float(self.tries) * self.surface)) | |
def exact(self): | |
if self.glyph is not None: | |
self._getPath() | |
points = 0 | |
for x in range(self.xMin, self.xMax): | |
for y in range(self.yMin, self.yMax): | |
if self.path.containsPoint_((x,y)): | |
points += 1 | |
return points | |
def _getPath(self): | |
self.path = self.glyph.naked().getRepresentation("defconAppKit.NSBezierPath") | |
self.xMin, self.yMin, self.xMax, self.yMax = g.box | |
self.xMin = int(round(self.xMin)) | |
self.yMin = int(round(self.yMin)) | |
self.xMax = int(round(self.xMax)) | |
self.yMax = int(round(self.yMax)) | |
self.surface = (self.xMax - self.xMin) * (self.yMax - self.yMin) | |
from mojo.events import addObserver, removeObserver | |
from threading import Timer | |
class DescartesObserver(object): | |
def __init__(self): | |
#addObserver(self, 'update', 'currentGlyphChanged') | |
addObserver(self, 'estimate', 'draw') | |
self.timer = None | |
def e(self): | |
print Descartes(CurrentGlyph()).estimate() | |
def estimate(self, info): | |
if self.timer: | |
self.timer.cancel() | |
self.timer = Timer(2.0, self.e) | |
self.timer.start() | |
#DescartesObserver() | |
g = CurrentGlyph() | |
print Descartes(g).estimate() | |
print Descartes(g).exact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment