Skip to content
This repository
Browse code

the wattenbergian fix for PVector is insane

  • Loading branch information...
commit 55d1cfb48cc8c73ca15ced9efbe3ace775c02cc2 1 parent a634dbf
Jonathan Feinberg authored
1  runtime/src/jycessing/PAppletJythonDriver.java
@@ -36,6 +36,7 @@
36 36 import processing.core.PApplet;
37 37 import processing.core.PConstants;
38 38 import processing.core.PImage;
  39 +import processing.core.PVector;
39 40
40 41 /**
41 42 *
51 runtime/src/jycessing/core.py
@@ -16,7 +16,7 @@
16 16 from processing.core import PSmoothTriangle
17 17 from processing.core import PStyle
18 18 from processing.core import PTriangle
19   -from processing.core import PVector
  19 +from processing.core import PVector as RealPVector
20 20
21 21 # Thanks, Guido!
22 22 # http://mail.python.org/pipermail/python-dev/2008-January/076194.html
@@ -26,19 +26,58 @@ def decorator(func):
26 26 return func
27 27 return decorator
28 28
  29 +class PVector(object):
  30 + @classmethod
  31 + def __new__(cls, *args):
  32 + return RealPVector(*args[1:])
  33 +
  34 + @classmethod
  35 + def add(cls, a, b, dest=None):
  36 + return RealPVector.add(a, b, dest)
  37 +
  38 + @classmethod
  39 + def sub(cls, a, b, dest=None):
  40 + return RealPVector.sub(a, b, dest)
  41 +
  42 + @classmethod
  43 + def mult(cls, a, b, dest=None):
  44 + return RealPVector.mult(a, b, dest)
  45 +
  46 + @classmethod
  47 + def div(cls, a, b, dest=None):
  48 + return RealPVector.div(a, b, dest)
  49 +
  50 + @classmethod
  51 + def cross(cls, a, b, dest=None):
  52 + return RealPVector.cross(a, b, dest)
  53 +
  54 + @classmethod
  55 + def dist(cls, a, b):
  56 + return RealPVector.dist(a, b)
  57 +
  58 + @classmethod
  59 + def dot(cls, a, b):
  60 + return RealPVector.dot(a, b)
  61 +
  62 + @classmethod
  63 + def angleBetween(cls, a, b):
  64 + return RealPVector.angleBetween(a, b)
  65 +
29 66 # Because PVector isn't serializable, we have to add __deepcopy__
30   -@monkeypatch_method(PVector)
  67 +@monkeypatch_method(RealPVector)
31 68 def __deepcopy__(self, memo):
32 69 return PVector(self.x, self.y, self.z)
33 70
34   -@monkeypatch_method(PVector)
  71 +@monkeypatch_method(RealPVector)
35 72 def __sub__(a, b):
36 73 return PVector(a.x - b.x, a.y - b.y, a.z - b.z)
37 74
38   -@monkeypatch_method(PVector)
  75 +@monkeypatch_method(RealPVector)
39 76 def __add__(a, b):
40 77 return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
41 78
42   -@monkeypatch_method(PVector)
  79 +@monkeypatch_method(RealPVector)
43 80 def __mul__(a, b):
44   - return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
  81 + if isinstance(b, RealPVector):
  82 + return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
  83 + return PVector(a.x * b, a.y * b, a.z * b)
19 testing/test_resources/test_pvector.py
... ... @@ -1,6 +1,23 @@
1 1 a = PVector(5, 7, 11)
2 2 b = PVector(13, 17, 23)
3   -print a - b
  3 +assert a - b == PVector(-8.0, -10.0, -12.0)
4 4 print b - a
5 5 print b + a
6 6 print a + b
  7 +
  8 +print PVector.add(a, b)
  9 +print PVector.add(a, b)
  10 +a.add(b)
  11 +print a
  12 +a.add(b)
  13 +print a
  14 +
  15 +print a * b
  16 +print a * 10
  17 +print a * 10
  18 +a.mult(10)
  19 +print a
  20 +
  21 +print PVector.dist(a, b)
  22 +print PVector.cross(a, b)
  23 +print PVector.dot(a, b)

0 comments on commit 55d1cfb

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