Skip to content

Commit

Permalink
refactored polygon code into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Dec 31, 2011
1 parent f02e865 commit aee68a9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 56 deletions.
17 changes: 17 additions & 0 deletions lib/polygon/__init__.py
@@ -0,0 +1,17 @@
from poly import PolyPolygon

Polygon = PolyPolygon


if __name__ == "__main__":
p = Polygon([[(0,0),(30,0),(15,30)]])
print 'p', p.area()
q = Polygon([[(10,5),(30,0),(15,20)]])
print 'q', q.area()
print 'p+q', (p+q).area()
print 'p-q', (p-q).area()
print 'p&q', (p&q).area()
print 'p|q', (p|q).area()



57 changes: 57 additions & 0 deletions lib/polygon/base.py
@@ -0,0 +1,57 @@
"""
kartograph - a svg mapping library
Copyright (C) 2011 Gregor Aisch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


class AbstractPolygon(object):
"""
all polygon implementation will implement this interface
"""
def __init__(self, contours=None, data=None):
self.data = data

def __len__(self):
raise NotImplementedError

def __getitem__(self, key):
raise NotImplementedError

def addContour(self, contour):
raise NotImplementedError

def __and__(self, other):
raise NotImplementedError

def __or__(self, other):
raise NotImplementedError

def __add__(self, other):
return self.__or__(other)

def __sub__(self, other):
raise NotImplementedError

def area(self):
raise NotImplementedError

def center(self):
raise NotImplementedError

def isInside(self, x, y):
raise NotImplementedError


60 changes: 4 additions & 56 deletions lib/polygon.py → lib/polygon/poly.py
Expand Up @@ -14,50 +14,14 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""


class AbstractPolygon(object):
"""
all polygon implementation will implement this interface
"""
def __init__(self, contours=None, data=None):
self.data = data

def __len__(self):
raise NotImplementedError

def __getitem__(self, key):
raise NotImplementedError

def addContour(self, contour):
raise NotImplementedError

def __and__(self, other):
raise NotImplementedError

def __or__(self, other):
raise NotImplementedError

def __add__(self, other):
return self.__or__(other)

def __sub__(self, other):
raise NotImplementedError

def area(self):
raise NotImplementedError

def center(self):
raise NotImplementedError

def isInside(self, x, y):
raise NotImplementedError

"""

from base import AbstractPolygon

class PolyPolygon(AbstractPolygon):
"""
wrapper for python polygon package
http://pypi.python.org/pypi/Polygon/2.0.4
http://www.j-raedler.de/projects/polygon/
"""
Expand Down Expand Up @@ -99,19 +63,3 @@ def center(self):
def isInside(self, x, y):
return self.poly.isInside(x,y)



Polygon = PolyPolygon



if __name__ == "__main__":

p = Polygon([[(0,0),(30,0),(15,30)]])
print 'p', p.area()
q = Polygon([[(10,5),(30,0),(15,20)]])
print 'q', q.area()
print 'p+q', (p+q).area()
print 'p-q', (p-q).area()
print 'p&q', (p&q).area()
print 'p|q', (p|q).area()

0 comments on commit aee68a9

Please sign in to comment.