Skip to content

Instantly share code, notes, and snippets.

@nasser
Created December 18, 2010 05:37
Show Gist options
  • Save nasser/746202 to your computer and use it in GitHub Desktop.
Save nasser/746202 to your computer and use it in GitHub Desktop.
Zajal Sketchbook
t = 0
v = 0
setup do
title "Ballerina"
smoothing true
framerate 60
end
update do
v += 0.005
t = sin(v)*100
end
def trig w, n
sin(2*n+w*0.05)*40*sin(n/50.0)
end
draw do
color 240
near = []
far = []
# determine x's
158.times do |n|
x = trig(t, n)
nx = trig(t+1, n)
p = x.abs/(40*sin(n/50.0))
p = 0.0 if p.nan?
# determine color and size
if nx > x
# far
c = 64 * (1-p) + 200*p
r = 0.5 + 0.5 * p
far << [x, n-160/2, r, c]
else
# near
c = 240 * (1-p) + 200*p
r = 1.5 - 0.5 * p
near << [x, n-160/2, r, c]
end
end
# draw points
matrix do
translate width/2, height/2
scale width/180.0, height/180.0
rotate 15
far.each do |point|
x, y, r, c = point
color c
circle x, y, r
end
near.each do |point|
x, y, r, c = point
color c
circle x, y, r
end
end
end
num = 2000
range = 6
a = []
setup do
size 420, 500
title "Brownian"
a << Point.new(width/2, height/2)
end
draw do
a << a.last.clone + Point.rand(range)
a.last.clamp! 0, width, 0, height
a.shift if a.length > num
for i in 0..(a.length-2)
color i.to_f/a.length * 240 + 32
line a[i].x, a[i].y, a[i+1].x, a[i+1].y
end
end
draw do
particles.each_pair do |a, b|
min_history = min a.history.length, b.history.length
for i in 0..min_history-1
k = 240
- p = i / min_history.to_f
+ p = 1 - 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
class Particle
def initialize x, y
@x, @y = x, 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 << self.clone
history.shift if history.length > 200
@fx *= 0.99
@fy *= 0.99
end
end
particles = []
setup do
size 420
title "Polygonal Flower"
framerate 60
alpha_blending true
line_width 4
background 0
particles << Particle.new(width/2, height/2)
particles << Particle.new(width/2, height/2)
end
update do
particles << Particle.new(width/2, height/2)
particles.each do |p|
p.update
end
particles.reject! do |p|
p.lifespan < 5
end
end
draw do
particles.each_pair do |a, b|
min_history = min a.history.length, b.history.length
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
require "vector-field"
vf = nil
particles = []
setup do
size 420
title "Mauve"
alpha_blending true
background 0
vf = VectorField.new 420, 420
vf.randomize!
end
update do
once_every(30) { vf.randomize! 100 }
particles << Point.new(width/2, random_height)
particles.shift if particles.size > 5000
particles.reject! { |p| p.x < 0 or p.x > width or p.y < 0 or p.y > height }
particles.each do |p|
dx, dy = vf[p.x, p.y].x, vf[p.x, p.y].y
p.x = p.x * 0.99 + (p.x + dx) * 0.01
p.y = p.y * 0.99 + (p.y + dy) * 0.01
end
end
draw do
matrix do
translate 150, -70
rotate 30
background 32, 0, 32
particles.each_with_index do |p, i|
dy = 1 - (p.y - height/2).abs / (height/2)
q = i/particles.size.to_f
r = 1 - q
color 255 * q, 255 * r, 128, 8 * q * dy
circle p.x, p.y, 25 * q
end
end
end
class VectorField
def initialize w, h, res=10
p w, h
@res = res
@ext_w, @ext_h = w.to_f, h.to_f
@int_w, @int_h = @ext_w/res, @ext_h/res
@vectors = Hash.new()
for x in 0..@int_w
for y in 0..@int_h
@vectors[[x, y]] = Point.new(0, 0)
end
end
end
def [] x, y
x_pct, y_pct = x/@ext_w, y/@ext_h
# puts "#{x}/#{@ext_w}, #{y}/#{@ext_h}"
return nil if not x_pct.between?(0, 1) or not y_pct.between?(0, 1)
field_x = (x_pct * @int_w).to_i
field_y = (y_pct * @int_h).to_i
return @vectors[[field_x, field_y]]
end
def randomize! amt=20
for x in 0..self.int_w
for y in 0..self.int_h
@vectors[[x, y]].x = random -amt, amt
@vectors[[x, y]].y = random -amt, amt
end
end
end
def draw
for x in 0..vf.int_w
for y in 0..vf.int_h
x1, y1 = x * @res, y * @res
x2, y2 = x1 + self[x1, y1].x, y1 + self[x1, y1].y
line x1, y1, x2, y2
end
end
end
def inspect
"VectorField[#{object_id}]"
end
end
pts = []
setup do
size 480
k = 20
p = 30
background :white
k.times do |i|
x = p+(i.to_f/k*(1..0))**5*(width-p*2)
y = p+(height-p*2)/k * i
pts << Point.new(x, y)
end
end
update do
pts.each do |a|
a.x += signed_noise(a.x, time*0.001)
a.y += signed_noise(a.y, time*0.001)
end
end
draw do
ofst = Vector.new 16, 26
color 100
pts.combination 3 do |a, b, c|
triangle a+ofst, b+ofst, c+ofst
end
pts.combination 3 do |a, b, c|
color noise(avg(a.x, b.x, a.y, b.y) * 0.1, time*0.0001) * 255
triangle a, b, c
end
end
t = 0
setup do
# smoothing true
alpha_blending true
circle_resolution 64
background 0
title "Wormsign"
framerate 60
width 500
end
draw do
color 255
t += 0.1
80.times do |n|
n *= 0.1
disc 130+n*30+sin(t+n)*40, 230+sin(t+n)*40, 40+cos(n)*10, 5
end
end
def disc x, y, r, res
res.times do |n|
p = n/res.to_f
q = 1-p
color 0, 40*p
circle x, y, (r*1.5)*q
end
color 230, 25, 25
circle x, y, r
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment