-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.rb
63 lines (48 loc) · 791 Bytes
/
vec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# terrible vector class =P
def vec(x,y)
Vec.new(x:x,y:y)
end
class Vec
attr_accessor :x, :y
def initialize(x:0,y:0)
@x = x
@y = y
end
def +(other)
vec(x+other.x,y+other.y)
end
def -(other)
vec(x-other.x,y-other.y)
end
def magnitude
Math.sqrt(@x*@x + @y*@y)
end
def unit
m = Math.sqrt(x*x+y*y)
vec(x/m, y/m)
end
def to_s
"Vec: [#{x},#{y}]"
end
def to_vec
self
end
alias_method :inspect, :to_s
def ==(other)
self.class == other.class and @x == other.x and @y == other.y
end
alias_method :eql?, :==
def hash
@x.hash + @y.hash
end
UP = vec(0,-1)
RIGHT = vec(1,0)
DOWN = vec(0,1)
LEFT = vec(-1,0)
NEIGHBOR_VECS = [
Vec::RIGHT,
Vec::UP,
Vec::DOWN,
Vec::LEFT,
]
end