Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 107 lines (93 sloc) 2.332 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
# from Towelie
class Array
  def stepwise
    self.each do |element1|
      self.each do |element2|
        next if element1 == element2
        yield element1, element2
      end
    end
  end
end

require 'erb'

@communication_threshold_distance = 175

Velocity = Struct.new(:x, :y)
Circle = Struct.new(:x, :y, :radius, :velocity) do
  attr_accessor :connections
  def move
    self.x += self.velocity.x
    self.y += self.velocity.y
    bounce_at_boundaries
    self.connections = []
  end
  def reverse
    self.velocity.x = -1 * self.velocity.x
    self.velocity.y = -1 * self.velocity.y
  end
  def bounce_at_boundaries
    if self.x < 0
      self.x = 0
      reverse
    end
    if self.y < 0
      self.y = 0
      reverse
    end
    if self.x > 1280
      self.x = 1280
      reverse
    end
    if self.y > 720
      self.y = 720
      reverse
    end
  end
  def communicate(other)
    self.connections ? self.connections << other : self.connections = [other]
  end
end

def frame_id(number)
  "output/tween#{sprintf("%.4d", number)}"
end

def render(template)
  ERB.new(File.read(template)).result(binding)
end

# this is really another render method
def graphic(number)
  File.open("#{frame_id(number)}.svg", "w") do |file|
    file.write render("bit101_style.erb")
  end
  # system("convert #{frame_id(number)}.svg #{frame_id(number)}.jpg")
  system("java -jar ~/Downloads/batik-1.7/batik-rasterizer.jar #{frame_id(number)}.svg")
  File.unlink("#{frame_id(number)}.svg")
  print "+"
end

def start
  random_circle = lambda do
    Circle.new(rand(700),
               rand(500),
               rand(10) + 10,
               Velocity.new(rand(50) - 25,
                            rand(50) - 25))
  end

  @circles = []
  20.times {@circles << random_circle[]}
end



# here we go

start
(0..30).each do |number|
  @circles.each {|circle| circle.move}

  @circles.stepwise do |circle1, circle2|
    delta_x = circle1.x - circle2.x
    delta_y = circle1.y - circle2.y
    distance = Math.sqrt((delta_x ** 2) + (delta_y ** 2))
    # this if could also draw on persistent phenomena, e.g., heat, so that proximity to other nodes
    # created effects that only wore off gradually
    if distance < @communication_threshold_distance
      circle1.communicate(circle2)
    end
    print "."
  end
  graphic(number)
end
puts
Something went wrong with that request. Please try again.