Created
February 19, 2017 01:05
-
-
Save JoshCheek/e00437fcbe2107f3b8e382fd3d3a7de3 to your computer and use it in GitHub Desktop.
Camera Shutter
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
require "graphics" | |
class Shutter < Graphics::Simulation | |
CLEAR_COLOR = [36, 30, 10] | |
def initialize | |
super 800, 450, 24 | |
@points = [ | |
[-1, -1], | |
[-w/2, h/2], | |
[-1, h], | |
[w/2, 3*h/2], | |
[w, h], | |
[3*w/2, h/2], | |
[w, -1], | |
[w/2, -h/2], | |
] | |
# @points = 100.times.map { |i| [ | |
# w/2*Math.cos(2*Math::PI*i/30.0)+w/2, | |
# h/2*Math.sin(2*Math::PI*i/30.0)+h/2, | |
# ] } | |
color.default_proc = -> h, k { k } | |
@drawers = [] | |
n = 500 | |
n.times do |i| | |
color = [255, 255, 255] | |
points = @points.dup | |
percent = | |
Math.sin(Math.sin(Math.sin(Math.sin(Math.sin( | |
Math.sin(Math::PI*(i/n.to_f%1)-Math::PI/2) | |
))))) | |
./( | |
Math.sin(Math.sin(Math.sin(Math.sin(Math.sin( | |
1 | |
))))) | |
).+(1)./(2) | |
@drawers[i] = [] | |
200.times do | |
points << points.first | |
crnt_points = points | |
@drawers[i] << lambda { draw_lines_between crnt_points, color } | |
points = percentages_between points, percent | |
end | |
end | |
end | |
def draw(ticks) | |
clear | |
@drawers[ticks % @drawers.length].each(&:call) | |
end | |
def dim((r, g, b), percent) | |
end_r, end_g, end_b = CLEAR_COLOR | |
[ r + (end_r-r)*percent, | |
g + (end_g-g)*percent, | |
b + (end_b-b)*percent, | |
] | |
end | |
def draw_lines_between(points, color) | |
points.each_cons 2 do |(x1, y1), (x2, y2)| | |
line x1, y1, x2, y2, color | |
end | |
end | |
# feels like if I think about calculus for a little bit, I might be able to | |
# come up with an equation for htis so that we don't have to repeatedly invoke it | |
def percentages_between(points, percent) | |
points.each_cons(2).map do |(x1, y1), (x2, y2)| | |
[x1+(x2-x1)*percent, y1+(y2-y1)*percent] | |
end | |
end | |
def rand_point | |
return [rand(w), rand(h)] | |
[rand(5*w)-2*w, rand(5*h)-2*h] | |
end | |
end | |
Shutter.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment