Class: GridGenerator::Rotator

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/rotator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(angle:, rotation_point:) ⇒ Rotator

Returns a new instance of Rotator.



3
4
5
6
7
8
9
10
# File 'lib/grid_generator/rotator.rb', line 3

def initialize(angle:, rotation_point:)
  @angle = angle 
  @matrix = Matrix[
    [Math.cos(angle), -1*Math.sin(angle)],
    [Math.sin(angle), Math.cos(angle)]
  ]
  @rotation_point = rotation_point
end

Instance Attribute Details

#angleObject (readonly)

Returns the value of attribute angle.



12
13
14
# File 'lib/grid_generator/rotator.rb', line 12

def angle
  @angle
end

#matrixObject (readonly)

Returns the value of attribute matrix.



12
13
14
# File 'lib/grid_generator/rotator.rb', line 12

def matrix
  @matrix
end

#rotation_pointObject (readonly)

Returns the value of attribute rotation_point.



12
13
14
# File 'lib/grid_generator/rotator.rb', line 12

def rotation_point
  @rotation_point
end

Instance Method Details

#rotate(obj) ⇒ Object

subtract rotation point to move point towards 0,0, rotate, then add to move back



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/grid_generator/rotator.rb', line 15

def rotate(obj)
  return obj if angle == 0 
  case obj
  when Matrix
    (matrix * (obj - rotation_point)) + rotation_point
  when GridGenerator::BaseLine
    new_a = rotate(obj.a)
    new_b = rotate(obj.b)
    GridGenerator::BaseLine.new(a: new_a, b: new_b)
  else
    raise ArgumentError, "Object must be Matrix or BaseLine"
  end
end