Class: Draught::Vector
- Inherits:
-
Object
- Object
- Draught::Vector
- Defined in:
- lib/draught/vector.rb
Constant Summary collapse
- NULL =
new(0,0)
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
- .from_degrees_and_magnitude(degrees, magnitude) ⇒ Object
- .from_radians_and_magnitude(radians, magnitude) ⇒ Object
- .from_xy(x, y) ⇒ Object
- .translation_between(point_1, point_2) ⇒ Object
- .translation_to_zero(point) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(x, y) ⇒ Vector
constructor
A new instance of Vector.
- #to_transform ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Vector
Returns a new instance of Vector.
32 33 34 |
# File 'lib/draught/vector.rb', line 32 def initialize(x, y) @x, @y = x, y end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
30 31 32 |
# File 'lib/draught/vector.rb', line 30 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
30 31 32 |
# File 'lib/draught/vector.rb', line 30 def y @y end |
Class Method Details
.from_degrees_and_magnitude(degrees, magnitude) ⇒ Object
11 12 13 14 |
# File 'lib/draught/vector.rb', line 11 def self.from_degrees_and_magnitude(degrees, magnitude) radians = degrees * (Math::PI / 180) from_radians_and_magnitude(radians, magnitude) end |
.from_radians_and_magnitude(radians, magnitude) ⇒ Object
16 17 18 19 20 |
# File 'lib/draught/vector.rb', line 16 def self.from_radians_and_magnitude(radians, magnitude) x = Math.cos(radians) * magnitude y = Math.sin(radians) * magnitude new(x, y) end |
.from_xy(x, y) ⇒ Object
7 8 9 |
# File 'lib/draught/vector.rb', line 7 def self.from_xy(x, y) new(x, y) end |
.translation_between(point_1, point_2) ⇒ Object
26 27 28 |
# File 'lib/draught/vector.rb', line 26 def self.translation_between(point_1, point_2) from_xy(point_2.x - point_1.x, point_2.y - point_1.y) end |
Instance Method Details
#==(other) ⇒ Object
36 37 38 |
# File 'lib/draught/vector.rb', line 36 def ==(other) other.respond_to?(:to_transform) && other.x == x && other.y == y end |
#to_transform ⇒ Object
40 41 42 43 44 |
# File 'lib/draught/vector.rb', line 40 def to_transform @transform ||= Transformations::Affine.new( Matrix[[1, 0, x], [0, 1, y], [0, 0, 1]] ) end |