Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: TyOverby/Notes
base: f04a69122d
...
head fork: TyOverby/Notes
compare: ce0ab1bbcc
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Sep 28, 2012
U-Ty-PC-Windows\Ty started working on the new vector classes ef588c1
U-Ty-PC-Windows\Ty split up into multiple files e19acf1
U-Ty-PC-Windows\Ty finished the majority of my vectoring code ce0ab1b
View
1  .gitignore
@@ -1,2 +1,3 @@
*.swp
*.swo
+*.pyc
View
42 math126/vector.py
@@ -2,38 +2,14 @@
import os
import math
-def distance(vec1, vec2):
- deltax = vec1[0] - vec2[0]
- deltay = vec1[1] - vec2[1]
- deltaz = vec1[2] - vec2[2]
-
- dist_squared = deltax*deltax + deltay*deltay + deltaz*deltaz
- print "sqrt(" + str(dist_squared) + ")"
- return math.sqrt(dist_squared)
-
-def get_vector(say="Input vector: "):
- text = raw_input(say)
- split = re.findall(r"-?\d+\.?\d*", text)
- if len(split) is not 3:
- print "Must contain 3 numbers seperated by a non-number"
- os._exit(1)
- return (float(split[0]), float(split[1]), float(split[2]))
-
-
-def distance_sub(vec1=None):
- if vec1 == None:
- print distance(get_vector("Enter First Vector: "), get_vector("Enter Second Vector: "))
-
-def main_switch(text = raw_input("Enter a command: ")):
- if "dist" in text:
- distance_sub();
- if "+" in text:
- print "\nnext:\n"
- main_switch(text)
-
+from vector2 import Vector2
+from vector3 import Vector3
+
+def v(*args):
+ if len(args) is 2:
+ return Vector2(args[0],args[1])
+ elif len(args) is 3:
+ return Vector3(args[0],args[1],args[2])
else:
- print "Command not found. Quitting"
- os._exit(1)
+ raise TypeError("Two and three dimensional vectors are all that is supported")
-main_switch()
-os._exit(0)
View
76 math126/vector2.py
@@ -0,0 +1,76 @@
+import math
+
+class Vector2(object):
+ def __init__(self, x, y):
+ super(Vector2, self).__setattr__('x', x)
+ super(Vector2, self).__setattr__('y', y)
+
+ def __str__(self):
+ return "<"+str(self.x)+", "+str(self.y)+">"
+
+ def __repr__(self):
+ return str(self)
+
+ def __setattr__(self, *args):
+ raise TypeError("Can't modify immutable vector")
+
+ def __getattr__(self, name):
+ if name in ["magnitude", "mag", "rho"]:
+ return math.sqrt(self.mag2)
+ elif name is "mag2":
+ return self.x*self.x + self.y*self.y
+ elif name is "theta":
+ theta = math.degrees(math.atan2(self.x,self.y))
+ if theta < -360 or theta > 360:
+ theta = theta % 360
+ if theta < 0:
+ theta = 360 + theta
+ return theta
+ else:
+ return super(Vector2, self).__getattr__(name)
+
+ __delattr__ = __setattr__
+
+ def __getitem__(self, key):
+ if key is 0:
+ return self.x
+ elif key is 1:
+ return self.y
+ elif key is "x":
+ return self.x
+ elif key is "y":
+ return self.y
+
+ def __len__(self):
+ return 2
+
+ def __add__(self, other):
+ return Vector2(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ return Vector2(self.x - other.x, self.y - other.y)
+
+ def __mul__(self, other):
+ return Vector2(self.x * other, self.y * other)
+ def __rmul__(self, other):
+ return self.__mul__(other)
+
+ def __div__(self,other):
+ return Vector2(self.x / other, self.y / other)
+
+
+ def __neg__(self, other):
+ return Vector2(-self.x, -self.y)
+
+ def __or__(self, other):
+ return self.magnitude
+ def __ror__(self,other):
+ return self.magnitude
+
+ def dot(v1, v2):
+ return v1.x*v2.x + v1.y*v2.y
+
+ __iadd__ = __setattr__
+ __isub__ = __setattr__
+ __imul__ = __setattr__
+ __idiv__ = __setattr__
View
79 math126/vector3.py
@@ -0,0 +1,79 @@
+import re
+import os
+import math
+
+class Vector3(object):
+ def __init__(self, x, y, z):
+ super(Vector3, self).__setattr__('x', x)
+ super(Vector3, self).__setattr__('y', y)
+ super(Vector3, self).__setattr__('z', z)
+
+ def __str__(self):
+ return "<"+str(self.x)+", "+str(self.y)+", "+str(self.z)+">"
+
+ def __repr__(self):
+ return str(self)
+
+ def __setattr__(self, *args):
+ raise TypeError("Can't modify immutable vector")
+
+ def __getattr__(self, name):
+ if name in ["magnitude", "mag", "rho"]:
+ return math.sqrt(self.mag2)
+ elif name is "mag2":
+ return self.x*self.x + self.y*self.y + self.z*self.z
+ else:
+ return super(Vector2, self).__getattr__(name)
+
+ __delattr__ = __setattr__
+
+ def __getitem__(self, key):
+ if key is 0:
+ return self.x
+ elif key is 1:
+ return self.y
+ elif key is 2:
+ return self.z
+ elif key is "x":
+ return self.x
+ elif key is "y":
+ return self.y
+ elif key is "z":
+ return self.z
+ def __len__(self):
+ return 3
+
+
+ def __add__(self, other):
+ return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
+
+ def __sub__(self, other):
+ return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
+
+ def __mul__(self, other):
+ return Vector3(self.x * other, self.y * other, self.z * other)
+ def __rmul__(self, other):
+ return self.__mul__(other)
+
+ def __div__(self,other):
+ return Vector3(self.x / other, self.y / other, self.z / other)
+
+
+ def __neg__(self, other):
+ return Vector3(-self.x, -self.y, -self.z)
+
+ def __or__(self, other):
+ return self.magnitude
+ def __ror__(self,other):
+ return self.magnitude
+
+ def dot(v1, v2):
+ return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
+
+ def cross(v1, v2):
+ return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y*v2.x)
+
+ __iadd__ = __setattr__
+ __isub__ = __setattr__
+ __imul__ = __setattr__
+ __idiv__ = __setattr__

No commit comments for this range

Something went wrong with that request. Please try again.