Advertisement
Sax

Operación con Vectores

Sax
Apr 7th, 2011
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # -*- coding: cp1252 -*-
  2. #Xavier Sánchez Díaz 1540717
  3. #Operaciones con vectores como objetos
  4.  
  5. class Vector:
  6.     def __init__(self, x, y, z):
  7.         self.i = x
  8.         self.j = y
  9.         self.k = z
  10.  
  11. def producto_cruz(vector1, vector2):
  12.     x = vector1.j * vector2.k - vector1.k * vector2.j
  13.     y = vector1.i * vector2.k - vector1.k * vector2.i
  14.     z = vector1.i * vector2.j - vector1.j * vector2.i
  15.    
  16.     return (x, -1* y, z)
  17.  
  18. def producto_punto(vector1, vector2):
  19.     return vector1.i * vector2.i + vector1.j * vector2.j + vector1.k * vector2.k
  20.  
  21. def suma (vector1, vector2):
  22.     return vector1.i + vector2.i, vector1.j + vector2.j, vector1.k + vector2.k
  23.  
  24. def resta (vector1, vector2):
  25.     return vector1.i - vector2.i, vector1.j - vector2.j, vector1.k - vector2.k
  26.  
  27. def imprime_vector(vector):
  28.     return vector.i, vector.j, vector.k
  29.  
  30. v1 = Vector(0,0,0)
  31. v2 = Vector(0,0,0)
  32.  
  33. print "Introduce dos vectores a continuacion: "
  34.  
  35. v1.i = int(raw_input("i del vector 1? -> "))
  36. v1.j = int(raw_input("j del vector 1? -> "))
  37. v1.k = int(raw_input("k del vector 1? -> "))
  38.  
  39. print imprime_vector(v1)
  40.  
  41. v2.i = int(raw_input("i del vector 2? -> "))
  42. v2.j = int(raw_input("j del vector 2? -> "))
  43. v2.k = int(raw_input("k del vector 2? -> "))
  44.  
  45. print imprime_vector(v2)
  46.  
  47. s = suma(v1,v2)
  48. r1 = resta(v1,v2)
  49. r2 = resta(v2,v1)
  50. pp = producto_punto(v1,v2)
  51. pc1 = producto_cruz(v1,v2)
  52. pc2 = producto_cruz(v2,v1)
  53.  
  54. print "La suma es " + str(s)
  55. print "La resta de v1-v2 es " + str(r1)
  56. print "La resta de v2-v1 es " + str(r2)
  57. print "El producto punto es " + str(pp)
  58. print "El producto cruz v1xv2 es " + str(pc1)
  59. print "El producto cruz v2xv1 es " + str(pc2)
  60.  
  61. raw_input("\nPresione enter para continuar... ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement