diff python/moving.py @ 184:d70e9b36889c

initial work on flow vectors and clustering algorithms
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 25 Nov 2011 18:38:54 -0500
parents d3f6de6c3918
children c06379f25ab8
line wrap: on
line diff
--- a/python/moving.py	Thu Nov 24 19:20:07 2011 -0500
+++ b/python/moving.py	Fri Nov 25 18:38:54 2011 -0500
@@ -129,6 +129,9 @@
     def __repr__(self):
         return self.__str__()
 
+    def __add__(self, other):
+        return Point(self.x+other.x, self.y+other.y)
+
     def __sub__(self, other):
         return Point(self.x-other.x, self.y-other.y)
 
@@ -199,6 +202,29 @@
         from matplotlib.pyplot import scatter
         scatter([p.x for p in points],[p.y for p in points], c=color)
 
+class FlowVector:
+    '''Class to represent 4-D flow vectors,
+    ie a position and a velocity'''
+    def __init__(self, position, velocity):
+        'position and velocity should be Point instances'
+        self.position = position
+        self.velocity = velocity
+
+    def __add__(self, other):
+        return FlowVector(self.position+other.position, self.velocity+other.velocity)
+
+    def multiply(self, alpha):
+        return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha))
+
+    def draw(self, options = ''):
+        from matplotlib.pylab import plot
+        plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options)
+        self.position.draw(options+'x')
+    
+    @staticmethod
+    def similar(f1, f2, maxDistance2, maxDeltavelocity2):
+        return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2
+
 def segmentIntersection(p1, p2, p3, p4):
     '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise'''
     from numpy import matrix