Class: Miyako::SegmentStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/Miyako/API/struct_segment.rb

Overview

線分の区間情報のための構造体クラス

位置変更メソッドを追加

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

インスタンスのかけ算

もう一方が整数のとき、x,yにotherを掛けたものを返す Point構造体や配列など、[]メソッドがつかえるもののとき、x,y同士を掛けたものを返す それ以外の時は例外が発生する 自分自身の値は変わらない

other

整数もしくはPoint構造体

返却値

Point構造体



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/Miyako/API/struct_segment.rb', line 183

def *(other)
  ret = self.dup
  if other.kind_of?(Numeric)
    ret[0] *= other
    ret[1] *= other
  elsif other.methods.include?(:[])
    ret[0] *= other[0]
    ret[1] *= other[1]
  else
    raise MiyakoError, "this parameter cannot access!"
  end
  ret
end

#+(other) ⇒ Object

インスタンスの足し算

もう一方が整数のとき、x,yにotherを足したものを返す Point構造体や配列など、[]メソッドがつかえるもののとき、x,y同士を足したものを返す それ以外の時は例外が発生する 自分自身の値は変わらない

other

整数もしくはPoint構造体

返却値

Point構造体



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/Miyako/API/struct_segment.rb', line 141

def +(other)
  ret = self.dup
  if other.kind_of?(Numeric)
    ret[0] += other
    ret[1] += other
  elsif other.methods.include?(:[])
    ret[0] += other[0]
    ret[1] += other[1]
  else
    raise MiyakoError, "this parameter cannot access!"
  end
  ret
end

#-(other) ⇒ Object

インスタンスの引き算

もう一方が整数のとき、x,yからotherを引いたものを返す Point構造体や配列など、[]メソッドがつかえるもののとき、x,y同士を引いたものを返す それ以外の時は例外が発生する 自分自身の値は変わらない

other

整数もしくはPoint構造体

返却値

Point構造体



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/Miyako/API/struct_segment.rb', line 162

def -(other)
  ret = self.dup
  if other.kind_of?(Numeric)
    ret[0] -= other
    ret[1] -= other
  elsif other.methods.include?(:[])
    ret[0] -= other[0]
    ret[1] -= other[1]
  else
    raise MiyakoError, "this parameter cannot access!"
  end
  ret
end

#/(other) ⇒ Object

インスタンスの割り算

もう一方が整数のとき、x,yからotherを割ったものを返す Point構造体や配列など、[]メソッドがつかえるもののとき、x,y同士を割ったものを返す それ以外の時は例外が発生する 自分自身の値は変わらない

other

整数もしくはPoint構造体

返却値

Point構造体



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/Miyako/API/struct_segment.rb', line 204

def /(other)
  ret = self.dup
  if other.kind_of?(Numeric)
    raise MiyakoValueError, "0 div!" if other == 0
    ret[0] /= other
    ret[1] /= other
  elsif other.methods.include?(:[])
    raise MiyakoValueError, "0 div!" if (other[0] == 0 || other[1] == 0)
    ret[0] /= other[0]
    ret[1] /= other[1]
  else
    raise MiyakoError, "this parameter cannot access!"
  end
  ret
end

#between?(v) ⇒ Boolean

値が線分の範囲内かどうかを判別する

値がminとmaxの値の範囲内にあるかどうかを判別する。範囲内にあればtrueを返す 値がminもしくはmaxに等しいときもtrueを返す

v

判別する値

返却値

範囲内のときはtrue、範囲外の時はfalseを返す

Returns:

  • (Boolean)


36
37
38
# File 'lib/Miyako/API/struct_segment.rb', line 36

def between?(other)
  return self[0] >= other[0] && self[1] <= other[1]
end

#collision?(other) ⇒ Boolean

レシーバがotherの範囲と重なっているかどうかを求める

最小数・最大数のどちらかがotherの範囲内かどうかを確認する

other

比較対象の線分

返却値

最小値・最大値のどちらかがotherの範囲内ならばtrueを返す

Returns:

  • (Boolean)


52
53
54
# File 'lib/Miyako/API/struct_segment.rb', line 52

def collision?(other)
  return (self[0] >= other[0] && self[0] <= other[1]) || (self[1] >= other[0] && self[1] <= other[1])
end

#covers?(other) ⇒ Boolean

otherがレシーバの範囲内かどうかを求める

最小数・最大数ともにotherの範囲外かどうかを確認する

other

比較対象の線分

返却値

最小値・最大値ともにotherの範囲外ならばtrueを返す

Returns:

  • (Boolean)


44
45
46
# File 'lib/Miyako/API/struct_segment.rb', line 44

def covers?(other)
  return self[0] <= other[0] && self[1] >= other[1]
end

