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



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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/Miyako/API/struct_point.rb', line 70

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



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

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



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

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方向)。単位はピクセル

返却値

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



49
50
51
# File 'lib/Miyako/API/struct_point.rb', line 49

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

#move!(dx, dy) ⇒ Object

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

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

dx

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

dy

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

返却値

自分自身を返す



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

def move!(dx, dy)
end

#move_to(x, y) ⇒ Object

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

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

x

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

y

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

返却値

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



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

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

#move_to!(x, y) ⇒ Object

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

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

x

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

y

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

返却値

自分自身を返す



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

def move_to!(x, y)
end

#to_aryObject

:nodoc:



148
149
150
# File 'lib/Miyako/API/struct_point.rb', line 148

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