Module: Miyako::Utility
- Defined in:
- lib/Miyako/API/utility.rb
Overview
ユーティリティモジュール
Class Method Summary collapse
-
.degree(point1, point2) ⇒ Object
2点間の傾きを角度で算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる point1:: 点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) point2:: 点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) 返却値:: 2点間の傾き.
-
.degree2(x1, y1, x2, y2) ⇒ Object
2点間の傾きを角度で算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる x1:: 点1の位置(x) y1:: 点1の位置(y) x2:: 点2の位置(x) y2:: 点2の位置(y) 返却値:: 2点間の傾き.
-
.get_step_array_f(v1, v2, amount, skip_even = false) ⇒ Object
:nodoc:.
- .in_bounds?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
-
.in_bounds_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
小線分を移動させたとき、大線分が範囲内かどうかを判別する 移動後の小線分が大線分の範囲内にあるかどうかをtrue/falseで取得する mini_pos:: 小線分の開始点の位置 mini_size:: 小線分の幅。0以上の整数 big_pos:: 大線分の開始点の位置 big_size:: 大線分の幅。1以上の整数 d:: mini_segmentの移動量 flag:: 大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse 返却値:: 範囲内のときはtrue、範囲外の時はfalseを返す.
- .in_bounds_ex?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
-
.in_bounds_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
小線分を移動させたとき、大線分が範囲内かどうかを判別して、その状態によって値を整数で返す 移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは-1、 プラス方向で出るときは1を返す mini_pos:: 小線分の開始点の位置 mini_size:: 小線分の幅。0以上の整数 big_pos:: 大線分の開始点の位置 big_size:: 大線分の幅。1以上の整数 d:: mini_segmentの移動量 flag:: 大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse 返却値:: 判別の結果.
- .in_bounds_rev?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
-
.in_bounds_rev_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す 移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは1、 プラス方向で出るときは-1を返す mini_pos:: 小線分の開始点の位置 mini_size:: 小線分の幅。0以上の整数 big_pos:: 大線分の開始点の位置 big_size:: 大線分の幅。1以上の整数 d:: mini_segmentの移動量 flag:: 大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse 返却値:: 判別の結果.
- .in_bounds_rev_ex?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
-
.in_bounds_rev_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す 移動量が0のときは0、 移動後の小線分の範囲が大線分の範囲内のときは1、 範囲外に出るときは-1を返す mini_pos:: 小線分の開始点の位置 mini_size:: 小線分の幅。0以上の整数 big_pos:: 大線分の開始点の位置 big_size:: 大線分の幅。1以上の整数 d:: mini_segmentの移動量 flag:: 大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse 返却値:: 判別の結果.
-
.interval(point1, point2) ⇒ Object
2点間の距離を算出する 2点(点1、点2)がどの程度離れているかを算出する。 返ってくる値は、正の実数で返ってくる point1:: 点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) point2:: 点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) 返却値:: 2点間の距離.
-
.interval2(x1, y1, x2, y2) ⇒ Object
2点間の距離を算出する 2点(点1、点2)がどの程度離れているかを算出する。 返ってくる値は、正の実数で返ってくる x1:: 点1の位置(x) y1:: 点1の位置(y) x2:: 点2の位置(x) y2:: 点2の位置(y) 返却値:: 2点間の距離.
-
.product_inner(x1, y1, x2, y2, size) ⇒ Object
:nodoc:.
-
.product_inner_f(x1, y1, x2, y2, size, skip_even = false) ⇒ Object
:nodoc:.
- .product_liner(rect, amount = 1) ⇒ Object
- .product_liner_by_square(square, amount = 1) ⇒ Object
-
.product_liner_by_square_f(square, amount = 1.0) ⇒ Object
矩形内の対角線の座標リストを実数で取得する (互換性維持のために残している) 矩形内の対角線の座標リストを取得する 引数には、Rect(x,y,w,h)形式のインスタンスを渡す 幅・高さはマイナスの値の設定が可能。 幅・高さのどちらかの値が0(Float::EPSILON未満)の場合は[]が返る 刻みの値は1以上の整数を渡す。0(Float::EPSILON未満)以下の場合は例外が発生する。 結果はの配列となるが、正確さを優先したため、必ず刻みの値の間隔で並んでいない (刻みの値より小さい)ことがある rect:: 矩形情報 amount:: 配列を作成する座標の刻み。デフォルトは1 返却値:: 矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ).
-
.product_liner_f(rect, amount = 1.0) ⇒ Object
矩形内の対角線の座標リストを実数で取得する (互換性維持のために残している) 矩形内の対角線の座標リストを取得する 引数には、Rect(x,y,w,h)形式のインスタンスを渡す 幅・高さはマイナスの値の設定が可能。 幅・高さのどちらかの値が0(Float::EPSILON未満)の場合は[]が返る 刻みの値は1以上の整数を渡す。0(Float::EPSILON未満)以下の場合は例外が発生する。 結果はの配列となるが、正確さを優先したため、必ず刻みの値の間隔で並んでいない (刻みの値より小さい)ことがある rect:: 矩形情報 amount:: 配列を作成する座標の刻み。デフォルトは1.0 返却値:: 矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ).
-
.product_liner_xy(x1, y1, x2, y2, amount) ⇒ Object
ToDo: 線形補完を使う -> 使った(2010.06.20).
- .product_liner_xy_f(x1, y1, x2, y2, amount) ⇒ Object
-
.product_position(position, rect, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す position(Point()形式)を基準として、矩形rect(Rect()形式)が、格子状の並べた矩形 (基準を[0,0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。.
-
.product_position_by_square(square, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す 矩形square(Square()形式)が、格子状の並べた矩形 (基準を[0,0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。 square:: 矩形情報 返却値:: 矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ).
-
.product_position_by_square_f(square, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す(実数で指定) 矩形square(Square()形式)が、格子状の並べた矩形 (基準を[0.0,0.0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。 square:: 矩形情報 返却値:: 矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ).
-
.product_position_f(position, rect, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す(実数で指定) position(Point()形式)を基準として、矩形rect(Rect()形式)が、格子状の並べた矩形 (基準を[0.0,0.0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。.
-
.radian(point1, point2, distance = nil) ⇒ Object
2点間の傾きをラジアンで算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる point1:: 点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) point2:: 点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) 返却値:: 2点間の傾き.
-
.radian2(x1, y1, x2, y2, distance = nil) ⇒ Object
2点間の傾きをラジアンで算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる x1:: 点1の位置(x) y1:: 点1の位置(y) x2:: 点2の位置(x) y2:: 点2の位置(y) 返却値:: 2点間の傾き.
-
.theta(point1, point2, distance = nil) ⇒ Object
2点間の傾きを角度で算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる point1:: 点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) point2:: 点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス) 返却値:: 2点間の傾き.
-
.theta2(x1, y1, x2, y2, distance = nil) ⇒ Object
2点間の傾きを角度で算出する 2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる x1:: 点1の位置(x) y1:: 点1の位置(y) x2:: 点2の位置(x) y2:: 点2の位置(y) 返却値:: 2点間の傾き.
Class Method Details
.degree(point1, point2) ⇒ Object
2点間の傾きを角度で算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- point1
-
点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- point2
-
点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- 返却値
-
2点間の傾き
314 315 316 317 318 |
# File 'lib/Miyako/API/utility.rb', line 314 def Utility.degree(point1, point2) return 0.0 if (point2[0].to_f-point1[0].to_f < Float::EPSILON) degree = (point2[1]-point1[1]).to_f/(point2[0]-point1[0]).to_f return degree < Float::EPSILON ? 0.0 : degree end |
.degree2(x1, y1, x2, y2) ⇒ Object
2点間の傾きを角度で算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- x1
-
点1の位置(x)
- y1
-
点1の位置(y)
- x2
-
点2の位置(x)
- y2
-
点2の位置(y)
- 返却値
-
2点間の傾き
329 330 331 332 333 |
# File 'lib/Miyako/API/utility.rb', line 329 def Utility.degree2(x1, y1, x2, y2) return 0.0 if (x2.to_f-x1[0].to_f < Float::EPSILON) degree = (y2-y1).to_f/(x2-x1).to_f return degree < Float::EPSILON ? 0.0 : degree end |
.get_step_array_f(v1, v2, amount, skip_even = false) ⇒ Object
:nodoc:
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/Miyako/API/utility.rb', line 26 def Utility.get_step_array_f(v1, v2, amount, skip_even = false) #:nodoc: steps = [] amount = amount.abs val = v1 if v1 < v2 loop do val = val + amount break if (skip_even && (v2-val).abs < Float::EPSILON) break if val > v2 steps << val end else loop do val = val - amount break if (skip_even && (v2-val).abs < Float::EPSILON) break if val < v2 steps << val end end return steps end |
.in_bounds?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
342 343 344 345 346 347 |
# File 'lib/Miyako/API/utility.rb', line 342 def Utility.in_bounds?(mini_segment, big_segment, d, flag = false) nx = mini_segment[0] + d nx2 = mini_segment[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_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
小線分を移動させたとき、大線分が範囲内かどうかを判別する
移動後の小線分が大線分の範囲内にあるかどうかをtrue/falseで取得する
- mini_pos
-
小線分の開始点の位置
- mini_size
-
小線分の幅。0以上の整数
- big_pos
-
大線分の開始点の位置
- big_size
-
大線分の幅。1以上の整数
- d
-
mini_segmentの移動量
- flag
-
大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse
- 返却値
-
範囲内のときはtrue、範囲外の時はfalseを返す
411 412 413 414 415 416 417 418 419 |
# File 'lib/Miyako/API/utility.rb', line 411 def Utility.in_bounds_by_size?(pos1, size1, pos2, size2, d, flag = false) raise MiyakoValueError, "illegal size1! #{size1}" if size1 < 0 raise MiyakoValueError, "illegal size2! #{size2}" if size2 <= 0 raise MiyakoValueError, "size1 is more than size2! #{size1}, #{size2}" if size1 > size2 min_x1 = pos1 + d min_x2 = pos1 + size1 + d min_x1, min_x2 = min_x2, min_x1 if min_x1 > min_x2 return flag ? (min_x1 >= pos2 && min_x2 <= pos2+size2) : (minx_x1 > pos2 && min_x2 < pos2+size2) end |
.in_bounds_ex?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
358 359 360 361 362 363 364 |
# File 'lib/Miyako/API/utility.rb', line 358 def Utility.in_bounds_ex?(mini_segment, big_segment, d, flag = false) nx = mini_segment[0] + d nx2 = mini_segment[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_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
小線分を移動させたとき、大線分が範囲内かどうかを判別して、その状態によって値を整数で返す
移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは-1、 プラス方向で出るときは1を返す
- mini_pos
-
小線分の開始点の位置
- mini_size
-
小線分の幅。0以上の整数
- big_pos
-
大線分の開始点の位置
- big_size
-
大線分の幅。1以上の整数
- d
-
mini_segmentの移動量
- flag
-
大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse
- 返却値
-
判別の結果
432 433 434 435 436 437 438 439 440 441 |
# File 'lib/Miyako/API/utility.rb', line 432 def Utility.in_bounds_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) raise MiyakoValueError, "illegal size1! #{size1}" if size1 < 0 raise MiyakoValueError, "illegal size2! #{size2}" if size2 <= 0 raise MiyakoValueError, "size1 is more than size2! #{size1}, #{size2}" if size1 > size2 min_x1 = pos1 + d min_x2 = pos1 + size1 + d min_x1, min_x2 = min_x2, min_x1 if min_x1 > min_x2 return -1 if (min_x1 < pos2) || (flag && (min_x1 == pos2)) return (min_x2 > pos2+size2) || (flag && (min_x2 == pos2+size2)) ? 1 : 0 end |
.in_bounds_rev?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
375 376 377 378 379 380 381 |
# File 'lib/Miyako/API/utility.rb', line 375 def Utility.in_bounds_rev?(mini_segment, big_segment, d, flag = false) nx = mini_segment[0] + d nx2 = mini_segment[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_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す
移動後の小線分の範囲が大線分の範囲内のときは0、 マイナス方向で範囲外に出るときは1、 プラス方向で出るときは-1を返す
- mini_pos
-
小線分の開始点の位置
- mini_size
-
小線分の幅。0以上の整数
- big_pos
-
大線分の開始点の位置
- big_size
-
大線分の幅。1以上の整数
- d
-
mini_segmentの移動量
- flag
-
大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse
- 返却値
-
判別の結果
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/Miyako/API/utility.rb', line 454 def Utility.in_bounds_rev_by_size?(pos1, size1, pos2, size2, d, flag = false) raise MiyakoValueError, "illegal size1! #{size1}" if size1 < 0 raise MiyakoValueError, "illegal size2! #{size2}" if size2 <= 0 raise MiyakoValueError, "size1 is more than size2! #{size1}, #{size2}" if size1 > size2 min_x1 = pos1 + d min_x2 = pos1 + size1 + d min_x1, min_x2 = min_x2, min_x1 if min_x1 > min_x2 return 1 if (min_x1 < pos2) || (flag && (min_x1 == pos2)) return (min_x2 > pos2+size2) || (flag && (min_x2 == pos2+size2)) ? -1 : 0 end |
.in_bounds_rev_ex?(mini_segment, big_segment, d, flag = false) ⇒ Boolean
392 393 394 395 396 397 398 399 400 |
# File 'lib/Miyako/API/utility.rb', line 392 def Utility.in_bounds_rev_ex?(mini_segment, big_segment, d, flag = false) return 0 if d == 0 dir = (d <=> 0) nx = mini_segment[0] + d nx2 = mini_segment[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_bounds_rev_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) ⇒ Boolean
移動先が表示範囲内かどうかを判別して、その状態によって値を整数で返す
移動量が0のときは0、 移動後の小線分の範囲が大線分の範囲内のときは1、 範囲外に出るときは-1を返す
- mini_pos
-
小線分の開始点の位置
- mini_size
-
小線分の幅。0以上の整数
- big_pos
-
大線分の開始点の位置
- big_size
-
大線分の幅。1以上の整数
- d
-
mini_segmentの移動量
- flag
-
大線分の端いっぱいも範囲外に含めるときはtrueを設定する。デフォルトはfalse
- 返却値
-
判別の結果
476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/Miyako/API/utility.rb', line 476 def Utility.in_bounds_rev_ex_by_size?(pos1, size1, pos2, size2, d, flag = false) return 0 if d == 0 raise MiyakoValueError, "illegal size1! #{size1}" if size1 < 0 raise MiyakoValueError, "illegal size2! #{size2}" if size2 <= 0 raise MiyakoValueError, "size1 is more than size2! #{size1}, #{size2}" if size1 > size2 dir = (d <=> 0) min_x1 = pos1 + d min_x2 = pos1 + size1 + d min_x1, min_x2 = min_x2, min_x1 if min_x1 > min_x2 return -dir if (min_x1 < pos2) || (flag && (min_x1 == pos2)) return (min_x2 > pos2+size2) || (flag && (min_x2 == pos2+size2)) ? -dir : dir end |
.interval(point1, point2) ⇒ Object
2点間の距離を算出する
2点(点1、点2)がどの程度離れているかを算出する。 返ってくる値は、正の実数で返ってくる
- point1
-
点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- point2
-
点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- 返却値
-
2点間の距離
221 222 223 224 225 226 |
# File 'lib/Miyako/API/utility.rb', line 221 def Utility.interval(point1, point2) #2点間の距離を求める d = Math.sqrt(((point1[0].to_f - point2[0].to_f) ** 2) + ((point1[1].to_f - point2[1].to_f) ** 2)) return d < Float::EPSILON ? 0.0 : d end |
.interval2(x1, y1, x2, y2) ⇒ Object
2点間の距離を算出する
2点(点1、点2)がどの程度離れているかを算出する。 返ってくる値は、正の実数で返ってくる
- x1
-
点1の位置(x)
- y1
-
点1の位置(y)
- x2
-
点2の位置(x)
- y2
-
点2の位置(y)
- 返却値
-
2点間の距離
236 237 238 239 240 241 |
# File 'lib/Miyako/API/utility.rb', line 236 def Utility.interval2(x1, y1, x2, y2) #2点間の距離を求める d = Math.sqrt(((x1.to_f - x2.to_f) ** 2) + ((y1.to_f - y2.to_f) ** 2)) return d < Float::EPSILON ? 0.0 : d end |
.product_inner(x1, y1, x2, y2, size) ⇒ Object
:nodoc:
142 143 144 145 146 |
# File 'lib/Miyako/API/utility.rb', line 142 def Utility.product_inner(x1, y1, x2, y2, size) #:nodoc: x_array = ((x1 / size[0])..(x2 / size[0])).to_a.map{|e| e * size[0]} y_array = ((y1 / size[1])..(y2 / size[1])).to_a.map{|e| e * size[1]} return x_array.product(y_array) end |
.product_inner_f(x1, y1, x2, y2, size, skip_even = false) ⇒ Object
:nodoc:
176 177 178 179 180 181 182 183 184 |
# File 'lib/Miyako/API/utility.rb', line 176 def Utility.product_inner_f(x1, y1, x2, y2, size, skip_even = false) #:nodoc: sz = size[0].to_f min = (x1.to_f/sz).floor.to_f * sz x_array = [min] + get_step_array_f(min, x2.to_f, sz, skip_even) sz = size[1].to_f min = (y1.to_f/sz).floor.to_f * sz y_array = [min] + get_step_array_f(min, y2.to_f, sz, skip_even) return x_array.uniq.product(y_array.uniq) end |
.product_liner(rect, amount = 1) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/Miyako/API/utility.rb', line 115 def Utility.product_liner(rect, amount = 1) raise MiyakoValueError, "Illegal amount! #{amount}" if amount <= 0 return [] if rect[2] == 0 || rect[3] == 0 x1 = rect[0] y1 = rect[1] x2 = x1 + rect[2] - 1 y2 = y1 + rect[3] - 1 return product_liner_xy(x1, y1, x2, y2, amount) end |
.product_liner_by_square(square, amount = 1) ⇒ Object
136 137 138 139 140 |
# File 'lib/Miyako/API/utility.rb', line 136 def Utility.product_liner_by_square(square, amount = 1) raise MiyakoValueError, "Illegal amount! #{amount}" if amount <= 0 return [] if (square[2] - square[0]) == 0 || (square[3] - square[1]) == 0 return product_liner_xy(*square, amount) end |
.product_liner_by_square_f(square, amount = 1.0) ⇒ Object
矩形内の対角線の座標リストを実数で取得する
(互換性維持のために残している) 矩形内の対角線の座標リストを取得する 引数には、Rect(x,y,w,h)形式のインスタンスを渡す 幅・高さはマイナスの値の設定が可能。 幅・高さのどちらかの値が0(Float::EPSILON未満)の場合は[]が返る 刻みの値は1以上の整数を渡す。0(Float::EPSILON未満)以下の場合は例外が発生する。 結果はの配列となるが、正確さを優先したため、必ず刻みの値の間隔で並んでいない (刻みの値より小さい)ことがある
- rect
-
矩形情報
- amount
-
配列を作成する座標の刻み。デフォルトは1
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
89 90 91 92 93 |
# File 'lib/Miyako/API/utility.rb', line 89 def Utility.product_liner_by_square_f(square, amount = 1.0) raise MiyakoValueError, "Illegal amount! #{amount}" if amount < Float::EPSILON return [] if (square[2] - square[0]) < Float::EPSILON || (square[3] - square[1]) < Float::EPSILON return product_liner_xy_f(*square, amount) end |
.product_liner_f(rect, amount = 1.0) ⇒ Object
矩形内の対角線の座標リストを実数で取得する
(互換性維持のために残している) 矩形内の対角線の座標リストを取得する 引数には、Rect(x,y,w,h)形式のインスタンスを渡す 幅・高さはマイナスの値の設定が可能。 幅・高さのどちらかの値が0(Float::EPSILON未満)の場合は[]が返る 刻みの値は1以上の整数を渡す。0(Float::EPSILON未満)以下の場合は例外が発生する。 結果はの配列となるが、正確さを優先したため、必ず刻みの値の間隔で並んでいない (刻みの値より小さい)ことがある
- rect
-
矩形情報
- amount
-
配列を作成する座標の刻み。デフォルトは1.0
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
67 68 69 70 71 72 73 74 75 |
# File 'lib/Miyako/API/utility.rb', line 67 def Utility.product_liner_f(rect, amount = 1.0) raise MiyakoValueError, "Illegal amount! #{amount}" if amount < Float::EPSILON return [] if rect[2] < Float::EPSILON || rect[3] < Float::EPSILON x1 = rect[0] y1 = rect[1] x2 = x1 + rect[2] - 1 y2 = y1 + rect[3] - 1 return product_liner_xy_f(x1, y1, x2, y2, amount) end |
.product_liner_xy(x1, y1, x2, y2, amount) ⇒ Object
ToDo: 線形補完を使う -> 使った(2010.06.20)
97 98 99 100 101 102 |
# File 'lib/Miyako/API/utility.rb', line 97 def Utility.product_liner_xy(x1, y1, x2, y2, amount) #:nodoc: distance = Utility.interval2(x1,y1,x2,y2) degree = Utility.radian2(x1,y1,x2,y2,distance) cos, sin = Math.cos(degree), Math.sin(degree) (0..distance).step(amount).with_object([]){|n, arr| arr << [x1+(n*cos).to_i, y1+(n*sin).to_i]} << [x2,y2] end |
.product_liner_xy_f(x1, y1, x2, y2, amount) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/Miyako/API/utility.rb', line 48 def Utility.product_liner_xy_f(x1, y1, x2, y2, amount) distance = Utility.interval2(x1,y1,x2,y2) degree = Utility.radian2(x1,y1,x2,y2,distance) cos, sin = Math.cos(degree), Math.sin(degree) (0..distance).step(amount).with_object([]){|n, arr| arr << [x1 + n * cos, y1 + n * sin]} << [x2,y2] end |
.product_position(position, rect, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す
position(Point()形式)を基準として、矩形rect(Rect()形式)が、格子状の並べた矩形 (基準を[0,0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。
- position
-
基準位置
- rect
-
矩形情報
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
156 157 158 159 160 161 162 163 164 |
# File 'lib/Miyako/API/utility.rb', line 156 def Utility.product_position(position, rect, size) return product_inner( position[0] + rect[0], position[1] + rect[1], position[0] + rect[0] + rect[2] - 1, position[1] + rect[1] + rect[3] - 1, size ) end |
.product_position_by_square(square, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す
矩形square(Square()形式)が、格子状の並べた矩形 (基準を[0,0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。
- square
-
矩形情報
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
172 173 174 |
# File 'lib/Miyako/API/utility.rb', line 172 def Utility.product_position_by_square(square, size) return product_inner(*square.to_a, size) end |
.product_position_by_square_f(square, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す(実数で指定)
矩形square(Square()形式)が、格子状の並べた矩形 (基準を[0.0,0.0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。
- square
-
矩形情報
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
211 212 213 |
# File 'lib/Miyako/API/utility.rb', line 211 def Utility.product_position_by_square_f(square, size) return product_inner_f(*square.to_a, size) end |
.product_position_f(position, rect, size) ⇒ Object
指定の矩形が格子状のどこに重なっているかを返す(実数で指定)
position(Point()形式)を基準として、矩形rect(Rect()形式)が、格子状の並べた矩形 (基準を[0.0,0.0]とした、大きさの矩形をタイル状に並べた物)にある場合、 どの格子状の矩形が重なっているかを、矩形の左上座標の配列として渡す(x座標とy座標の組み合わせ)。
- position
-
基準位置
- rect
-
矩形情報
- 返却値
-
矩形左上位置の配列(指定の矩形に掛かる位置の組み合わせ)
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/Miyako/API/utility.rb', line 194 def Utility.product_position_f(position, rect, size) return product_inner_f( position[0] + rect[0], position[1] + rect[1], position[0] + rect[0] + rect[2], position[1] + rect[1] + rect[3], size, true ) end |
.radian(point1, point2, distance = nil) ⇒ Object
2点間の傾きをラジアンで算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- point1
-
点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- point2
-
点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- 返却値
-
2点間の傾き
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/Miyako/API/utility.rb', line 276 def Utility.radian(point1, point2, distance = nil) #2点間の距離を求める d = distance || Math.sqrt(((point1[0].to_f - point2[0].to_f) ** 2) + ((point1[1].to_f - point2[1].to_f) ** 2)) x = point2[0].to_f - point1[0].to_f # 傾き・幅が0のときは傾きは0度 return 0.0 if (x.abs < Float::EPSILON or d < Float::EPSILON) theta = Math.acos(x / d) return theta < Float::EPSILON ? 0.0 : (point2[1]-point1[1]<0 ? 2*Math::PI-theta : theta) end |
.radian2(x1, y1, x2, y2, distance = nil) ⇒ Object
2点間の傾きをラジアンで算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- x1
-
点1の位置(x)
- y1
-
点1の位置(y)
- x2
-
点2の位置(x)
- y2
-
点2の位置(y)
- 返却値
-
2点間の傾き
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/Miyako/API/utility.rb', line 296 def Utility.radian2(x1, y1, x2, y2, distance = nil) #2点間の距離を求める d = distance || Math.sqrt(((x1.to_f - x2.to_f) ** 2) + ((y1.to_f - y2.to_f) ** 2)) x = x2.to_f - x1.to_f # 傾き・幅が0のときは傾きは0度 return 0.0 if (x.abs < Float::EPSILON or d < Float::EPSILON) theta = Math.acos(x / d) return theta < Float::EPSILON ? 0.0 : (y2-y1<0 ? 2*Math::PI-theta : theta) end |
.theta(point1, point2, distance = nil) ⇒ Object
2点間の傾きを角度で算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- point1
-
点1の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- point2
-
点2の位置(Point/Rect/Square構造体、2要素以上の配列、もしくはx,yメソッドを持つインスタンス)
- 返却値
-
2点間の傾き
250 251 252 253 |
# File 'lib/Miyako/API/utility.rb', line 250 def Utility.theta(point1, point2, distance = nil) theta = (Utility.radian(point1,point2,distance) / (2 * Math::PI)) * 360.0 return theta < Float::EPSILON ? 0.0 : theta end |
.theta2(x1, y1, x2, y2, distance = nil) ⇒ Object
2点間の傾きを角度で算出する
2点(点1、点2)がどの程度傾いているか算出する。傾きの中心は点1とする。 角度の単位は度(0.0<=θ<360.0) 返ってくる値は、正の実数で返ってくる
- x1
-
点1の位置(x)
- y1
-
点1の位置(y)
- x2
-
点2の位置(x)
- y2
-
点2の位置(y)
- 返却値
-
2点間の傾き
264 265 266 267 |
# File 'lib/Miyako/API/utility.rb', line 264 def Utility.theta2(x1, y1, x2, y2, distance = nil) theta = (Utility.radian2(x1,y1,x2,y2,distance) / (2 * Math::PI)) * 360.0 return theta < Float::EPSILON ? 0.0 : theta end |