Sha256: eb47ea1c39c61d0651de4884a20a7a86e84968d7a785ab14d40fa319ebbf9ddf

Contents?: true

Size: 946 Bytes

Versions: 1

Compression:

Stored size: 946 Bytes

Contents

require_relative './vector'
require_relative './pointlike'
require 'matrix'

module Draught
  class Point
    def self.from_matrix(matrix)
      x, y = matrix.to_a.flatten
      Point.new(x, y)
    end

    include Pointlike

    attr_reader :x, :y

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

    def point_type
      :point
    end

    def ==(other)
      other.point_type == point_type &&
        other.x == x && other.y == y
    end

    def approximates?(other, delta)
      other.point_type == point_type &&
        ((other.x - x).abs <= delta) &&
        ((other.y - y).abs <= delta)
    end

    def translate(vector)
      transform(vector.to_transform)
    end

    def translation_to(point)
      Vector.translation_between(self, point)
    end

    def to_matrix
      @matrix ||= Matrix[[x],[y],[1]].freeze
    end

    def transform(transformation)
      transformation.call(self)
    end

    ZERO = new(0, 0)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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