Last active
May 12, 2017 14:45
-
-
Save JoshCheek/ba21c4a5b225c08c1798c664c135c064 to your computer and use it in GitHub Desktop.
Walkers
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/217144282 | |
require 'graphics' | |
class Walkers < Graphics::Simulation | |
def initialize | |
super 800, 600, 24 | |
color.default_proc = -> h, k { k } | |
@walkers = 50.times.map do | |
length = 15 | |
colour = [ | |
rand(200)+55, | |
rand(200)+55, | |
rand(200)+55, | |
] | |
Walker.new rand(w), rand(h), rand(2*Math::PI), colour, length | |
end | |
end | |
def update(n) | |
@walkers.each &:update | |
end | |
def draw(n) | |
clear | |
@walkers.each { |w| w.draw self } | |
end | |
end | |
class Walker | |
attr_accessor :x, :y, :ø, :colour, :length, :segments | |
def initialize(x, y, ø, colour, length) | |
self.x = x | |
self.y = y | |
self.ø = ø | |
self.colour = colour | |
self.length = length | |
self.segments = [[x, y]] | |
end | |
TWO_PI = 2*Math::PI | |
def update | |
self.ø += rand(TWO_PI) | |
self.ø %= TWO_PI | |
self.x += 10 * Math.cos(ø) | |
self.y += 10 * Math.sin(ø) | |
segments.unshift [x, y] | |
segments.pop if segments.length > length | |
end | |
def draw(canvas) | |
segments.each_cons 2 do |(x1, y1), (x2, y2)| | |
canvas.line x1, y1, x2, y2, colour | |
end | |
end | |
end | |
Walkers.new.run |
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 Walkers < Graphics::Simulation | |
def initialize | |
super 800, 600, 24 | |
color.default_proc = -> h, k { k } | |
@walkers = 200.times.map do | |
length = 15 | |
colour = [ | |
rand(255), | |
rand(255), | |
rand(255), | |
] | |
Walker.new rand(w), rand(h), rand(2*Math::PI), colour, length | |
end | |
end | |
def update(n) | |
@walkers.each &:update | |
end | |
def draw(n) | |
# clear | |
@walkers.each { |w| w.draw self } | |
end | |
end | |
class Walker | |
attr_accessor :x, :y, :ø, :colour, :length, :segments | |
def initialize(x, y, ø, colour, length) | |
self.x = x | |
self.y = y | |
self.ø = ø | |
self.colour = colour | |
self.length = length | |
self.segments = [[x, y]] | |
end | |
TWO_PI = 2*Math::PI | |
def update | |
self.ø += rand(TWO_PI) | |
self.ø %= TWO_PI | |
self.x += 10 * Math.cos(ø) | |
self.y += 10 * Math.sin(ø) | |
segments.unshift [x, y] | |
segments.pop if segments.length > length | |
end | |
def draw(canvas) | |
segments.each_cons 2 do |(x1, y1), (x2, y2)| | |
canvas.line x1, y1, x2, y2, colour | |
canvas.line x1+1, y1, x2+1, y2, colour | |
canvas.line x1, y1+1, x2, y2+1, colour | |
end | |
end | |
end | |
Walkers.new.run |
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 Walkers < Graphics::Simulation | |
def initialize | |
super 1000, 600, 24 | |
color.default_proc = -> h, k { k } | |
@walkers = 30.times.map do | |
length = 15 | |
colour = [ | |
rand(200)+55, | |
rand(200)+55, | |
rand(200)+55, | |
] | |
Walker.new 30, rand(w), rand(h), rand(2*Math::PI), colour, length, w, h | |
end | |
end | |
def update(n) | |
@walkers.each &:update | |
end | |
def draw(n) | |
clear | |
@walkers.each { |w| w.draw self } | |
end | |
end | |
class Walker | |
attr_accessor :x, :y, :ø, :colour, :length, :segments, :stride, :xmax, :ymax | |
def initialize(stride, x, y, ø, colour, length, xmax, ymax) | |
self.x = x | |
self.y = y | |
self.stride = stride | |
self.ø = ø | |
self.colour = colour | |
self.length = length | |
self.xmax = xmax | |
self.ymax = ymax | |
self.segments = [[x, y]] | |
end | |
TWO_PI = 2*Math::PI | |
def update | |
self.ø += rand(TWO_PI) | |
self.ø %= TWO_PI | |
self.x += stride * Math.cos(ø) | |
self.y += stride * Math.sin(ø) | |
segments.unshift [x, y] | |
if x < 0 | |
self.x *= -1 | |
elsif xmax < x | |
self.x -= 2*(x-xmax) | |
end | |
if y < 0 | |
self.y *= -1 | |
elsif ymax < y | |
self.y -= 2*(y-ymax) | |
end | |
segments.pop if segments.length > length+2 | |
end | |
def draw(canvas) | |
percent = rand | |
points = segments | |
points = percentages_between points, percent | |
points = percentages_between points, percent | |
points = percentages_between points, percent | |
points = percentages_between points, percent | |
draw_lines_between canvas, points, colour, rand(3), rand(3) | |
end | |
def draw_lines_between(canvas, points, color, xoff, yoff) | |
points.each_cons 2 do |(x1, y1), (x2, y2)| | |
canvas.line x1, y1, x2, y2, color | |
canvas.line x1+xoff, y1+yoff, x2+xoff, y2+yoff, color | |
end | |
end | |
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 | |
end | |
Walkers.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment