lib/perfect_shape/multi_point.rb in perfect-shape-0.3.5 vs lib/perfect_shape/multi_point.rb in perfect-shape-0.4.0

- old
+ new

@@ -22,22 +22,31 @@ require 'perfect_shape/shape' module PerfectShape # Represents multi-point shapes like Line, Polygon, and Polyline module MultiPoint + class << self + 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 + end + attr_reader :points def initialize(points: []) self.points = points end # Sets points, normalizing to an Array of Arrays of (x,y) pairs as BigDecimal def points=(the_points) - unless the_points.first.is_a?(Array) - 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) - the_points = xs.zip(ys) - end + 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) ]