Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active October 25, 2018 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/b6667bf61610d0ae5cd1d071b6cb8e71 to your computer and use it in GitHub Desktop.
Save JoshCheek/b6667bf61610d0ae5cd1d071b6cb8e71 to your computer and use it in GitHub Desktop.
Undulating Waves
# video @ https://vimeo.com/218743733
require 'graphics'
class Waves < Graphics::Simulation
include Math
def initialize
super 600, 600, 24
self.color.default_proc = -> h, k { k }
end
def draw(t)
# clear
num_lines.times.map do |i|
relative_t = t/5.0+i
offset = i*segment_len + segment_len/2
freq = 0.1/1.75
ø = 2*Math::PI*i/num_lines.pred+t*freq
color1 = [c(ø), c(ø+PI/2), c(-ø)]
color2 = [c(ø+PI/2), c(ø), c(-ø)]
horizontal_line relative_t, freq, offset, color1.map { |n| n/2 }, color1
vertical_line relative_t*0.8, freq, offset, color2.map { |n| n/2 }, color2
end
end
def c(ø)
sin(ø/2+0.5).+(1)./(2).*(255).to_i
end
def num_lines
@num_lines ||= 20
end
def segment_len
@segment_len ||= h/num_lines
end
def horizontal_line(t, freq, y_base, color1, color2)
pairs(t, freq, y_base).each do |(x1, y1), (x2, y2)|
line x1, y1-3, x2, y2-3, color1
line x1, y1, x2, y2, color2
line x1, y1+3, x2, y2+3, color1
end
end
def vertical_line(t, freq, x_base, color1, color2)
pairs(t, freq, x_base).each do |(y1, x1), (y2, x2)|
line x1-3, y1, x2-3, y2, color1
line x1, y1, x2, y2, color2
line x1+3, y1, x2+3, y2, color1
end
end
def pairs(t, freq, base)
num_lines.succ.succ.times.map do |i|
val1 = segment_len*i - segment_len/2
val2 = base + segment_len*sin(2*PI*(t+i)*freq)/2
[val1, val2]
end.each_cons(2)
end
end
Waves.new.run
@zenspider
Copy link

looks like a broke this one. You are the only person to find the array as color thing and it was removed.

@zenspider
Copy link

I did a horrible thing and changed your default_proc to create and register the color properly. It doesn't quite work until the first full undulation is done, but then it looks fine (?):

screen shot 2018-10-24 at 23 16 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment