Skip to content
Browse files

the wattenbergian fix for PVector is insane

  • Loading branch information...
1 parent a634dbf commit 55d1cfb48cc8c73ca15ced9efbe3ace775c02cc2 @jdf committed Sep 15, 2010
View
1 runtime/src/jycessing/PAppletJythonDriver.java
@@ -36,6 +36,7 @@
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PImage;
+import processing.core.PVector;
/**
*
View
51 runtime/src/jycessing/core.py
@@ -16,7 +16,7 @@
from processing.core import PSmoothTriangle
from processing.core import PStyle
from processing.core import PTriangle
-from processing.core import PVector
+from processing.core import PVector as RealPVector
# Thanks, Guido!
# http://mail.python.org/pipermail/python-dev/2008-January/076194.html
@@ -26,19 +26,58 @@ def decorator(func):
return func
return decorator
+class PVector(object):
+ @classmethod
+ def __new__(cls, *args):
+ return RealPVector(*args[1:])
+
+ @classmethod
+ def add(cls, a, b, dest=None):
+ return RealPVector.add(a, b, dest)
+
+ @classmethod
+ def sub(cls, a, b, dest=None):
+ return RealPVector.sub(a, b, dest)
+
+ @classmethod
+ def mult(cls, a, b, dest=None):
+ return RealPVector.mult(a, b, dest)
+
+ @classmethod
+ def div(cls, a, b, dest=None):
+ return RealPVector.div(a, b, dest)
+
+ @classmethod
+ def cross(cls, a, b, dest=None):
+ return RealPVector.cross(a, b, dest)
+
+ @classmethod
+ def dist(cls, a, b):
+ return RealPVector.dist(a, b)
+
+ @classmethod
+ def dot(cls, a, b):
+ return RealPVector.dot(a, b)
+
+ @classmethod
+ def angleBetween(cls, a, b):
+ return RealPVector.angleBetween(a, b)
+
# Because PVector isn't serializable, we have to add __deepcopy__
-@monkeypatch_method(PVector)
+@monkeypatch_method(RealPVector)
def __deepcopy__(self, memo):
return PVector(self.x, self.y, self.z)
-@monkeypatch_method(PVector)
+@monkeypatch_method(RealPVector)
def __sub__(a, b):
return PVector(a.x - b.x, a.y - b.y, a.z - b.z)
-@monkeypatch_method(PVector)
+@monkeypatch_method(RealPVector)
def __add__(a, b):
return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
-@monkeypatch_method(PVector)
+@monkeypatch_method(RealPVector)
def __mul__(a, b):
- return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
+ if isinstance(b, RealPVector):
+ return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
+ return PVector(a.x * b, a.y * b, a.z * b)
View
19 testing/test_resources/test_pvector.py
@@ -1,6 +1,23 @@
a = PVector(5, 7, 11)
b = PVector(13, 17, 23)
-print a - b
+assert a - b == PVector(-8.0, -10.0, -12.0)
print b - a
print b + a
print a + b
+
+print PVector.add(a, b)
+print PVector.add(a, b)
+a.add(b)
+print a
+a.add(b)
+print a
+
+print a * b
+print a * 10
+print a * 10
+a.mult(10)
+print a
+
+print PVector.dist(a, b)
+print PVector.cross(a, b)
+print PVector.dot(a, b)

0 comments on commit 55d1cfb

Please sign in to comment.
Something went wrong with that request. Please try again.