Sha256: df602d7c48c76d8d936ad68c53ecdf16565d5adea9fc256903769e4c2203e5b0

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'matrix'
require_relative './transformations/affine'
require_relative './point'

module Draught
  class Vector
    def self.from_xy(x, y)
      new(x, y)
    end

    def self.from_degrees_and_magnitude(degrees, magnitude)
      radians = degrees * (Math::PI / 180)
      from_radians_and_magnitude(radians, magnitude)
    end

    def self.from_radians_and_magnitude(radians, magnitude)
      x = Math.cos(radians) * magnitude
      y = Math.sin(radians) * magnitude
      new(x, y)
    end

    def self.translation_to_zero(point)
      translation_between(point, Point::ZERO)
    end

    def self.translation_between(point_1, point_2)
      from_xy(point_2.x - point_1.x, point_2.y - point_1.y)
    end

    attr_reader :x, :y

    def initialize(x, y)
      @x, @y = x, y
    end

    def ==(other)
      other.respond_to?(:to_transform) && other.x == x && other.y == y
    end

    def to_transform
      @transform ||= Transformations::Affine.new(
        Matrix[[1, 0, x], [0, 1, y], [0, 0, 1]]
      )
    end

    NULL = new(0,0)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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