Código Python:
Ver original
from math import acos, degrees class vector3D(object): def __init__(self, x=0, y=0, z=0): """ Si, esto es un constructor """ self.x = x self.y = y self.z = z self.presicion = 2 def __add__(self, vect): """ Suma de vectores: v1 = <x1, y1, z1> v2 = <x2, y2, z2> regresa v3 = <x1 + x2, y1 + y2, z1 + z2> """ return vector3D(self.x + vect.x, self.y + vect.y, self.z + vect.z) def __iadd__(self, vect): """ Suma de vectores in-place v1 = <x1, y1, z1> v2 = <x2, y2, z2> v3 = <x1 + x2, y1 + y2, z1 + z2> """ self.x += vect.x self.y += vect.y self.z += vect.z def __sub__(self, vect): """ Resta de vectores: v1 = <x1, y1, z1> v2 = <x2, y2, z2> v3 = <x1 - x2, y1 - y2, z1 - z2> """ return vector3D(self.x - vect.x, self.y - vect.y, self.z - vect.z) def __isub__(self, vect): """ Resta de vectores in-place """ self.x -= vect.x self.y -= vect.y self.z -= vect.z def __mul__(self, vect): """ Producto vectorial """ xn = self.y * vect.z - self.z * vect.y yn = self.z * vect.x - self.x * vect.z zn = self.x * vect.y - self.y * vect.x return vector3D(xn, yn, zn) def __rmul__(self, k): return vector3D(self.x * k, self.y * k, self.z * k) def __radd(self, k): return vector3D(self.x + k, self.y + k, self.z + k) def __imul__(self, vect): """ Producto vectorial in-place """ xn = self.y * vect.z - self.z * vect.y yn = self.z * vect.x - self.x * vect.z zn = self.x * vect.y - self.y * vect.x self.x = xn self.y = yn self.z = zn def __mod__(self, vect): """ Producto punto: v1 = <x1, y1, z1> v2 = <x2, y2, z2> producto_punto = x1 * x2 + y1 * y2 + z1 * z2 """ return self.x * vect.x + self.y * vect.y + self.z * vect.z def __str__(self): """ Representacion del objeto en una cadena """ return "Vector: <%.*f, %.*f, %.*f>" % (self.presicion, self.x, self.presicion, self.y, self.presicion, self.z) def __repr__(self): """ Manda a llamar a __str__ y regresa el valor de __str__ """ return self.__str__() def __unicode__(self): """ Lo mismo que __str__ pero en unicode """ return unicode(self.__str__()) def __nonzero__(self): """ Si la magnitud del vector es 0 entonces es falso de otra manera verdadero """ return self.x or self.y or self.z def __getlen(self): """ getter para la magnitud del vector """ return (self.x ** 2 + self.y ** 2 + self.z ** 2) ** .5 def angle(self, vect, rad=False): """ Regresa el angulo entre 2 vectores """ an = acos(self % vect / float(self.len * vect.len)) if rad: return an else: return degrees(an) len = property(__getlen, doc="Regresa la magnitud del vector") def main(): v1 = vector3D(1, 0, 0) v2 = vector3D(0, 1, 0) v3 = vector3D() print "V1 =", v1 print "V2 =", v2 print "V3 =", v3 print "" print "V1 + V2 =", v1 + v2 #Suma de vectores print "V1 * V2 =", v1 * v2 #Producto cruz print "V1 % V2 =", v1 % v2 #Producto punto print "|V1| =", v1.len #Magnitud del vector print "bool(V1) =", bool(v1) #Ver si un vector es falso o verdadero print "bool(V3) = ", bool(v3) v3 = v1 + v2 print "V3 = V1 + V2" print "V3 =", v3 print "3 * V3 =", 3 * v3 print "Angulo entre V1 y V2 =", v1.angle(v2) if __name__ == "__main__": main()