Sha256: 295d9884add93fe460be387e2da8393f4bf8582479a95bfa5323ad2712b06b30

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'matrix'
require_relative 'point'
require_relative 'transformations/affine'
require_relative 'transformations/proclike'
require_relative 'transformations/composer'

module Draught
  module Transformations
    extend self

    MM_TO_PT = 2.8346456692913

    def mm_to_pt
      Affine.new(Matrix[
        [MM_TO_PT, 0, 0],
        [0, MM_TO_PT, 0],
        [0, 0, 1]
      ])
    end

    def x_axis_reflect
      Affine.new(Matrix[
        [1,0,0],
        [0,-1,0],
        [0,0,1]
      ])
    end

    def y_axis_reflect
      Affine.new(Matrix[
        [-1,0,0],
        [0,1,0],
        [0,0,1]
      ])
    end

    def xy_axis_reflect
      Composer.compose(x_axis_reflect, y_axis_reflect)
    end

    def scale(factor)
      Transformations::Affine.new(Matrix[
        [factor, 0, 0],
        [0, factor, 0],
        [0, 0, 1]
      ])
    end

    def rotate(radians)
      cos = Math.cos(radians)
      sin = Math.sin(radians)
      Transformations::Affine.new(Matrix[
        [cos, -sin, 0],
        [sin, cos, 0],
        [0, 0, 1]
      ])
    end

    def round_to_n_decimal_places(n)
      Proclike.new(->(p) { [p.x.round(n), p.y.round(n)] })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
draught-0.1.0 lib/draught/transformations.rb