forked from iberianpig/xSwipe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.rb
107 lines (86 loc) · 1.65 KB
/
Utils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'open3'
class CommandLine
def CommandLine.execute(command)
output = ""
Open3.popen3(command) do |stdin, stdout, stderr|
output = stdout.read
end
return output
end
end
class Vector
attr_reader :x
attr_reader :y
def initialize(x, y)
@x = x.to_f
@y = y.to_f
end
def angle
angle = Math.atan(@y/@x)
if @x > 0 and @y < 0
angle += 2*Math::PI
elsif @x < 0
angle += Math::PI
end
return (angle * 360) / (Math::PI*2)
end
def magnitude
return Math.sqrt(@x**2 + @y**2)
end
def zero?
return (@x == 0.0 and @y == 0.0)
end
def set(x, y)
@x = x.to_f
@y = y.to_f
end
def add(x, y)
@x += x
@y += y
end
def addVector(v)
add(v.x, v.y)
end
def reset
@x = 0.0
@y = 0.0
end
def -(vector)
return Vector.new(0,0) unless vector.is_a? Vector
return Vector.new(self.x-vector.x, self.y-vector.y)
end
def parallel?(vector, tolerance=5)
angle1 = self.angle
angle2 = vector.angle
return false if angle1.nan? or angle2.nan?
return true if angle1.between?(angle2-tolerance, angle2+tolerance)
return true if ( (angle1 + 180) % 360 ).between?(angle2-tolerance, angle2+tolerance)
end
def concorde?(vector, tolerance=5)
angle1 = self.angle
angle2 = vector.angle
return false if angle1.nan? or angle2.nan?
return true if angle1.between?(angle2-tolerance, angle2+tolerance)
end
def inspect
string = ""
#string += "[#@x, #@y]\n"
string += "{#{self.magnitude}, #{self.angle}}"
return string
end
end
class MeanVector < Vector
def initialize(x, y)
super
@n = 0
end
def add(x, y)
@x = (@n*@x + x) / (@n+1)
@y = (@n*@y + y) / (@n+1)
@n += 1
end
def reset
super
@n = 0
end
end