Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active November 20, 2016 08:28
Show Gist options
  • Save JoshCheek/8fe33d170c7d6181d57ac9ec53dba6c4 to your computer and use it in GitHub Desktop.
Save JoshCheek/8fe33d170c7d6181d57ac9ec53dba6c4 to your computer and use it in GitHub Desktop.
Playing with Ryan's Graphics lib
# https://vimeo.com/192116528
require "graphics"
class Clock < Graphics::Simulation
def initialize
@radius = 400
@margin = 20
@increment_rate = 0.0009
@num_lines = 1600
@line_width = 2
@start_num = (@num_lines/2.0)-1
@finish_num = (@num_lines/3.0)+1
super 1440, 900
color.default_proc = -> h, k { k }
end
def draw(iteration)
clear
multiplier = @start_num + (@finish_num-@start_num)*progress(iteration)
# p time: Time.now.strftime('%H:%M:%S'), progress: progress(iteration)
@num_lines.times do |index|
percent = index.to_f / @num_lines
start = CIRCUMFERENCE * percent
finish = start * multiplier
green = (Math.cos(start+TILT) * 127.5).to_i + 128
color = rgba 255, green, 0 # omitting blue for now b/c https://github.com/zenspider/graphics/issues/23
line x(start), y(start), x(finish), y(finish), color
line x(start), y(start), x(finish), 1+y(finish), color # thicken the line a bit
end
end
private
CIRCUMFERENCE = 2 * Math::PI
TILT = Math::PI
def x(rad, radius: @radius, x_mid: w/2)
Math.cos(rad) * radius + x_mid
end
def y(rad, radius: @radius, y_mid: h/2)
Math.sin(rad) * radius + y_mid
end
def rgba(r, g, b, a=255)
screen.format.map_rgba(r.to_i, g.to_i, b.to_i, a.to_i)
end
# This function determines how quickly we move through the different parts of the animation
# Particularly, we want it to move pretty quickly through the middle bits so that it doesn't
# take 20 minutes to watch, but we also want it to move slowly through the end-points,
# in order to enjoy the satisfaction of all the lines matching up. I explained the logic
# of how it works here: http://math.stackexchange.com/a/2019541/258281
# and an image of this vs sine wave @ https://twitter.com/josh_cheek/status/799520755801268224
def progress(iteration)
numerator = Math.sin Math.sin Math.sin iteration*@increment_rate
@progress_denominator ||= Math.sin Math.sin 1
0.5 + (numerator / @progress_denominator) / 2
end
end
Clock.new.run if $0 == __FILE__
# https://vimeo.com/192214982
require "graphics"
class Clock < Graphics::Simulation
def initialize
@radius = 400
@margin = 20
@increment_rate = 0.005
@num_lines = 1600
@start_num = (@num_lines/2.0)-1 # interesting points in the progression
@finish_num = (@num_lines/3.0)+1 # emphasize them by turning around at them
super 1440, 900, 24 # my screen resolution, and 24 bpp (https://github.com/zenspider/graphics/issues/23)
color.default_proc = -> h, k { k } # so we don't have to rely on named colours
end
def draw(iteration)
clear
multiplier = @start_num + (@finish_num-@start_num)*progress(iteration)
@num_lines.times do |index|
start = CIRCUMFERENCE * index.to_f / @num_lines
finish = start * multiplier
line x(start, x_mid: w/2-@radius*0.7),
y(start, y_mid: h/2+@margin),
x(finish, x_mid: w/2+@radius*0.7),
y(finish, y_mid: h/2+@margin),
:white
end
end
private
CIRCUMFERENCE = 2 * Math::PI
def x(rad, radius: @radius, x_mid: w/2)
Math.cos(rad) * radius + x_mid
end
def y(rad, radius: @radius, y_mid: h/2)
Math.sin(rad) * radius + y_mid
end
# This function determines how quickly we move through the different parts of the animation
# Particularly, we want it to move pretty quickly through the middle bits so that it doesn't
# take 20 minutes to watch, but we also want it to move slowly through the end-points,
# in order to enjoy the satisfaction of all the lines matching up. I explained the logic
# of how it works here: http://math.stackexchange.com/a/2019541/258281
def progress(iteration)
numerator = Math.sin Math.sin Math.sin iteration*@increment_rate
@progress_denominator ||= Math.sin Math.sin 1
0.5 + numerator / @progress_denominator / 2
end
end
Clock.new.run
# https://vimeo.com/192217151
require "graphics"
class Clock < Graphics::Simulation
def initialize
@radius = 400
@margin = 20
@increment_rate = 0.005
@num_lines = 1600
@start_num = 0
@finish_num = 2*@num_lines
super 1440, 900, 24 # my screen resolution, and 24 bpp (https://github.com/zenspider/graphics/issues/23)
color.default_proc = -> h, k { k } # so we don't have to rely on named colours
end
def draw(iteration)
clear
multiplier = @start_num + (@finish_num-@start_num)*progress(iteration)
@num_lines.times do |index|
percent = index.to_f / @num_lines
start = CIRCUMFERENCE * percent
finish = start * multiplier
blue = (Math.cos(start) * 127.5).to_i + 128
green = (Math.sin(start) * 50.5).to_i + 128
red = 255 - blue
color = rgba red, green, blue
line x(start, x_mid: w/2, radius: @radius),
y(start, y_mid: h/2+@margin, radius: @radius),
x(finish, x_mid: w/2+@radius*0.5, radius: @radius*0.25),
y(finish, y_mid: h/2+@radius*0.5, radius: @radius*0.25),
color
end
end
private
CIRCUMFERENCE = 2 * Math::PI
def x(rad, radius: @radius, x_mid: w/2)
Math.cos(rad) * radius + x_mid
end
def y(rad, radius: @radius, y_mid: h/2)
Math.sin(rad) * radius + y_mid
end
def rgba(r, g, b, a=255)
screen.format.map_rgba(r.to_i, g.to_i, b.to_i, a.to_i)
end
# This function determines how quickly we move through the different parts of the animation
# Particularly, we want it to move pretty quickly through the middle bits so that it doesn't
# take 20 minutes to watch, but we also want it to move slowly through the end-points,
# in order to enjoy the satisfaction of all the lines matching up. I explained the logic
# of how it works here: http://math.stackexchange.com/a/2019541/258281
def progress(iteration)
numerator = Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin iteration*@increment_rate
@progress_denominator ||= Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin Math.sin 1
0.5 + numerator / @progress_denominator / 2
end
end
Clock.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment