パッケージ
クラス名
taustation_geom2d.Vector
クラス仕様
概要
x、y二つの成分を持ち、絶対値の計算や加減乗算など2次元ベクトルの代数演算を行うメソッドを提供する。
クラス変数
なし
クラス・メソッド
- distance(pos1, pos2)
- 2つのVectorオブジェクトpos1、pos2を点を表す位置ベクトルとして、2点間の距離を返す。
インスタンス・プロパティ
- x
- ベクトルのx成分
- y
- ベクトルのy成分
コンストラクタ
- constructor(x, y)
- x成分とY成分を指定してVectorのインスタンスを生成する。
インスタンス・メソッド
基本メソッド
- set(x, y)
- Vectorのx成分とy成分をセットする。
- toString()
- Vectorの文字列形式の表現を返す。
- getClone()
- 元のVectorオブジェクトと同じ内容のクローンを生成して返す。
演算
- abs
- Vectorの絶対値(長さ)を返す。
- times(coeff)
- Vectorに定数coeffを乗じたベクトルを新たに生成して返す。
- timesEq(coeff)
- Vectorに定数coeffを乗じて変更する。新しいオブジェクトは生成しない。
- div(coeff)
- Vectorを定数coeffで除したベクトルを新たに生成して返す。
- divEq(coeff)
- Vectorを定数coeffで除して変更する。新しいオブジェクトは生成しない。
- plus(other)
- Vectorにotherベクトルを加えたベクトルを新たに生成して返す。
- plusEq(other)
- Vectorにotherベクトルを加えて変更する。新しいオブジェクトは生成しない。
- minus(other)
- Vectorからotherベクトルを減じたベクトルを新たに生成して返す。
- minusEq(other)
- Vectorからotherベクトルを減じて変更する。新しいオブジェクトは生成しない。
- inprod(other)
- Vectorとotherベクトルの内積値を返す。
- ang(other)
- Vectorとotherのなす角を計算する(0~π)。
新たなVectorの生成
- getUnit()
- Vectorと同じ向きの単位ベクトル(長さが1)を新たに生成して返す。
- getInverse()
- Vectorと逆向きのベクトルを新たに生成して返す。
- getOrthogonalR()
- Vectorから右に90度回転したベクトルを新たに生成して返す。
- getOrthogonalL()
- Vectorから左に90度回転したベクトルを新たに生成して返す。
- getUnitOrthogonalR()
- Vectorから右に90度回転した単位ベクトルを新たに生成して返す。
- getUnitOrthogonalL()
- Vectorから左に90度回転した単位ベクトルを新たに生成して返す。
判定
- isHeadingToRightOf(other)
- このVectorオブジェクトの進行方向が、引数のVectorオブジェクトotherの進行方向より右方を指しているときtrue、左方を指しているときfalseを返す。
- isHeadingToLeftOf(other)
- isHeadingToRightOf()と左右逆の判定結果を返す
- isInFrontOf(movingAgent)
- このVectorオブジェクトが位置ベクトルとして、その点が引数のMovingAgentオブジェクトの前方(左右180度より進行方向の側)にあるときtrue、後方にあるときfalseを返す。真横の時は前方と判定される。
- isIntheRearOf(movingAgent)
- isInFrontOf()と前後逆の判定結果を返す。
- isOnTheRightOf(movingAgent)
- このVectorオブジェクトが位置ベクトルとして、その点が引数のMovingAgentオブジェクトの右方にあるときtrue、左方にあるときfalseを返す。進行方向の延長線上にあるときは右方と判定される。
- isOnTheLeftOf(movingAgent)
- isOnTheRightOf()と左右逆の判定結果を返す。
コード
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |   class Vector     # 2つの位置ベクトルで表される点の間の距離を返す     @distance: (pos1, pos2) ->       Math.sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) +                 (pos1.y - pos2.y) * (pos1.y - pos2.y))     constructor: (@x, @y) ->     set: (x, y) -> @x = x; @y = y; return     toString: -> "(#{@x}, #{@y})"     # 自身と同じ内容のVectorオブジェクトを生成して返す     getClone: -> new Vector(@x, @y)     # ベクトルの絶対値(長さ)     abs: -> Math.sqrt(@x * @x + @y * @y)     # ベクトルとスカラーの乗除算     # 以降のメソッド群でも、Eqがついていれば元のオブジェクトを変更し、     # Eqがついていなければ新しいベクトルを生成して返す     times: (a) -> new Vector(@x * a, @y * a)     timesEq: (a) -> @x *= a; @y *= a; return     div: (a) -> new Vector(@x / a, @y / a)     divEq: (a) -> @x /= a; @y /= a; return     # ベクトル同士の加減算     plus: (other) -> new Vector(@x + other.x, @y + other.y)     plusEq: (other) -> @x += other.x; @y += other.y; return     minus: (other) -> new Vector(@x - other.x, @y - other.y)     minusEq: (other) -> @x -= other.x; @y -= other.y; return     # 2つのベクトルの内席となす角     inprod: (other) -> @x * other.x + @y * other.y     ang: (other) -> Math.acos(@inprod(other) / @.abs() / other.abs())     # 単位ベクトル、逆ベクトル、直交ベクトルを返す     getUnit: -> a = @abs(); new Vector(@x / a, @y / a)     getInverse: -> new Vector(-@x, -@y)     getOrthogonalR: -> new Vector(@y, -@x)     getOrthogonalL: -> new Vector(-@y, @x)     getUnitOrthogonalR: -> a = @abs(); new Vector(@y / a, -@x / a)     getUnitOrthogonalL: -> a = @abs(); new Vector(-@y / a, @x / a)     # このVectorが引数のVectorの向きに対してどちらに向いているか     isHeadingToRightOf: (other) ->       if @x * other.y - @y * other.x >=0 then true else false     isHeadingToLeftOf: (other) -> return not @isHeadingToRightOf(other)     # 以下、Vectorを位置ベクトルと考えたときのMovingAgentとの位置関係     #     # Vectorの位置がMovingAgentの前方か     isInFrontOf: (movingAgent) ->       inprod = movingAgent.v.x * (@x - movingAgent.pos.x) +                movingAgent.v.y * (@y - movingAgent.pos.y)       if inprod >= 0 then true else false     # Vectorの位置がMovingAgentの後方か     isInTheRearOf: (movingAgent) -> not @isInFrontOf(movingAgent)     # Vectorの位置がMovingAgentの右方か     isOnTheRightOf: (movingAgent) ->       inprod = (@x - movingAgent.pos.x) * movingAgent.v.y -                (@y - movingAgent.pos.y) * movingAgent.v.x       if inprod >= 0 then true else false     # Vectorの位置がMovingAgentの左方か     isOnTheLeftOf: (movingAgent) -> not @isOnTheRightOf(movingAgent) |