Sha256: 0c2317c350c9326c4aacc9cc38d523f91bf09d3ccdbefd02d8c535954f6f0f15

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 Bytes

Contents

module Overlap
  class Union

    attr_reader :segment, :quantity, :intersections, :quantity_with_intersections, :intersection_quantity, :segments

    def initialize(overlapped_segments)
      @overlapped_segments = overlapped_segments
      @segments = overlapped_segments[:segments].sort
      build!
    end

    private

    def build!
      @segment = @overlapped_segments[:union]
      @quantity = segment.quantity

      previous_value = nil
      @intersections = segments.sort.map do |_segment|
        intersection = nil
        if previous_value
          intersection = previous_value - _segment.start_position
        end
        previous_value = _segment.end_position
        intersection
      end.compact

      @intersection_quantity = intersections.reduce(:+)&.round(3) || 0
      @quantity_with_intersections = segments.map(&:quantity).reduce(:+)&.round(3) || 0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
overlap-0.2.0 lib/overlap/union.rb