-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrect.rb
More file actions
111 lines (95 loc) · 2.34 KB
/
Copy pathrect.rb
File metadata and controls
111 lines (95 loc) · 2.34 KB
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
108
109
110
111
class Rect
attr_accessor :type, :dis, :x, :y, :w, :h, :matched
def initialize(type, dis, x, y, w, h)
@type = type
@dis = dis
@x = x
@y = y
@w = w
@h = h
@matched = false
end
def self.makeRect(desc)
eles = desc.split
type = eles[0].to_i
dis = eles[1].to_f
x, y, w, h = eles[2].split(':').map(&:to_i)
Rect.new(type, dis, x, y, w, h)
end
def self.makePureRect(sdesc)
x, y, w, h, dis = sdesc.split(':').map(&:to_i)
ty_raw = sdesc.split(':')[-1]
ty = if ty_raw[0] == '['
ty_raw
else
ty_raw.to_i
end
Rect.new(ty, dis, x, y, w, h)
end
def include(other, margin = 0)
c1 = has_point other.x + (other.w / 2), other.y + (other.h / 2), margin
c2 = other.has_point x + (w / 2), y + (h / 2), margin
# c1 || c2
c1 && c2
end
def to_s
# "#{@type}:#{@x}:#{@y}:#{@w}:#{@h}"
"#{@x}:#{@y}:#{@w}:#{@h}:#{@dis}:#{@type}"
end
def to_short_s
"#{@x}:#{@y}:#{@w}:#{@h}"
end
def has_point(x, y, margin = 0)
dx = x - @x
dy = y - @y
dx > margin * @w && @w * (1 - margin) > dx && dy > @h * margin && @h * (1 - margin) > dy
end
def distance_from(x, y)
Math.sqrt((@x + @w * 0.5 - x)**2 + (@y + @h * 0.5 - y)**2)
end
def diff(arect)
aw = arect.w
ah = arect.h
sw = (@w + aw)
sh = (@h + ah)
dx = (@x - arect.x + (@w - aw).to_f / 2).abs.to_f / sw
dy = (@y - arect.y + (@h - ah).to_f / 2).abs.to_f / sh
dw = (Math.log(@w) - Math.log(aw)).abs.to_f
dx + dy + dw
end
def shift!(dx, dy)
@x += dx * @w
@y += dy * @h
end
def *(k) # for average calculation
atype = @type
adis = @dis
ax = (@x * k).to_i
ay = (@y * k).to_i
aw = (@w * k).to_i
ah = (@h * k).to_i
Rect.new(atype, adis, ax, ay, aw, ah)
end
def +(other) # for average calculation
atype = @type
adis = 0
ax = @x + other.x
ay = @y + other.y
aw = @w + other.w
ah = @h + other.h
Rect.new(atype, adis, ax, ay, aw, ah)
end
def -(other) # for difference calculation
atype = @type
adis = 0
ax = @x - other.x
ay = @y - other.y
aw = @w - other.w
ah = @h - other.h
Rect.new(atype, adis, ax, ay, aw, ah)
end
def /(n)
Rect.new(@type, @dis, (@x.to_f / n).to_i, (@y.to_f / n).to_i,
(@w.to_f / n).to_i, (@h.to_f / n).to_i)
end
end