Sha256: 060c8195e72d048ab9244fe6f10b4e4dc5cfa647661f77f7c42ce43ae039da67

Contents?: true

Size: 600 Bytes

Versions: 1

Compression:

Stored size: 600 Bytes

Contents

# frozen_string_literal: true

module Sc2
  # A set geometric/map/math methods for unit group
  class UnitGroup
    # Returns the center (average) position of all units or nil if the group is empty.
    # Outliers effect this point
    # @return [Api::Point2D, nil]
    def pos_centroid
      return nil if size == 0
      size = @units.size
      sum_x = 0.0
      sum_y = 0.0
      i = 0

      while i < size
        unit = at(i)
        sum_x += unit.pos.x.to_f
        sum_y += unit.pos.y.to_f
        i += 1
      end

      Api::Point2D[sum_x / size.to_f, sum_y / size.to_f]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sc2ai-0.3.0 lib/sc2ai/unit_group/geo_ext.rb