Class: Sangaku::Grid
- Inherits:
-
Object
- Object
- Sangaku::Grid
- Defined in:
- lib/sangaku/grid.rb
Instance Method Summary collapse
- #get_stars(poly) ⇒ Object
-
#initialize(xs, ys) ⇒ Grid
constructor
A new instance of Grid.
Constructor Details
#initialize(xs, ys) ⇒ Grid
Returns a new instance of Grid.
5 6 7 |
# File 'lib/sangaku/grid.rb', line 5 def initialize(xs, ys) @xs, @ys = xs, ys end |
Instance Method Details
#get_stars(poly) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sangaku/grid.rb', line 9 def get_stars(poly) raise Errors::OpenPolygonError.new unless poly.closed? stars = [] @xs.each do |x| ys = poly.select(x, nil).map { |line| line.get_y(x) } next if ys.length % 2 != 0 stars += ys.sort.each_slice(2).map do |s| height = s.reduce(:-).abs y = 0.5 * s.reduce(:+) xs = poly.select(nil, y).map { |line| line.get_x(y) } xs.sort! s = xs.zip(xs.rotate).find { |a, b| x.between?(a, b) } # TODO: de-hack next unless s width = 2 * [(x-s[0]).abs, (x-s[1]).abs].min Star.new([x, y], [width, height]) end end @ys.each do |y| xs = poly.select(nil, y).map { |line| line.get_x(y) } next if xs.length % 2 != 0 stars += xs.sort.each_slice(2).map do |s| width = s.reduce(:-).abs x = 0.5 * s.reduce(:+) ys = poly.select(x, nil).map { |line| line.get_y(x) } ys.sort! s = ys.zip(ys.rotate).find { |a, b| y.between?(a, b) } # TODO: de-hack next unless s height = 2 * [(y-s[0]).abs, (y-s[1]).abs].min Star.new([x, y], [width, height]) end end #TODO: de-hack stars.reject{|s| s.nil?} end |