Class: Miyako::SquareStruct

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

Overview

Square構造体用クラス

位置変更メソッドを追加

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

インスタンスのかけ算

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

other

整数もしくはPoint構造体

返却値

Point構造体



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/Miyako/API/struct_square.rb', line 192

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

#+(other) ⇒ Object

インスタンスの足し算

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

other

整数もしくはPoint構造体

返却値

Point構造体



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/Miyako/API/struct_square.rb', line 142

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

#-(other) ⇒ Object

インスタンスの引き算

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

other

整数もしくはPoint構造体

返却値

Point構造体



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/Miyako/API/struct_square.rb', line 167

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

#/(other) ⇒ Object

インスタンスの割り算

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

other

整数もしくはPoint構造体

返却値

Point構造体



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/Miyako/API/struct_square.rb', line 219

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

#between?(x, y) ⇒ Boolean

指定の座標が矩形の範囲内かを問い合わせる

x

指定のx座標

y

指定のy座標

返却値

座標が矩形の範囲内ならtrueを返す

Returns:

  • (Boolean)


253
254
255
# File 'lib/Miyako/API/struct_square.rb', line 253

def between?(x, y)
  return in_range?(x, y)
end

#in_range?(x, y) ⇒ Boolean

指定の座標が矩形の範囲内かを問い合わせる

x

指定のx座標

y

指定のy座標

返却値

座標が矩形の範囲内ならtrueを返す

Returns:

  • (Boolean)


245
246
247
# File 'lib/Miyako/API/struct_square.rb', line 245

def in_range?(x, y)
  return (x >= self[0] && y >= self[1] && x <= self[2] && y <= self[3])
end

#move(dx, dy) ⇒ Object

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

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

dx

移動量(x方向)。単位はピクセル

dy

移動量(y方向)。単位はピクセル

返却値

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



85
86
87
# File 'lib/Miyako/API/struct_square.rb', line 85

def move(dx, dy)
  self.dup.move!(dx, dy)
end

#move!(dx, dy) ⇒ Object

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

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

dx

移動量(x方向)。単位はピクセル

dy

移動量(y方向)。単位はピクセル

返却値

自分自身を返す



68
69
# File 'lib/Miyako/API/struct_square.rb', line 68

def move!(dx, dy)
end

#move_to(x, y) ⇒ Object

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

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

x

移動先位置(x方向)。単位はピクセル

y

移動先位置(y方向)。単位はピクセル

返却値

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



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

def move_to(x, y)
  self.dup.move_to!(x, y)
end

#move_to!(x, y) ⇒ Object

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

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

x

移動先位置(x方向)。単位はピクセル

y

移動先位置(y方向)。単位はピクセル

返却値

自分自身を返す



76
77
# File 'lib/Miyako/API/struct_square.rb', line 76

def move_to!(x, y)
end

#posObject

矩形の左上位置部分のみ返す

返却値

Position構造体のインスタンス



259
260
261
# File 'lib/Miyako/API/struct_square.rb', line 259

def pos
  return Point.new(self[0], self[1])
end

#resize(dw, dh) ⇒ Object

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

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

dw

幅変更。単位はピクセル

dh

高さ変更。単位はピクセル

返却値

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



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

def resize(dw, dh)
  self.dup.resize!(dw,dh)
end

#resize!(dw, dh) ⇒ Object

サイズを変更する(変化量を指定)

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

dw

幅変更。単位はピクセル

dh

高さ変更。単位はピクセル

返却値

自分自身を返す



104
105
# File 'lib/Miyako/API/struct_square.rb', line 104

def resize!(dw, dh)
end

#resize_to(w, h) ⇒ Object

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

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

w

幅変更。単位はピクセル

h

高さ変更。単位はピクセル

返却値

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



131
132
133
# File 'lib/Miyako/API/struct_square.rb', line 131

def resize_to(w, h)
  self.dup.resize_to!(w,h)
end

#resize_to!(w, h) ⇒ Object

サイズを変更する

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

w

幅変更。単位はピクセル

h

高さ変更。単位はピクセル

返却値

自分自身を返す



112
113
# File 'lib/Miyako/API/struct_square.rb', line 112

def resize_to!(w, h)
end

#sizeObject

矩形の大きさのみ返す

返却値

Size構造体のインスタンス



265
266
267
# File 'lib/Miyako/API/struct_square.rb', line 265

def size
  return Size.new(self[2]-self[0]+1, self[3]-self[1]+1)
end

#to_aryObject

矩形情報を配列に変換する

[left, top, right, bottom]の配列を生成して返す。

返却値

生成した配列



272
273
274
# File 'lib/Miyako/API/struct_square.rb', line 272

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

#to_rectObject

矩形情報をRect構造体に変換する

返却値

生成したRect構造体



278
279
280
# File 'lib/Miyako/API/struct_square.rb', line 278

def to_rect
  Rect.new(self[0], self[1], self[2]-self[0]+1, self[3]-self[1]+1)
end

#update!(obj) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/Miyako/API/struct_square.rb', line 27

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

#update_by_point!(obj) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/Miyako/API/struct_square.rb', line 35

def update_by_point!(obj)
  w = self[2]-self[0]
  h = self[3]-self[1]
  self[0] = obj[0]
  self[1] = obj[1]
  self[2] = self[0] + w
  self[3] = self[1] + h
  self
end

#update_by_rect!(obj) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/Miyako/API/struct_square.rb', line 51

def update_by_rect!(obj)
  self[0] = obj[0]
  self[1] = obj[1]
  self[2] = self[0] + obj[2] - 1
  self[3] = self[1] + obj[3] - 1
  self
end

#update_by_size!(obj) ⇒ Object



45
46
47
48
49
# File 'lib/Miyako/API/struct_square.rb', line 45

def update_by_size!(obj)
  self[2] = self[0] + obj[0] - 1
  self[3] = self[1] + obj[1] - 1
  self
end

#update_by_square!(obj) ⇒ Object



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

def update_by_square!(obj)
  update!(obj)
end