Created
January 31, 2017 21:58
-
-
Save JoshCheek/360cb2d9b8c00f62c7399c488e4f19b1 to your computer and use it in GitHub Desktop.
Procedural circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://codepen.io/clawtros/pen/yVONbR | |
require 'graphics' | |
SIZE = 0.5 | |
Node = Struct.new :x, :y, :∆x, :∆y, :color, :lifespan, :on_death do | |
def successor | |
new∆x = ∆x*0.98 + (rand - 0.5) / 2 | |
new∆y = ∆y*0.98 + (rand - 0.5) / 2 | |
Node.new x+new∆x, y+new∆y, | |
new∆x, new∆y, | |
color, | |
lifespan-1, | |
on_death | |
end | |
def draw(canvas) | |
canvas.line x, y, x+∆x, y+∆y, color | |
end | |
def alive? | |
0 <= lifespan | |
end | |
end | |
class Velo < Graphics::Simulation | |
include Math | |
attr_accessor :nodes | |
def initialize(width, height) | |
super width, height, 24 | |
color.default_proc = -> h, k { k } | |
self.nodes = 0.step(by: 2*PI/width, to: 2*PI).map do |angle| | |
x = cos(angle) * height / 5 | |
y = sin(angle) * height / 5 | |
Node.new x + width/2, y + height/2, | |
0, 0, | |
[255, 0, 0], | |
10_000 * rand, | |
:explode | |
end | |
end | |
def update(n) | |
self.nodes = nodes.each_with_object([]) do |node, new_nodes| | |
successor = node.successor | |
if successor.alive? | |
new_nodes << successor | |
elsif successor.on_death == :explode | |
rand(20).times do | |
new_nodes << Node.new( | |
node.x, node.y, | |
node.∆x + (rand - 0.5), node.∆y + (rand - 0.5), | |
[255, 255, 255], | |
(100 * rand).to_i, | |
:nothing | |
) | |
end | |
end | |
end | |
end | |
def draw(n) | |
nodes.each { |node| node.draw self } | |
end | |
end | |
Velo.new(1200, 900).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment