module PerfectShape::MultiPoint

Represents multi-point shapes like Line, Polygon, and Polyline

Attributes

points[R]

Public Class Methods

new(points: []) click to toggle source
# File lib/perfect_shape/multi_point.rb, line 42
def initialize(points: [])
  self.points = points
end
normalize_point_array(the_points) click to toggle source
# File lib/perfect_shape/multi_point.rb, line 28
def normalize_point_array(the_points)
  if the_points.all? {|the_point| the_point.is_a?(Array)}
    the_points
  else
    the_points = the_points.flatten
    xs = the_points.each_with_index.select {|n, i| i.even?}.map(&:first)
    ys = the_points.each_with_index.select {|n, i| i.odd?}.map(&:first)
    xs.zip(ys)
  end
end

Public Instance Methods

max_x() click to toggle source
# File lib/perfect_shape/multi_point.rb, line 66
def max_x
  points.map(&:first).max
end
max_y() click to toggle source
# File lib/perfect_shape/multi_point.rb, line 70
def max_y
  points.map(&:last).max
end
min_x() click to toggle source
# File lib/perfect_shape/multi_point.rb, line 58
def min_x
  points.map(&:first).min
end
min_y() click to toggle source
# File lib/perfect_shape/multi_point.rb, line 62
def min_y
  points.map(&:last).min
end
points=(the_points) click to toggle source

Sets points, normalizing to an Array of Arrays of (x,y) pairs as BigDecimal

# File lib/perfect_shape/multi_point.rb, line 47
def points=(the_points)
  the_points = MultiPoint.normalize_point_array(the_points)
  @points = the_points.map do |pair|
    [
      pair.first.is_a?(BigDecimal) ? pair.first : BigDecimal(pair.first.to_s),
      pair.last.is_a?(BigDecimal) ? pair.last : BigDecimal(pair.last.to_s)
    ]
  end
  @points
end