]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Changed the TwoVector add() method to an operator overload.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 25 Oct 2009 05:10:30 +0000 (01:10 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 25 Oct 2009 05:10:30 +0000 (01:10 -0400)
Added an operator overload for TwoVector subtraction.
Added the wkt() method for Polygons.

src/Geometry.py

index 8207b40465df1f23672c4173069f09016cfd6b80..6c039a8e445a908acc290c9f06f8bf1a551bb96f 100644 (file)
@@ -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],