module Snowy::DefaultDriver

Public Class Methods

render(size, triangles, background, color, outline, angle) click to toggle source
# File lib/snowy.rb, line 9
def self.render(size, triangles, background, color, outline, angle)
  img = Canvas.new(size, size, 0, 1, plt = [background, color])
  plt << outline if outline

  img.instance_exec do
    translate(width / 2.0, height / 2.0)
    scale(width / 32.0, height / 32.0)
    rotate_deg(angle)
    sqrt3 = Math.sqrt(3)
    [30, 90, 150, 210, 270, 330].each do |deg|
      push_matrix do
        rotate_deg(deg)
        scale(1, sqrt3)
        triangles.each do |t|
          triangle(*t)
        end
      end
    end

    # plot outline
    if outline
      w = width
      h = height
      pix = pixels
      (1 ... (h - 1)).step(1) do |py|
        py0 = py * w
        py1 = (py + 1) * w
        py2 = (py - 1) * w
        (1 ... (w - 1)).step(1) do |px|
          px1 = px + 1
          px2 = px - 1
          if pix.getbyte(py0 + px) == 0
            if pix.getbyte(py0 + px1) == 1 ||
               pix.getbyte(py0 + px2) == 1 ||
               pix.getbyte(py1 + px ) == 1 ||
               pix.getbyte(py2 + px ) == 1
              pix.setbyte(py0 + px, 2)
            end
          end
        end
      end
    end
    export_to_png
  end
end