Last active
October 25, 2018 06:16
-
-
Save JoshCheek/b6667bf61610d0ae5cd1d071b6cb8e71 to your computer and use it in GitHub Desktop.
Undulating Waves
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks like a broke this one. You are the only person to find the array as color thing and it was removed.