Method: Grid.from_points

Defined in:
lib/cem/cruzzles.rb

.from_points(point_array, bounding_box = true, char = 'x', empty = ' ', &block) ⇒ Object

Given a list of Point2D, will create a Grid that contains all points.

By default the grid will be recentered using the ‘bottomleft’-most point. Set :bounding_box to false to get coordinates transferred as is.

By default all cells for which points are found are marked with an ‘x’. Set :char to another character or supply a block which receives the point and should return the character to use.

By default the empty cells are filled with spaces. Set :empty to something else to change this.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/cem/cruzzles.rb', line 306

def self.from_points(point_array, bounding_box=true, char='x', empty=' ', &block)
  
  min, max = Point2D.minmax(point_array)
  if !bounding_box
    min = Point2D.new(0,0)
  end
  
  result = Grid.new(max.x - min.x + 1, max.y - min.y + 1, empty)
  
  point_array.each { |p|
    result[p-min] = if block
      block.call(p)
    else
      char
    end
  }
  
  return result
end