roguev6 > vect.py


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

#coding:shift-jis
'''
>>> vec = Vector2d(10,20)
>>> tuple(vec)
(10, 20)
>>> vec2 = Vector2d(20,30)
>>> tuple(vec+vec2) 
(30, 50)
>>> vec+=vec2
>>> vec == (30,50)
True
>>> vec3 = Vector2d(100,100)
>>> tuple(vec3 - vec2)
(80, 70)
>>> vec3-=vec
>>> vec3 == (70,50)
True
>>> vec3 == Vector2d(70,50)
True
'''
class Vector2d:
  def __init__(self,x,y):
    self.x=x
    self.y=y
  def __add__(self,t):
    return Vector2d(self.x+t.x,self.y+t.y)
  def __sub__(self,t):
    return Vector2d(self.x-t.x,self.y-t.y)
  def inv(t):
    return Vector2d(self.x,self.y)
  def __iadd__(self,t):
    self.x+=t.x
    self.y+=t.y
    return self
  def __isub__(self,t):
    self.x-=t.x
    self.y-=t.y
    return self
  def __eq__(self,t):
    if type(t)==tuple and len(t)==2:
      return self.x==t[0] and self.y ==t[1]
    if isinstance(t,Vector2d):
      return self.x==t.x and self.y == t.y
    else:
      return False
  def __iter__(self):
    return (self.x,self.y)

class Direction:
  NO_DIR= Vector2d(0,0)
  NORTH = Vector2d(0,-1)
  SOUTH = Vector2d(0,1)
  WEST =  Vector2d(-1,0)
  EAST =  Vector2d(1,0)
  NORTH_WEST = NORTH + WEST
  NORTH_EAST = NORTH + EAST
  SOUTH_WEST = SOUTH + WEST
  SOUTH_EAST = SOUTH + EAST

if __name__=='__main__':
  import doctest
  doctest.testmod()