Class: Miyako::RectStruct

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

Overview

矩形情報のための構造体クラス

矩形変更メソッドを追加

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

インスタンスのかけ算

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

other

整数もしくはPoint構造体

返却値

Point構造体



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/Miyako/API/struct_rect.rb', line 180

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構造体



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/Miyako/API/struct_rect.rb', line 138

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構造体



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/Miyako/API/struct_rect.rb', line 159

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構造体



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/Miyako/API/struct_rect.rb', line 201

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?(x, y) ⇒ Boolean

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

x

指定のx座標

y

指定のy座標

返却値

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

Returns:

  • (Boolean)


229
230
231
# File 'lib/Miyako/API/struct_rect.rb', line 229

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

#in_range?(x, y) ⇒ Boolean

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

x

指定のx座標

y

指定のy座標

返却値

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

Returns:

  • (Boolean)


221
222
223
# File 'lib/Miyako/API/struct_rect.rb', line 221

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

#move(dx, dy) ⇒ Object

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

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

dx

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

dy

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

返却値

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



81
82
83
# File 'lib/Miyako/API/struct_rect.rb', line 81

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

#move!(dx, dy) ⇒ Object

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

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

dx

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

dy

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

返却値

自分自身を返す



64
65
# File 'lib/Miyako/API/struct_rect.rb', line 64

def move!(dx, dy)
end

#move_to(x, y) ⇒ Object

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

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

x

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

y

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

返却値

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



91
92
93
# File 'lib/Miyako/API/struct_rect.rb', line 91

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

#move_to!(x, y) ⇒ Object

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

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

x

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

y

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

返却値

自分自身を返す



72
73
# File 'lib/Miyako/API/struct_rect.rb', line 72

def move_to!(x, y)
end

#posObject

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

返却値

Position構造体のインスタンス



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

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

#resize(dw, dh) ⇒ Object

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

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

dw

幅変更。単位はピクセル

dh

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

返却値

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



117
118
119
# File 'lib/Miyako/API/struct_rect.rb', line 117

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

#resize!(dw, dh) ⇒ Object

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

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

dw

幅変更。単位はピクセル

dh

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

返却値

自分自身を返す



100
101
# File 'lib/Miyako/API/struct_rect.rb', line 100

def resize!(dw, dh)
end

#resize_to(w, h) ⇒ Object

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

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

w

幅変更。単位はピクセル

h

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

返却値

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



127
128
129
# File 'lib/Miyako/API/struct_rect.rb', line 127

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

#resize_to!(w, h) ⇒ Object

サイズを変更する

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

w

幅変更。単位はピクセル

h

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

返却値

自分自身を返す



108
109
# File 'lib/Miyako/API/struct_rect.rb', line 108

def resize_to!(w, h)
end

#sizeObject

矩形の大きさのみ返す

返却値

Size構造体のインスタンス



241
242
243
# File 'lib/Miyako/API/struct_rect.rb', line 241

def size
  return Size.new(self[2], self[3])
end

#to_aryObject

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

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

返却値

生成した配列



248
249
250
# File 'lib/Miyako/API/struct_rect.rb', line 248

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

#to_squareObject

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

返却値

生成したSquare構造体



254
255
256
# File 'lib/Miyako/API/struct_rect.rb', line 254

def to_square
  Square.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_rect.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
# File 'lib/Miyako/API/struct_rect.rb', line 35

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

#update_by_rect!(obj) ⇒ Object



47
48
49
# File 'lib/Miyako/API/struct_rect.rb', line 47

def update_by_rect!(obj)
  update!(obj)
end

#update_by_size!(obj) ⇒ Object



41
42
43
44
45
# File 'lib/Miyako/API/struct_rect.rb', line 41

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

#update_by_square!(obj) ⇒ Object



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

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