#in_bounds?(big_segment, d, flag = false) ⇒ Boolean

小線分を移動させたとき、大線分が範囲内かどうかを判別する

移動後の小線分が大線分の範囲内にあるかどうかをtrue/falseで取得する

big_segment

大線分の範囲。Segment構造体、もしくはで構成された配列

d

selfの移動量

flag

大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse

返却値

範囲内のときはtrue、範囲外の時はfalseを返す

Returns:

  • (Boolean)


287
288
289
290
291
292
293
294
# File 'lib/Miyako/API/struct_segment.rb', line 287

def in_bounds?(big_segment, d, flag = false)
  nx = self[0] + d
  nx2 = self[1] + d
  nx, nx2 = nx2, nx if nx > nx2
  return flag ?
         (nx >= big_segment[0] && nx2 <= big_segment[1]) :
         (nx > big_segment[0] && (nx2 - 1) < big_segment[1])
end

#in_bounds_ex?(big_segment, d, flag = false) ⇒ Boolean

小線分を移動させたとき、大線分が範囲内かどうかを判別して、その状態によって値を整数で返す

移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは-1、 プラス方向で出るときは1を返す

big_segment

大線分の範囲。Segment構造体、もしくはで構成された配列

d

selfの移動量

flag

大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse

返却値

判別の結果

Returns:

  • (Boolean)


304
305
306
307
308
309
310
# File 'lib/Miyako/API/struct_segment.rb', line 304

def in_bounds_ex?(big_segment, d, flag = false)
  nx = self[0] + d
  nx2 = self[1] + d - 1
  nx, nx2 = nx2, nx if nx > nx2
  return -1 if (nx < big_segment[0]) || (flag && (nx == big_segment[0]))
  return (nx2 > big_segment[1]) || (flag && (nx2 == big_segment[1])) ? 1 : 0
end

#in_bounds_rev?(big_segment, d, flag = false) ⇒ Boolean

移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す

移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは1、 プラス方向で出るときは-1を返す

big_segment

大線分の範囲。Segment構造体、もしくはで構成された配列

d

selfの移動量

flag

大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse

返却値

判別の結果

Returns:

  • (Boolean)


320
321
322
323
324
325
326
# File 'lib/Miyako/API/struct_segment.rb', line 320

def in_bounds_rev?(big_segment, d, flag = false)
  nx = self[0] + d
  nx2 = self[1] + d - 1
  nx, nx2 = nx2, nx if nx > nx2
  return 1 if (nx < big_segment[0]) || (flag && (nx == big_segment[0]))
  return (nx2 > big_segment[1]) || (flag && (nx2 == big_segment[1])) ? -1 : 0
end

#in_bounds_rev_ex?(big_segment, d, flag = false) ⇒ Boolean

移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す

移動量が0のときは0、 移動後の小線分の範囲が大線分の範囲内のときは1、 範囲外に出るときは-1を返す

big_segment

大線分の範囲。Segment構造体、もしくはで構成された配列

d

selfの移動量

flag

大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse

返却値

判別の結果

Returns:

  • (Boolean)


336
337
338
339
340
341
342
343
344
# File 'lib/Miyako/API/struct_segment.rb', line 336

def in_bounds_rev_ex?(big_segment, d, flag = false)
  return 0 if d == 0
  dir = (d <=> 0)
  nx = self[0] + d
  nx2 = self[1] + d - 1
  nx, nx2 = nx2, nx if nx > nx2
  return -dir if (nx < big_segment[0]) || (flag && (nx == big_segment[0]))
  return (nx2 > big_segment[1]) || (flag && (nx2 == big_segment[1])) ? -dir : dir
end

#in_edge?(v) ⇒ Boolean

値が線分の端かどうかを判別する

値がminもしくはmaxと等しければtrueを返す

v

判別する値

返却値

vがminもしくはmaxと等しければtrue、それ以外の時はfalseを返す

Returns:

  • (Boolean)


261
262
263
# File 'lib/Miyako/API/struct_segment.rb', line 261

def in_edge?(v)
  v == self[0] || v == self[1]
end

#in_range?(v) ⇒ Boolean

値が線分の範囲内かどうかを判別する

値がminとmaxの値の範囲内にあるかどうかを判別する。範囲内にあればtrueを返す 値がminもしくはmaxに等しいときもtrueを返す

v

判別する値

返却値

範囲内のときはtrue、範囲外の時はfalseを返す

Returns:

  • (Boolean)


244
245
246
# File 'lib/Miyako/API/struct_segment.rb', line 244

def in_range?(v)
  v >= self[0] && v <= self[1]
end

#max?(v) ⇒ Boolean

値が最大値かどうかを判別する

値がmaxと等しければtrueを返す

v

判別する値

返却値

vがmaxと等しければtrue、それ以外の時はfalseを返す

