Sha256: d3c7733ef8d7b03c7c66fc191e725eb43db64981d06f5e3d26c7247371da823c

Contents?: true

Size: 765 Bytes

Versions: 13

Compression:

Stored size: 765 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)
      case obj
      when Matrix
        (matrix * (obj - rotation_point)) + rotation_point
      when GridGenerator::Line
        new_a = rotate(obj.a)
        new_b = rotate(obj.b)
        GridGenerator::Line.new(a: new_a, b: new_b)
      else
        raise ArgumentError, "Object must be Matrix or Line"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grid_generator-0.2.23 lib/grid_generator/rotator.rb
grid_generator-0.2.22 lib/grid_generator/rotator.rb
grid_generator-0.2.21 lib/grid_generator/rotator.rb
grid_generator-0.2.20 lib/grid_generator/rotator.rb
grid_generator-0.2.19 lib/grid_generator/rotator.rb
grid_generator-0.2.18 lib/grid_generator/rotator.rb
grid_generator-0.2.17 lib/grid_generator/rotator.rb
grid_generator-0.2.16 lib/grid_generator/rotator.rb
grid_generator-0.2.15 lib/grid_generator/rotator.rb
grid_generator-0.2.14 lib/grid_generator/rotator.rb
grid_generator-0.2.13 lib/grid_generator/rotator.rb
grid_generator-0.2.12 lib/grid_generator/rotator.rb
grid_generator-0.2.11 lib/grid_generator/rotator.rb