Sha256: f93ba965621de0f475664a3620f9a441bebf10c760ecfbfa58ee40757cf6ea43

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require_relative 'boxlike'
require_relative 'point'

module Draught
  class BoundingBox
    include Boxlike

    attr_reader :paths

    def initialize(*paths)
      @paths = paths
    end

    def width
      x_max - x_min
    end

    def height
      y_max - y_min
    end

    def lower_left
      @lower_left ||= Point.new(x_min, y_min)
    end

    def translate(point)
      self.class.new(*paths.map { |path| path.translate(point) })
    end

    def transform(transformer)
      self.class.new(*paths.map { |path| path.transform(transformer) })
    end

    def zero_origin
      move_to(Point::ZERO)
    end

    def ==(other)
      paths == other.paths
    end

    def containers
      []
    end

    def box_type
      [:container]
    end

    private

    def x_max
      @x_max ||= upper_rights.map(&:x).max
    end

    def x_min
      @x_min ||= lower_lefts.map(&:x).min
    end

    def y_max
      @y_max ||= upper_rights.map(&:y).max
    end

    def y_min
      @y_min ||= lower_lefts.map(&:y).min
    end

    def lower_lefts
      @lower_lefts ||= paths.map(&:lower_left)
    end

    def upper_rights
      @upper_rights ||= paths.map(&:upper_right)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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