Returns:

  • (Boolean)


277
278
279
# File 'lib/Miyako/API/struct_segment.rb', line 277

def max?(v)
  v == self[1]
end

#min?(v) ⇒ Boolean

値が最小値かどうかを判別する

値がminと等しければtrueを返す

v

判別する値

返却値

vがminと等しければtrue、それ以外の時はfalseを返す

Returns:

  • (Boolean)


269
270
271
# File 'lib/Miyako/API/struct_segment.rb', line 269

def min?(v)
  v == self[0]
end

#move(d) ⇒ Object

位置を変更したインスタンスを返す(変化量を指定)

引数で指定したぶん移動させたときの位置を新しくインスタンスを生成して返す 自分自身の値は変わらない

d

移動量

返却値

自分自身の複製を更新したインスタンス



86
87
88
# File 'lib/Miyako/API/struct_segment.rb', line 86

def move(d)
  return self.dup.move!(d)
end

#move!(d) ⇒ Object

位置を変更する(変化量を指定)

d

移動量

返却値

自分自身を返す



66
67
68
69
70
# File 'lib/Miyako/API/struct_segment.rb', line 66

def move!(d)
  self[0] += d
  self[1] += d
  return self
end

#move_to(v) ⇒ Object

位置を変更したインスタンスを返す(位置指定)

引数で指定したぶん移動させたときの位置を新しくインスタンスを生成して返す 自分自身の値は変わらない

v

移動先位置。単位はピクセル

返却値

自分自身の複製を更新したインスタンス



95
96
97
# File 'lib/Miyako/API/struct_segment.rb', line 95

def move_to(v)
  return self.dup.move_to!(v)
end

#move_to!(v) ⇒ Object

位置を変更する(位置指定)

v

移動先位置。単位はピクセル

返却値

自分自身を返す



75
76
77
78
79
# File 'lib/Miyako/API/struct_segment.rb', line 75

def move_to!(v)
  self[1] = self[1] - self[0] + v
  self[0] = v
  return self
end

#reset!(min, max) ⇒ Object

線分情報を変更する

minとmaxを一緒に更新する min>maxのときは、それぞれの値を入れ替える

min

線分の最小値

max

線分の最大値

返却値

自分自身



226
227
228
229
230
# File 'lib/Miyako/API/struct_segment.rb', line 226

def reset!(min, max) #:nodoc:
  self[0], self[1] = min, max
  self[0], self[1] = self[1], self[0] if self[0] > self[1]
  self
end

#resize(d) ⇒ Object

サイズを変更したインスタンスを返す(変化量を指定)

引数で指定したぶん変えたときの大きさを新しくインスタンスを生成して返す 自分自身の値は変わらない

d

変更量。単位はピクセル

返却値

自分自身の複製を更新したインスタンス



121
122
123
# File 'lib/Miyako/API/struct_segment.rb', line 121

def resize(d)
  self.dup.resize!(d)
end

#resize!(d) ⇒ Object

幅を変更する(変化量を指定)

d

変更量。単位はピクセル

返却値

自分自身を返す



102
103
104
105
# File 'lib/Miyako/API/struct_segment.rb', line 102

def resize!(d)
  self[1] += d
  return self
end

#resize_to(v) ⇒ Object

サイズを変更したインスタンスを返す

引数で指定したぶん変えたときの大きさを新しくインスタンスを生成して返す 自分自身の値は変わらない

v

変更後の幅。単位はピクセル

返却値

自分自身の複製を更新したインスタンス



130
131
132
# File 'lib/Miyako/API/struct_segment.rb', line 130

def resize_to(v)
  self.dup.resize_to!(v)
end

#resize_to!(v) ⇒ Object

幅を変更する

ブロックを渡したとき、ブロックの評価した結果、偽になったときは移動させた値を元に戻す

v

変更後の幅。単位はピクセル

返却値

自分自身を返す



111
112
113
114
# File 'lib/Miyako/API/struct_segment.rb', line 111

def resize_to!(v)
  self[1] = self[0] + v - 1
  return self
end

#sizeObject

線分の大きさを求める

max - min + 1 の値を求めて返す

返却値

線分の大きさ



59
60
61
# File 'lib/Miyako/API/struct_segment.rb', line 59

def size
  self[1] - self[0] + 1
end

#to_aryObject

線分情報を配列に変換する

[min, max]の配列を生成して返す。

返却値

生成した配列



235
236
237
# File 'lib/Miyako/API/struct_segment.rb', line 235

def to_ary
  [self[0], self[1]]
end

#update!(obj) ⇒ Object



27
28
29
30
# File 'lib/Miyako/API/struct_segment.rb', line 27

def update!(obj)
  self[0] = obj[0]
  self[1] = obj[1]
end