From: Michael Orlitzky Date: Sun, 25 Oct 2009 05:10:30 +0000 (-0400) Subject: Changed the TwoVector add() method to an operator overload. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=ce56f20ad834efa5d9b5094d8280cc594097f848;p=dead%2Fcensus-tools.git Changed the TwoVector add() method to an operator overload. Added an operator overload for TwoVector subtraction. Added the wkt() method for Polygons. --- diff --git a/src/Geometry.py b/src/Geometry.py index 8207b40..6c039a8 100644 --- a/src/Geometry.py +++ b/src/Geometry.py @@ -56,7 +56,23 @@ class TwoVector: # If v is neither a TwoVector nor a tuple, we ain't it. return False + + def __add__(self, other_vector): + """ + Add other_vector to this vector, and return a new TwoVector + containing the result. + """ + return TwoVector(self.x + other_vector.x, self.y + other_vector.y) + + def __sub__(self, other_vector): + """ + Subtract other_vector from this vector. Return a new + TwoVector. + """ + return TwoVector(self.x - other_vector.x, self.y - other_vector.y) + + def __str__(self): """ Print the contents of our ordered pair. @@ -85,15 +101,7 @@ class TwoVector: return (self.x * other_vector.x) + (self.y * other_vector.y) - def add(self, other_vector): - """ - Add other_vector to this vector, and return a new TwoVector - containing the result. - """ - return TwoVector(self.x + other_vector.x, self.y + other_vector.y) - - class Polygon: """ Wraps shapely.geometry.Polygon. @@ -145,10 +153,19 @@ class Polygon: def __str__(self): """ - Write out the well-known text representation of this polygon. + Write out the well-known text representation of this polygon, + as well as the class name, which helps distinguish + Geometry.Polygon from shapely.geometry.Polygon.. """ return "Geometry.Polygon<%s>" % self._shapely_polygon.to_wkt() + + def wkt(self): + """ + Return just the well-known text for this Polygon. + """ + return self._shapely_polygon.to_wkt() + @classmethod def from_shapely(self, shapely_polygon): @@ -187,7 +204,7 @@ class Polygon: Translate the self polygon by the TwoVector v. """ regular_coords = self.coords() - f = (lambda p: p.add(v)) + f = (lambda p: p + v) translated_coords = map(f, regular_coords) return Polygon(translated_coords) @@ -241,8 +258,8 @@ class Polygon: initial_dv = self.drag_vertices(v) terminal_dv = [0, 0] # Just initializing it. - terminal_dv[0] = initial_dv[0].add(v) - terminal_dv[1] = initial_dv[1].add(v) + terminal_dv[0] = initial_dv[0] + v + terminal_dv[1] = initial_dv[1] + v return Polygon( (initial_dv[1], initial_dv[0],