geom_craft 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +20 -0
- data/Rakefile +4 -0
- data/geom_craft.gemspec +23 -0
- data/lib/geom_craft/core_ext/math.rb +13 -0
- data/lib/geom_craft/core_ext/numeric.rb +44 -0
- data/lib/geom_craft/core_ext.rb +7 -0
- data/lib/geom_craft/rand_norm.rb +55 -0
- data/lib/geom_craft/range2.rb +462 -0
- data/lib/geom_craft/rect.rb +723 -0
- data/lib/geom_craft/top_level_bind.rb +17 -0
- data/lib/geom_craft/vec2.rb +610 -0
- data/lib/geom_craft/version.rb +3 -0
- data/lib/geom_craft.rb +8 -0
- data/spec/geom_craft_spec.rb +4 -0
- data/spec/spec_helper.rb +6 -0
- data/workbench/core_ext.rb +21 -0
- data/workbench/range2.rb +217 -0
- data/workbench/rect.rb +271 -0
- data/workbench/setup.rb +8 -0
- data/workbench/vec2.rb +538 -0
- metadata +110 -0
data/workbench/range2.rb
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#+hidden: true
|
|
2
|
+
require "./setup"
|
|
3
|
+
|
|
4
|
+
#+title2: 早見表
|
|
5
|
+
|
|
6
|
+
# | 種類 | 意味 | Methods | 同類・備考 |
|
|
7
|
+
# |--------+--------------------------+----------------------+--------------------------------|
|
|
8
|
+
# | 生成 | pos ± (len / 2) | from_pos_and_len | 推奨 |
|
|
9
|
+
# | 生成 | start..end | new(start, end) | |
|
|
10
|
+
# | 参照 | 左, 右, 中央 | start / end / middle | |
|
|
11
|
+
# | 参照 | 強さ (end - start) | magnitude | |
|
|
12
|
+
# | 参照 | 長さ (強さの絶対値) | length | |
|
|
13
|
+
# | 向き | 向き | direction | -1, 0, 1 を返す |
|
|
14
|
+
# | 向き | 相手と同じ向きか? | same_direction?(o) | |
|
|
15
|
+
# | 向き | 反転 | invert | |
|
|
16
|
+
# | 向き | 「←」なら「→」にする | absolute | |
|
|
17
|
+
# | 対領域 | OR | a.max(o) | |
|
|
18
|
+
# | 対領域 | OR (向きを維持する) | a.max_directed(o) | |
|
|
19
|
+
# | 対領域 | AND | a.overlap(o) | |
|
|
20
|
+
# | 移動 | 相手の軸に揃える | a.align_start_of(o) | align_end_of / align_middle_of |
|
|
21
|
+
# | 移動 | 相手の軸の ? に揃える | a.align_to(?, o) | |
|
|
22
|
+
# | 移動 | 相手の隣に並べる | a.align_after(o) | a.align_before(o) |
|
|
23
|
+
# | 移動 | v だけ移動する | shift(v) | |
|
|
24
|
+
# | 変形 | 縮小 | pad(v) | pad_start / pad_end / pad_ends |
|
|
25
|
+
# | 変形 | v に近い方の端を広げる | stretch_to_value(v) | |
|
|
26
|
+
# | その他 | 線形補完 | a.lerp(v) | a.map_value(v, o) |
|
|
27
|
+
# | その他 | 領域に含まれるか? | a.contains?(v) | |
|
|
28
|
+
# | その他 | 相手を補正する | clamp_value(v) | v.clamp(range) に類似 |
|
|
29
|
+
# | その他 | v に近い方の Edge を返す | closest_edge(v) | |
|
|
30
|
+
# | その他 | 小数補正 | round | floor / ceil / truncate |
|
|
31
|
+
|
|
32
|
+
#+title2: 特徴
|
|
33
|
+
|
|
34
|
+
# - Range クラスとは根本的に考え方が異なる
|
|
35
|
+
# - 単に start と end の位置を持つだけ
|
|
36
|
+
# - `start > end` の関係になることもある
|
|
37
|
+
# - 終端を必ず含む
|
|
38
|
+
# - 値オブジェクト風
|
|
39
|
+
# - start end は外部から更新できるものの破壊的メソッドはない
|
|
40
|
+
|
|
41
|
+
#+title2: コンストラクタ
|
|
42
|
+
|
|
43
|
+
#+title4: 基本
|
|
44
|
+
|
|
45
|
+
Range2.new(1, 2) # => (1 -> 2)
|
|
46
|
+
|
|
47
|
+
#+title4: pos を中心に半径 len / 2 の幅とする
|
|
48
|
+
|
|
49
|
+
Range2::from_pos_and_len(100.0, 10.0) # => (95.0 -> 105.0)
|
|
50
|
+
|
|
51
|
+
#+title2: それぞれの値
|
|
52
|
+
|
|
53
|
+
Range2.new(100.0, 200.0).start # => 100.0
|
|
54
|
+
Range2.new(100.0, 200.0).middle # => 150.0
|
|
55
|
+
Range2.new(100.0, 200.0).end # => 200.0
|
|
56
|
+
|
|
57
|
+
#+title2: ベクトルの強さと長さ
|
|
58
|
+
|
|
59
|
+
#+title4: 強さ (end - start)
|
|
60
|
+
|
|
61
|
+
Range2.new(100, -200).magnitude # => -300
|
|
62
|
+
|
|
63
|
+
#+title4: 長さ (強さの絶対値)
|
|
64
|
+
|
|
65
|
+
Range2.new(100, -200).length # => 300
|
|
66
|
+
|
|
67
|
+
#+title2: 小数の補正
|
|
68
|
+
|
|
69
|
+
Range2.new(0.4, 0.5).round # => (0 -> 1)
|
|
70
|
+
Range2.new(0.4, 0.5).floor # => (0 -> 0)
|
|
71
|
+
Range2.new(0.4, 0.5).ceil # => (1 -> 1)
|
|
72
|
+
Range2.new(0.4, 0.5).truncate # => (0 -> 0)
|
|
73
|
+
|
|
74
|
+
#+title2: 範囲
|
|
75
|
+
|
|
76
|
+
#+title4: OR (向きを破壊する)
|
|
77
|
+
|
|
78
|
+
a = Range2.new(5.0, 3.0)
|
|
79
|
+
b = Range2.new(4.0, 6.0)
|
|
80
|
+
a.max(b) # => (3.0 -> 6.0)
|
|
81
|
+
|
|
82
|
+
#+title4: OR (向きを維持する)
|
|
83
|
+
|
|
84
|
+
a = Range2.new(5.0, 3.0)
|
|
85
|
+
b = Range2.new(4.0, 6.0)
|
|
86
|
+
a.max_directed(b) # => (6.0 -> 3.0)
|
|
87
|
+
|
|
88
|
+
# a が右向きなら max と同じだが左向きなら max の invert になる。
|
|
89
|
+
|
|
90
|
+
#+title4: AND (向きを破壊する)
|
|
91
|
+
|
|
92
|
+
a = Range2.new(5.0, 3.0)
|
|
93
|
+
b = Range2.new(4.0, 6.0)
|
|
94
|
+
a.overlap(b) # => (4.0 -> 5.0)
|
|
95
|
+
|
|
96
|
+
#+title2: 向き
|
|
97
|
+
|
|
98
|
+
#+title4: 現在の向きを返す
|
|
99
|
+
|
|
100
|
+
Range2.new(0, 10).direction # => 1.0
|
|
101
|
+
Range2.new(10, 0).direction # => -1.0
|
|
102
|
+
Range2.new(10, 10).direction # => 0.0
|
|
103
|
+
|
|
104
|
+
#+title4: 向きが同じか?
|
|
105
|
+
|
|
106
|
+
a = Range2.new(1, 2)
|
|
107
|
+
b = Range2.new(3, 4)
|
|
108
|
+
a.same_direction?(b) # => true
|
|
109
|
+
|
|
110
|
+
#+title4: 向きを反転する
|
|
111
|
+
|
|
112
|
+
Range2.new(0, 100).invert # => (100 -> 0)
|
|
113
|
+
|
|
114
|
+
#+title4: 正の向きにする
|
|
115
|
+
|
|
116
|
+
Range2.new(10, 0).absolute # => (0 -> 10)
|
|
117
|
+
|
|
118
|
+
# `start > end` なら invert する。
|
|
119
|
+
|
|
120
|
+
#+title2: スケーリング
|
|
121
|
+
|
|
122
|
+
a = Range2.new(0.0, 1.0)
|
|
123
|
+
b = Range2.new(0.0, 100.0)
|
|
124
|
+
a.map_value(0.9, b) # => 90.0
|
|
125
|
+
|
|
126
|
+
# a の 0.9 は b では 90 になる。
|
|
127
|
+
|
|
128
|
+
# 元の範囲が 0..1 の場合 lerp 使うと簡潔に書ける。
|
|
129
|
+
|
|
130
|
+
b.lerp(0.9) # => 90.0
|
|
131
|
+
|
|
132
|
+
#+title2: 指定の軸で整列する
|
|
133
|
+
|
|
134
|
+
#+title4: 相手の左端に揃える
|
|
135
|
+
|
|
136
|
+
a = Range2.new(0, 100)
|
|
137
|
+
b = Range2.new(50, 100)
|
|
138
|
+
a.align_start_of(b) # => (50 -> 150)
|
|
139
|
+
|
|
140
|
+
#+title4: 相手の右端に揃える
|
|
141
|
+
|
|
142
|
+
a = Range2.new(0, 50)
|
|
143
|
+
b = Range2.new(0, 100)
|
|
144
|
+
a.align_end_of(b) # => (50 -> 100)
|
|
145
|
+
|
|
146
|
+
#+title4: 相手の中央に揃える
|
|
147
|
+
|
|
148
|
+
a = Range2.new(0.0, 50.0)
|
|
149
|
+
b = Range2.new(0.0, 100.0)
|
|
150
|
+
a.align_middle_of(b) # => (25.0 -> 75.0)
|
|
151
|
+
|
|
152
|
+
#+title4: 相手のどこかに揃える
|
|
153
|
+
|
|
154
|
+
a = Range2.new(0.0, 5.0)
|
|
155
|
+
b = Range2.new(10.0, 20.0)
|
|
156
|
+
a.align_to(:start, b) # => (10.0 -> 15.0)
|
|
157
|
+
a.align_to(:end, b) # => (15.0 -> 20.0)
|
|
158
|
+
a.align_to(:middle, b) # => (12.5 -> 17.5)
|
|
159
|
+
|
|
160
|
+
#+title2: 横に並べる
|
|
161
|
+
|
|
162
|
+
#+title4: 相手の左隣り並べる
|
|
163
|
+
|
|
164
|
+
a = Range2.new(0.0, 5.0)
|
|
165
|
+
b = Range2.new(0.0, 10.0)
|
|
166
|
+
a.align_after(b) # => (10.0 -> 15.0)
|
|
167
|
+
|
|
168
|
+
#+title4: 相手の右隣り並べる
|
|
169
|
+
|
|
170
|
+
a = Range2.new(0.0, 5.0)
|
|
171
|
+
b = Range2.new(0.0, 0.0)
|
|
172
|
+
a.align_before(b) # => (-5.0 -> 0.0)
|
|
173
|
+
|
|
174
|
+
#+title2: Edge を寄せる (サイズが変わる)
|
|
175
|
+
|
|
176
|
+
#+title4: 左端を内側に寄せる
|
|
177
|
+
|
|
178
|
+
Range2.new(10, 0).pad_start(3) # => (7 -> 0)
|
|
179
|
+
|
|
180
|
+
#+title4: 右端を内側に寄せる
|
|
181
|
+
|
|
182
|
+
Range2.new(10, 0).pad_end(3) # => (10 -> 3)
|
|
183
|
+
|
|
184
|
+
#+title4: 両端を内側に寄せる
|
|
185
|
+
|
|
186
|
+
Range2.new(10, 0).pad(3) # => (7 -> 3)
|
|
187
|
+
|
|
188
|
+
#+title4: 両端を内側に寄せる (個別指定)
|
|
189
|
+
|
|
190
|
+
Range2.new(10, 0).pad_ends(3, 4) # => (7 -> 4)
|
|
191
|
+
|
|
192
|
+
#+title2: この範囲に含むか?
|
|
193
|
+
|
|
194
|
+
Range2.new(1, 2).contains?(2) # => true
|
|
195
|
+
|
|
196
|
+
#+title2: 対象を補正する
|
|
197
|
+
|
|
198
|
+
Range2.new(10, 0).clamp_value(-1) # => 0
|
|
199
|
+
Range2.new(10, 0).clamp_value(11) # => 10
|
|
200
|
+
|
|
201
|
+
#+title2: ずらす (サイズ不変)
|
|
202
|
+
|
|
203
|
+
Range2.new(2, 3).shift(10) # => (12 -> 13)
|
|
204
|
+
|
|
205
|
+
#+title2: 近い方の端を引き伸ばす
|
|
206
|
+
|
|
207
|
+
Range2.new(10, 20).stretch_to_value(5) # => (5 -> 20)
|
|
208
|
+
Range2.new(10, 20).stretch_to_value(25) # => (10 -> 25)
|
|
209
|
+
|
|
210
|
+
# 範囲内を指定した場合は何も変化しない。
|
|
211
|
+
|
|
212
|
+
Range2.new(10, 20).stretch_to_value(15) # => (10 -> 20)
|
|
213
|
+
|
|
214
|
+
#+title2: 値に近い方の Edge を返す
|
|
215
|
+
|
|
216
|
+
Range2.new(0.0, 10.0).closest_edge(4.0) # => :start
|
|
217
|
+
Range2.new(0.0, 10.0).closest_edge(6.0) # => :end
|
data/workbench/rect.rb
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#+hidden: true
|
|
2
|
+
require "./setup"
|
|
3
|
+
|
|
4
|
+
#+title2: 早見表
|
|
5
|
+
|
|
6
|
+
# | 種類 | 意味 | Methods | 同類・補足 |
|
|
7
|
+
# |--------+--------------------+--------------------+-----------------------------------------------|
|
|
8
|
+
# | 生成 | x, y, w, h から | from_x_y_w_h | from_xy_wh |
|
|
9
|
+
# | 生成 | w, h から | from_w_h | from_wh |
|
|
10
|
+
# | 生成 | 対角から | from_corners | |
|
|
11
|
+
# | 参照 | 個別 | x y w h | |
|
|
12
|
+
# | 参照 | 配列 | x_y w_h | |
|
|
13
|
+
# | 参照 | ベクトル型 | xy wh | xy_wh |
|
|
14
|
+
# | 参照 | まとめて | l_r_b_t | l_t_w_h l_b_w_h |
|
|
15
|
+
# | 参照 | 十字の先端 | mid_left | mid_top mid_right mid_bottom |
|
|
16
|
+
# | 参照 | 辺 | left | right bottom top |
|
|
17
|
+
# | 角 | 個別 | top_left | top_right bottom_left bottom_right |
|
|
18
|
+
# | 角 | まとめて | corners | |
|
|
19
|
+
# | 角 | 座標に近い角の名前 | closest_corner(xy) | stretch_to_point の影響を受ける角が分かる |
|
|
20
|
+
# | 領域 | 上下左右 | subdivision_ranges | subdivisions |
|
|
21
|
+
# | 領域 | 対角切断時の三角形 | triangles | |
|
|
22
|
+
# | 対領域 | 相手との AND | a.overlap(b) | |
|
|
23
|
+
# | 対領域 | 相手との OR | a.max(b) | |
|
|
24
|
+
# | 移動 | 相手の軸の ? に | a.align_x_of(?, b) | align_y_of align_middle_of |
|
|
25
|
+
# | 移動 | 相手の内側の辺に | a.mid_top_of(b) | mid_bottom_of mid_left_of mid_right_of |
|
|
26
|
+
# | 移動 | 相手の中心に | a.middle_of(b) | |
|
|
27
|
+
# | 移動 | 相手の辺の外に | left_of(b) | right_of below above |
|
|
28
|
+
# | 移動 | 相手の辺の内に | align_left_of(b) | align_right_of align_bottom_of align_top_of |
|
|
29
|
+
# | 移動 | 相手の角の内に | top_left_of(b) | top_right_of bottom_left_of bottom_right_of |
|
|
30
|
+
# | 変形 | 軸を動かす | shift(vec) | shift_x shift_y |
|
|
31
|
+
# | 変形 | 指定座標を覆う | stretch_to(vec) | stretch_to_point(vec) |
|
|
32
|
+
# | 変形 | 縮小 | pad | pad_left pad_right pad_bottom pad_top padding |
|
|
33
|
+
# | 向き | 反転 | invert_x | invert_y |
|
|
34
|
+
# | 向き | 正にする | absolute | |
|
|
35
|
+
# | その他 | 座標が含まれるか? | contains?(vec) | |
|
|
36
|
+
# | その他 | 相対的な領域を返す | relative_to(xy) | relative_to_x relative_to_y |
|
|
37
|
+
|
|
38
|
+
#+title2: コンストラクタ
|
|
39
|
+
|
|
40
|
+
#+title5: x, y, w, h から作る
|
|
41
|
+
|
|
42
|
+
Rect.from_xy_wh(V[0.0, 0.0], V[10.0, 10.0]) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
43
|
+
Rect.from_x_y_w_h(0.0, 0.0, 100.0, 100.0) # => ((-50.0 -> 50.0), (-50.0 -> 50.0))
|
|
44
|
+
|
|
45
|
+
#+title5: w, h から作る
|
|
46
|
+
|
|
47
|
+
Rect.from_wh(V[10.0, 10.0]) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
48
|
+
Rect.from_w_h(100.0, 100.0) # => ((-50.0 -> 50.0), (-50.0 -> 50.0))
|
|
49
|
+
|
|
50
|
+
#+title5: 対角から作る
|
|
51
|
+
|
|
52
|
+
Rect.from_corners(V[-10, 10], V[-10, 10]) # => ((-10 -> -10), (10 -> 10))
|
|
53
|
+
|
|
54
|
+
#+title2: 基本的な値の取得
|
|
55
|
+
|
|
56
|
+
a = Rect.from_x_y_w_h(10.0, 20.0, 100.0, 100.0)
|
|
57
|
+
|
|
58
|
+
#+title5: x, y
|
|
59
|
+
|
|
60
|
+
[a.x_middle, a.y_middle] # => [10.0, 20.0]
|
|
61
|
+
a.x_y # => [10.0, 20.0]
|
|
62
|
+
a.xy # => (10.0, 20.0)
|
|
63
|
+
|
|
64
|
+
#+title5: w, h
|
|
65
|
+
|
|
66
|
+
[a.w, a.h] # => [100.0, 100.0]
|
|
67
|
+
a.w_h # => [100.0, 100.0]
|
|
68
|
+
a.wh # => (100.0, 100.0)
|
|
69
|
+
|
|
70
|
+
#+title5: x, y, w, h
|
|
71
|
+
|
|
72
|
+
a.xy_wh # => [(10.0, 20.0), (100.0, 100.0)]
|
|
73
|
+
|
|
74
|
+
#+title5: 角
|
|
75
|
+
|
|
76
|
+
a.top_left # => (-40.0, 70.0)
|
|
77
|
+
a.top_right # => (60.0, 70.0)
|
|
78
|
+
a.bottom_left # => (-40.0, -30.0)
|
|
79
|
+
a.bottom_right # => (60.0, -30.0)
|
|
80
|
+
|
|
81
|
+
#+title5: 辺の中央
|
|
82
|
+
|
|
83
|
+
a.mid_left # => (-40.0, 20.0)
|
|
84
|
+
a.mid_top # => (10.0, 70.0)
|
|
85
|
+
a.mid_right # => (60.0, 20.0)
|
|
86
|
+
a.mid_bottom # => (10.0, -30.0)
|
|
87
|
+
|
|
88
|
+
#+title5: 辺
|
|
89
|
+
|
|
90
|
+
a.left # => -40.0
|
|
91
|
+
a.right # => 60.0
|
|
92
|
+
a.bottom # => -30.0
|
|
93
|
+
a.top # => 70.0
|
|
94
|
+
|
|
95
|
+
#+title5: まとめて
|
|
96
|
+
|
|
97
|
+
a.l_r_b_t # => [-40.0, 60.0, -30.0, 70.0]
|
|
98
|
+
a.l_t_w_h # => [-40.0, 70.0, 100.0, 100.0]
|
|
99
|
+
a.l_b_w_h # => [-40.0, -30.0, 100.0, 100.0]
|
|
100
|
+
|
|
101
|
+
#+title2: 整列
|
|
102
|
+
|
|
103
|
+
#+title5: a を b の座標の align に揃える
|
|
104
|
+
|
|
105
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
106
|
+
b = Rect.from_wh(V[100.0, 100.0])
|
|
107
|
+
a.align_x_of(:start, b) # => ((-50.0 -> -40.0), (-5.0 -> 5.0))
|
|
108
|
+
a.align_x_of(:middle, b) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
109
|
+
a.align_x_of(:end, b) # => ((40.0 -> 50.0), (-5.0 -> 5.0))
|
|
110
|
+
|
|
111
|
+
a.align_y_of(:start, b) # => ((-5.0 -> 5.0), (-50.0 -> -40.0))
|
|
112
|
+
a.align_y_of(:middle, b) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
113
|
+
a.align_y_of(:end, b) # => ((-5.0 -> 5.0), (40.0 -> 50.0))
|
|
114
|
+
|
|
115
|
+
# `a.align_x_of(:middle, b)` と `a.align_y_of(:middle, b)` のショートカット:
|
|
116
|
+
|
|
117
|
+
a.align_middle_x_of(b) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
118
|
+
a.align_middle_y_of(b) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
119
|
+
|
|
120
|
+
# a を b の**内側**の上下左右の辺にくっつける
|
|
121
|
+
|
|
122
|
+
a.mid_top_of(b) # => ((-5.0 -> 5.0), (40.0 -> 50.0))
|
|
123
|
+
a.mid_bottom_of(b) # => ((-5.0 -> 5.0), (-50.0 -> -40.0))
|
|
124
|
+
a.mid_left_of(b) # => ((-50.0 -> -40.0), (-5.0 -> 5.0))
|
|
125
|
+
a.mid_right_of(b) # => ((40.0 -> 50.0), (-5.0 -> 5.0))
|
|
126
|
+
|
|
127
|
+
# a を b の中心に配置する
|
|
128
|
+
|
|
129
|
+
a.middle_of(b) # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
130
|
+
|
|
131
|
+
#+title2: 中央で十字に区切る
|
|
132
|
+
|
|
133
|
+
a = Rect.from_x_y_w_h(200.0, 300.0, 10.0, 10.0)
|
|
134
|
+
|
|
135
|
+
# 座標
|
|
136
|
+
|
|
137
|
+
b = a.subdivision_ranges
|
|
138
|
+
b.x_a # => (195.0 -> 200.0)
|
|
139
|
+
b.x_b # => (200.0 -> 205.0)
|
|
140
|
+
b.y_a # => (295.0 -> 300.0)
|
|
141
|
+
b.y_b # => (300.0 -> 305.0)
|
|
142
|
+
|
|
143
|
+
# 領域
|
|
144
|
+
|
|
145
|
+
a.subdivisions[0] # => ((195.0 -> 200.0), (295.0 -> 300.0))
|
|
146
|
+
a.subdivisions[1] # => ((200.0 -> 205.0), (295.0 -> 300.0))
|
|
147
|
+
a.subdivisions[2] # => ((195.0 -> 200.0), (300.0 -> 305.0))
|
|
148
|
+
a.subdivisions[3] # => ((200.0 -> 205.0), (300.0 -> 305.0))
|
|
149
|
+
|
|
150
|
+
#+title2: 内部の方向を正にする
|
|
151
|
+
|
|
152
|
+
a = Rect.new(Range2.new(1.0, -1.0), Range2.new(1.0, -1.0))
|
|
153
|
+
a # => ((1.0 -> -1.0), (1.0 -> -1.0))
|
|
154
|
+
a.absolute # => ((-1.0 -> 1.0), (-1.0 -> 1.0))
|
|
155
|
+
|
|
156
|
+
# 同じ領域でも右下から左上の向きになっている場合がある。それを左上から右下方向に直す。
|
|
157
|
+
|
|
158
|
+
#+title2: AND 領域
|
|
159
|
+
|
|
160
|
+
a = Rect.from_x_y_w_h(100.0, 100.0, 100.0, 100.0)
|
|
161
|
+
b = Rect.from_x_y_w_h(150.0, 150.0, 100.0, 100.0)
|
|
162
|
+
a.overlap(b) # => ((100.0 -> 150.0), (100.0 -> 150.0))
|
|
163
|
+
|
|
164
|
+
#+title2: OR 領域
|
|
165
|
+
|
|
166
|
+
a = Rect.from_x_y_w_h(100.0, 100.0, 100.0, 100.0)
|
|
167
|
+
b = Rect.from_x_y_w_h(150.0, 150.0, 100.0, 100.0)
|
|
168
|
+
a.max(b) # => ((50.0 -> 200.0), (50.0 -> 200.0))
|
|
169
|
+
|
|
170
|
+
#+title2: x y をそれぞれ移動
|
|
171
|
+
|
|
172
|
+
a = Rect.from_wh(V[100.0, 100.0])
|
|
173
|
+
a # => ((-50.0 -> 50.0), (-50.0 -> 50.0))
|
|
174
|
+
a.shift_x(25.0) # => ((-25.0 -> 75.0), (-50.0 -> 50.0))
|
|
175
|
+
a.shift_y(25.0) # => ((-50.0 -> 50.0), (-25.0 -> 75.0))
|
|
176
|
+
|
|
177
|
+
#+warn: x, y の一方だけをずらすため元の形が崩れる
|
|
178
|
+
|
|
179
|
+
#+title2: 相手のどこかに移動する
|
|
180
|
+
|
|
181
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
182
|
+
b = Rect.from_wh(V[100.0, 100.0])
|
|
183
|
+
|
|
184
|
+
#+title5: 辺の外側 (below: 下, above: 上)
|
|
185
|
+
|
|
186
|
+
a.left_of(b) # => ((-60.0 -> -50.0), (-5.0 -> 5.0))
|
|
187
|
+
a.right_of(b) # => ((50.0 -> 60.0), (-5.0 -> 5.0))
|
|
188
|
+
a.below(b) # => ((-5.0 -> 5.0), (-60.0 -> -50.0))
|
|
189
|
+
a.above(b) # => ((-5.0 -> 5.0), (50.0 -> 60.0))
|
|
190
|
+
|
|
191
|
+
#+title5: 辺の内側
|
|
192
|
+
|
|
193
|
+
a.align_left_of(b) # => ((-50.0 -> -40.0), (-5.0 -> 5.0))
|
|
194
|
+
a.align_right_of(b) # => ((40.0 -> 50.0), (-5.0 -> 5.0))
|
|
195
|
+
a.align_bottom_of(b) # => ((-5.0 -> 5.0), (-50.0 -> -40.0))
|
|
196
|
+
a.align_top_of(b) # => ((-5.0 -> 5.0), (40.0 -> 50.0))
|
|
197
|
+
|
|
198
|
+
#+title5: 角の内側
|
|
199
|
+
|
|
200
|
+
a.top_left_of(b) # => ((-50.0 -> -40.0), (40.0 -> 50.0))
|
|
201
|
+
a.top_right_of(b) # => ((40.0 -> 50.0), (40.0 -> 50.0))
|
|
202
|
+
a.bottom_left_of(b) # => ((-50.0 -> -40.0), (-50.0 -> -40.0))
|
|
203
|
+
a.bottom_right_of(b) # => ((40.0 -> 50.0), (-50.0 -> -40.0))
|
|
204
|
+
|
|
205
|
+
#+title2: 指定の座標を含むように近い方の辺を広げる
|
|
206
|
+
|
|
207
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
208
|
+
a.stretch_to_point(V[6.0, 6.0]) # => ((-5.0 -> 6.0), (-5.0 -> 6.0))
|
|
209
|
+
|
|
210
|
+
#+warn: 比率が壊れる
|
|
211
|
+
|
|
212
|
+
#+title2: 指定の座標にいちばん近い角の名前を返す
|
|
213
|
+
|
|
214
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
215
|
+
a.closest_corner(V[1.0, 1.0]) # => :top_right
|
|
216
|
+
a.closest_corner(V[-1.0, -1.0]) # => :bottom_left
|
|
217
|
+
a.closest_corner(V[-1.0, 1.0]) # => :top_left
|
|
218
|
+
a.closest_corner(V[1.0, -1.0]) # => :bottom_right
|
|
219
|
+
|
|
220
|
+
#+title2: 角の座標を返す
|
|
221
|
+
|
|
222
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
223
|
+
a.corners # => [(-5.0, 5.0), (5.0, 5.0), (5.0, -5.0), (-5.0, -5.0)]
|
|
224
|
+
|
|
225
|
+
#+title2: 左上から右下に切ってできる2つの三角形を返す
|
|
226
|
+
|
|
227
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
228
|
+
a.triangles[0] # => [(-5.0, 5.0), (5.0, 5.0), (5.0, -5.0)]
|
|
229
|
+
a.triangles[1] # => [(-5.0, 5.0), (5.0, -5.0), (-5.0, -5.0)]
|
|
230
|
+
|
|
231
|
+
#+title2: x, y をまとめて移動
|
|
232
|
+
|
|
233
|
+
a = Rect.from_wh(V[100.0, 100.0])
|
|
234
|
+
a.shift(V[10.0, 10.0]) # => ((-40.0 -> 60.0), (-40.0 -> 60.0))
|
|
235
|
+
|
|
236
|
+
#+title2: 指定の座標が含まれるか?
|
|
237
|
+
|
|
238
|
+
a = Rect.from_wh(V[100.0, 100.0])
|
|
239
|
+
a.contains?(V[0.0, 50.0]) # => true
|
|
240
|
+
a.contains?(V[0.0, 51.0]) # => false
|
|
241
|
+
|
|
242
|
+
#+title2: 指定の座標が含まれるまで近い方を伸ばす
|
|
243
|
+
|
|
244
|
+
a = Rect.from_wh(V[2.0, 2.0])
|
|
245
|
+
a.stretch_to(V[5.0, 5.0]) # => ((-1.0 -> 5.0), (-1.0 -> 5.0))
|
|
246
|
+
|
|
247
|
+
#+title2: 領域を内側に縮小する
|
|
248
|
+
|
|
249
|
+
a = Rect.from_wh(V[100.0, 100.0])
|
|
250
|
+
a.pad_left(10.0) # => ((-40.0 -> 50.0), (-50.0 -> 50.0))
|
|
251
|
+
a.pad_right(10.0) # => ((-50.0 -> 40.0), (-50.0 -> 50.0))
|
|
252
|
+
a.pad_bottom(10.0) # => ((-50.0 -> 50.0), (-40.0 -> 50.0))
|
|
253
|
+
a.pad_top(10.0) # => ((-50.0 -> 50.0), (-50.0 -> 40.0))
|
|
254
|
+
a.pad(10.0) # => ((-40.0 -> 40.0), (-40.0 -> 40.0))
|
|
255
|
+
b = Rect.new(Range2.new(10.0, 10.0), Range2.new(10.0, 10.0))
|
|
256
|
+
a.padding(b) # => ((-40.0 -> 40.0), (-40.0 -> 40.0))
|
|
257
|
+
|
|
258
|
+
#+title2: 相対的な範囲を返す
|
|
259
|
+
|
|
260
|
+
a = Rect.from_wh(V[10.0, 10.0])
|
|
261
|
+
a # => ((-5.0 -> 5.0), (-5.0 -> 5.0))
|
|
262
|
+
a.relative_to_x(10.0) # => ((-15.0 -> -5.0), (-5.0 -> 5.0))
|
|
263
|
+
a.relative_to_y(10.0) # => ((-5.0 -> 5.0), (-15.0 -> -5.0))
|
|
264
|
+
a.relative_to(V[10.0, 10.0]) # => ((-15.0 -> -5.0), (-15.0 -> -5.0))
|
|
265
|
+
|
|
266
|
+
#+title2: 反転
|
|
267
|
+
|
|
268
|
+
a = Rect.from_wh(V[2.0, 2.0])
|
|
269
|
+
a # => ((-1.0 -> 1.0), (-1.0 -> 1.0))
|
|
270
|
+
a.invert_x # => ((1.0 -> -1.0), (-1.0 -> 1.0))
|
|
271
|
+
a.invert_y # => ((-1.0 -> 1.0), (1.0 -> -1.0))
|