Sha256: b086e0bfbc72e04bfefaaae305d3b66a90635eabf86730733619f3e122411208

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# Drawing code stolen from here:
#  http://www.rubyquiz.com/quiz104.html

$:.unshift File.expand_path(File.dirname(__FILE__) + "/../../lib")
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../../ext")

require 'ray'

Ray::Game.new("Turtle") do
  register do
    add_hook :quit, method(:exit!)
  end

  scene :mandala do
    img = Ray::Image.new window.size
    img.turtle do
      def polygon(r, n)
        center = target.clip.center

        poly = []
        theta = 360 * (Math::PI / 180) / n

        n.times do |k|
          angle = k * theta
          poly << [center.x + r * sin(angle),
                   center.y + r * cos(angle)]
        end

        poly
      end

      def ray(from, to)
        pen_down
        go_to to

        pen_up
        go_to from
      end

      def fan(from, others)
        others.each { |to| ray from, to }
      end

      def mandala(r, n)
        poly = polygon(r, n)

        until poly.empty?
          v = poly.shift
          go_to v
          fan v, poly
        end
      end

      pen_up
      mandala(180, 24)
    end

    @sprite = sprite img

    render do |win|
      win.draw @sprite
    end
  end

  push_scene :mandala
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ray-0.2.1 samples/turtle/mandala.rb