# 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.
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.
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):
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)
# Ex: POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))
if (text.lower().find('polygon') == -1):
return None
-
- return shapely.wkt.loads(text)
+
+ sp = shapely.wkt.loads(text)
+ return Polygon.from_shapely(sp)
def drag_rectangle(self, v):
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],
def drag(self, v):
"""
- Drag this polygon along the line v = (x2-x1, y2-y1), and
+ Drag this polygon along the vector v = (x2-x1, y2-y1), and
return the new polygon consisting of the union of all points
- that I've contained along the way.
+ that we've contained along the way.
"""
terminal_p = self.translate(v)
dv_rect = self.drag_rectangle(v)