Skip to content

Instantly share code, notes, and snippets.

@scholtes
Created May 20, 2016 00:13
Show Gist options
  • Save scholtes/b90000d1a5b80a8c405514d9abda4b39 to your computer and use it in GitHub Desktop.
Save scholtes/b90000d1a5b80a8c405514d9abda4b39 to your computer and use it in GitHub Desktop.
require 'chunky_png'
include ChunkyPNG
include Math
def encirlate(points, w1, w2, iterations)
iterations.times do |k|
puts "iter #{k}"
new_points = []
for i in 0...points.length
new_points << {
x: (1-w1)*points[i][:x] + w1*points[(i+1)%points.length][:x],
y: (1-w1)*points[i][:y] + w1*points[(i+1)%points.length][:y]
}
new_points << {
x: (1-w2)*points[i][:x] + w2*points[(i+1)%points.length][:x],
y: (1-w2)*points[i][:y] + w2*points[(i+1)%points.length][:y]
}
end
points = new_points
end
points
end
def draw_points(img, points, for_color)
for i in 0...points.length
img.compose_pixel(points[i][:x].round, points[i][:y].round, for_color)
print "." if i % (points.length/20) == 0
end
puts
img
end
# Run this in directory of this script
# ffmpeg -f image2 -i imgs\%d.png -framerate 30 out.gif
# Before doing so, make sure there is also:
# 1. A directory called 'imgs' in this directory
# 2. A 900x900 (or whatever) image called 'bg.png' in this directory
if __FILE__ == $0
FRAMES = 64
for i in 1..FRAMES
puts "====== imgs/#{i}.png ======"
points = [
{x: 75 + 50*cos(-2*PI*(1.0*i/FRAMES + 0/8.0)), y: 75 + 50*sin(-2*PI*(1.0*i/FRAMES + 0/8.0))},
{x: 825 + 50*cos(-2*PI*(1.0*i/FRAMES + 1/8.0)), y: 75 + 50*sin(-2*PI*(1.0*i/FRAMES + 1/8.0))},
{x: 825 + 50*cos(-2*PI*(1.0*i/FRAMES + 2/8.0)), y: 825 + 50*sin(-2*PI*(1.0*i/FRAMES + 2/8.0))},
{x: 75 + 50*cos(-2*PI*(1.0*i/FRAMES + 3/8.0)), y: 825 + 50*sin(-2*PI*(1.0*i/FRAMES + 3/8.0))}
]
#img = Image.new(900, 900, Color::WHITE)
img = Image.from_file(".\\bg.png")
points = encirlate(points,
(4.0 + 2.6*(0.5+0.5*sin(2*PI*i/FRAMES)))/8.0,
(4.0 - 2.6*(0.5+0.5*cos(2*PI*i/FRAMES)))/8.0,
17)
draw_points(img, points, Color.rgba(
128,
(64*(0.5+0.5*sin(2*PI*i/FRAMES))).round,
(32*(0.5+0.5*cos(2*PI*i/FRAMES))).round,
12)).save("imgs/#{i}.png")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment