三维向量类

class vector3:

 def __init__(self, x_ = 0, y_ = 0, z_ = 0): #构造函数

  self.x = x_

  self.y = y_

  self.z = z_

 def __add__(self, obj): 

  return vector3(self.x+obj.x, self.y+obj.y, self.z+obj.z)

 def __sub__(self, obj): 
  return vector3(self.x-obj.x, self.y-obj.y, self.z-obj.z)

 def __mul__(self, obj): 

  return vector3(self.x*obj.x, self.y*obj.y, self.z*obj.z)

 def __pow__(self, obj): 

  return vector3(self.y*obj.z-obj.y*self.z, self.z*obj.x-self.x*obj.z, self.x*obj.y-obj.x*self.y)

 def __str__(self): 

  return str(self.x)+','+str(self.y)+','+str(self.z)

v1 = vector3(1, 2, 3)

v2 = vector3(0, 1, 2)

print (v1 + v2)
print (v1 - v2)
print (v1 * v2)
print (v1 ** v2)

  

posted @ 2020-06-14 14:52  liuchun玲  阅读(215)  评论(0编辑  收藏  举报