Class: Draught::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/draught/vector.rb

Constant Summary collapse

NULL =
new(0,0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#xObject (readonly)

Returns the value of attribute x.



30
31
32
# File 'lib/draught/vector.rb', line 30

def x
  @x
end

#yObject (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

.translation_to_zero(point) ⇒ Object



22
23
24
# File 'lib/draught/vector.rb', line 22

def self.translation_to_zero(point)
  translation_between(point, Point::ZERO)
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_transformObject



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