Class: Miyako::PointStruct

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

Overview

位置情報のための構造体クラス

位置変更メソッドを追加

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

インスタンスのかけ算

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

other

整数もしくはPoint構造体

返却値

Point構造体



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/Miyako/API/struct_point.rb', line 134

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Miyako/API/struct_point.rb', line 92

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



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/Miyako/API/struct_point.rb', line 113

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/Miyako/API/struct_point.rb', line 155

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?(:[])
    ret[0] /= other[0]
    ret[1] /= other[1]
  else
    raise MiyakoError, "this parameter cannot access!"
  end
  ret
end

#move(dx, dy) ⇒ Object

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

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

dx

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

dy

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

返却値

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



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

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

#move!(dx, dy) ⇒ Object

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

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

dx

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

dy

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

返却値

自分自身を返す



54
55
# File 'lib/Miyako/API/struct_point.rb', line 54

def move!(dx, dy)
end

#move_to(x, y) ⇒ Object

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

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

x

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

y

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

返却値

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



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

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

#move_to!(x, y) ⇒ Object

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

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

x

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

y

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

返却値

自分自身を返す



62
63
# File 'lib/Miyako/API/struct_point.rb', line 62

def move_to!(x, y)
end

#to_aryObject

:nodoc:



170
171
172
# File 'lib/Miyako/API/struct_point.rb', line 170

def to_ary #:nodoc:
  [self[0], self[1]]
end

#update!(obj) ⇒ Object



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

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

#update_by_point!(obj) ⇒ Object



33
34
35
# File 'lib/Miyako/API/struct_point.rb', line 33

def update_by_point!(obj)
  update!(obj)
end

#update_by_rect!(obj) ⇒ Object



41
42
43
# File 'lib/Miyako/API/struct_point.rb', line 41

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

#update_by_size!(obj) ⇒ Object



37
38
39
# File 'lib/Miyako/API/struct_point.rb', line 37

def update_by_size!(obj)
  self
end

#update_by_square!(obj) ⇒ Object



45
46
47
# File 'lib/Miyako/API/struct_point.rb', line 45

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