Class: Vector
- Inherits:
-
Object
- Object
- Vector
- Extended by:
- Forwardable
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/nswtopo/geometry/vector.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(scalar) ⇒ Object
- #angle ⇒ Object
- #cross(other) ⇒ Object
- #dot(other) ⇒ Object
-
#initialize(x, y) ⇒ Vector
constructor
A new instance of Vector.
- #inspect ⇒ Object
- #norm ⇒ Object
- #normalised ⇒ Object
- #perp ⇒ Object
- #proj(other) ⇒ Object
- #rotate_by(angle) ⇒ Object
- #rotate_by_degrees(angle) ⇒ Object
- #to_ary ⇒ Object
- #to_d ⇒ Object
- #to_f ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Vector
Returns a new instance of Vector.
2 3 4 5 |
# File 'lib/nswtopo/geometry/vector.rb', line 2 def initialize(x, y) @x, @y = x, y freeze end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
11 12 13 |
# File 'lib/nswtopo/geometry/vector.rb', line 11 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
11 12 13 |
# File 'lib/nswtopo/geometry/vector.rb', line 11 def y @y end |
Class Method Details
.[](x, y) ⇒ Object
7 8 9 |
# File 'lib/nswtopo/geometry/vector.rb', line 7 def self.[](x, y) new x, y end |
Instance Method Details
#*(scalar) ⇒ Object
54 55 56 |
# File 'lib/nswtopo/geometry/vector.rb', line 54 def *(scalar) Vector[@x * scalar, @y * scalar] end |
#+(other) ⇒ Object
46 47 48 |
# File 'lib/nswtopo/geometry/vector.rb', line 46 def +(other) Vector[@x + other.x, @y + other.y] end |
#+@ ⇒ Object
62 63 64 |
# File 'lib/nswtopo/geometry/vector.rb', line 62 def +@ self end |
#-(other) ⇒ Object
50 51 52 |
# File 'lib/nswtopo/geometry/vector.rb', line 50 def -(other) Vector[@x - other.x, @y - other.y] end |
#-@ ⇒ Object
66 67 68 |
# File 'lib/nswtopo/geometry/vector.rb', line 66 def -@ self * -1 end |
#/(scalar) ⇒ Object
58 59 60 |
# File 'lib/nswtopo/geometry/vector.rb', line 58 def /(scalar) Vector[@x / scalar, @y / scalar] end |
#angle ⇒ Object
82 83 84 |
# File 'lib/nswtopo/geometry/vector.rb', line 82 def angle Math::atan2 @y, @x end |
#cross(other) ⇒ Object
78 79 80 |
# File 'lib/nswtopo/geometry/vector.rb', line 78 def cross(other) perp.dot other end |
#dot(other) ⇒ Object
70 71 72 |
# File 'lib/nswtopo/geometry/vector.rb', line 70 def dot(other) @x * other.x + @y * other.y end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/nswtopo/geometry/vector.rb', line 24 def inspect "{%s, %s}" % [@x, @y] end |
#norm ⇒ Object
86 87 88 |
# File 'lib/nswtopo/geometry/vector.rb', line 86 def norm Math::sqrt(dot self) end |
#normalised ⇒ Object
90 91 92 |
# File 'lib/nswtopo/geometry/vector.rb', line 90 def normalised self / norm end |
#perp ⇒ Object
74 75 76 |
# File 'lib/nswtopo/geometry/vector.rb', line 74 def perp Vector[-@y, @x] end |
#proj(other) ⇒ Object
94 95 96 |
# File 'lib/nswtopo/geometry/vector.rb', line 94 def proj(other) dot(other) / other.norm end |
#rotate_by(angle) ⇒ Object
36 37 38 39 40 |
# File 'lib/nswtopo/geometry/vector.rb', line 36 def rotate_by(angle) cos = Math::cos(angle) sin = Math::sin(angle) Vector[@x * cos - @y * sin, @x * sin + @y * cos] end |
#rotate_by_degrees(angle) ⇒ Object
42 43 44 |
# File 'lib/nswtopo/geometry/vector.rb', line 42 def rotate_by_degrees(angle) rotate_by(angle * Math::PI / 180.0) end |
#to_ary ⇒ Object
13 14 15 |
# File 'lib/nswtopo/geometry/vector.rb', line 13 def to_ary [@x, @y] end |
#to_d ⇒ Object
28 29 30 |
# File 'lib/nswtopo/geometry/vector.rb', line 28 def to_d Vector[@x.to_d, @y.to_d] end |
#to_f ⇒ Object
32 33 34 |
# File 'lib/nswtopo/geometry/vector.rb', line 32 def to_f Vector[@x.to_f, @y.to_f] end |