Created
December 18, 2010 05:32
-
-
Save nasser/746196 to your computer and use it in GitHub Desktop.
Polygonal Flower Code
This file contains 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
class Particle | |
attr_accessor :x, :y | |
attr_accessor :fx, :fy | |
attr_accessor :history | |
attr_accessor :lifespan | |
def initialize x, y | |
@x = x | |
@y = y | |
@lifespan = 255 | |
@history = [] | |
a = random 0, TwoPi | |
k = 2 | |
@fx = k * cos(a) | |
@fy = k * sin(a) | |
end | |
def update | |
@x += @fx | |
@y += @fy | |
@lifespan -= 2 | |
history << Particle.new(@x, @y) | |
history.shift if history.length > 200 | |
@fx *= 0.99 | |
@fy *= 0.99 | |
end | |
end | |
particles = [] | |
setup do | |
size 420 | |
# smoothing true | |
line_width 4 | |
alpha_blending true | |
title "Polygonal Flower" | |
particles << Particle.new(width/2, height/2) | |
particles << Particle.new(width/2, height/2) | |
background 0 | |
framerate 60 | |
end | |
update do | |
particles << Particle.new(width/2, height/2) | |
particles.each do |p| | |
p.update | |
end | |
particles = particles.reject { |p| p.lifespan < 5 } | |
end | |
draw do | |
particles.each_cons(2) do |a, b| | |
min_history = [a.history.length, b.history.length].min | |
for i in 0..min_history-1 | |
k = 240 | |
p = i / min_history.to_f | |
color k, k*p, k, a.lifespan*p | |
line a.history[i].x, a.history[i].y, b.history[i].x, b.history[i].y | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment