Sha256: 151076d75027573d56f2eec8b3f0569bbc6adc114313dba2de39144e6c8b9f61

Contents?: true

Size: 809 Bytes

Versions: 19

Compression:

Stored size: 809 Bytes

Contents

module GridGenerator
  class Rotator
    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

    attr_reader :angle, :matrix, :rotation_point

    # subtract rotation point to move point towards 0,0, rotate, then add to move back
    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
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
grid_generator-0.6.7 lib/grid_generator/rotator.rb
grid_generator-0.6.6 lib/grid_generator/rotator.rb
grid_generator-0.6.5 lib/grid_generator/rotator.rb
grid_generator-0.6.4 lib/grid_generator/rotator.rb
grid_generator-0.6.3 lib/grid_generator/rotator.rb
grid_generator-0.6.2 lib/grid_generator/rotator.rb
grid_generator-0.6.1 lib/grid_generator/rotator.rb
grid_generator-0.6.0 lib/grid_generator/rotator.rb
grid_generator-0.5.3 lib/grid_generator/rotator.rb
grid_generator-0.5.2 lib/grid_generator/rotator.rb
grid_generator-0.5.1 lib/grid_generator/rotator.rb
grid_generator-0.5.0 lib/grid_generator/rotator.rb
grid_generator-0.4.5 lib/grid_generator/rotator.rb
grid_generator-0.4.4 lib/grid_generator/rotator.rb
grid_generator-0.4.3 lib/grid_generator/rotator.rb
grid_generator-0.4.2 lib/grid_generator/rotator.rb
grid_generator-0.4.1 lib/grid_generator/rotator.rb
grid_generator-0.4.0 lib/grid_generator/rotator.rb
grid_generator-0.3.3 lib/grid_generator/rotator.rb