class Vector3: def __init__(self, a = 0, b = 0, c = 0): self.x = a self.y = b self.z = c def __add__(self, other): return Vector3(self.x + other.x, self.y + other.y, self.z + other.z) def __radd__(self, other): return Vector3(self.x + other.x, self.y + other.y, self.z + other.z) def __mul__(self, scalar): return Vector3(self.x * scalar, self.y * scalar, self.z * scalar) def __rmul__(self, scalar): return Vector3(self.x * scalar, self.y * scalar, self.z * scalar) def __neg__(self): return Vector3(-self.x, -self.y, -self.z) def __pos__(self): return Vector3(self.x, self.y, self.z) def __xor__(self, other): cx = self.y * other.z - self.z * other.y cy = self.z * other.x - self.x * other.z cz = self.x * other.y - self.y * other.x return Vector3(cx, cy, cz) def cross(self, other): return self ^ other def __str__(self): return "(" + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + ")" def Test(): v1 = Vector3(1, 2, 3) v2 = Vector3(4, 5, 6) v3 = v1 + v2 v4 = v1 * 3 v5 = v1 ^ v2 v6 = v1.cross(v2) print(str(v3)) print(str(v4)) print("\n"+str(v5)) print(str(v6)) Test() print("DONE")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question