Skip to content

Commit

Permalink
refactoring the multiple particle system complex example to use inner…
Browse files Browse the repository at this point in the history
… classes

Former-commit-id: 8eab6d0
Former-commit-id: 6f3875d11dd22432348791f0bbd05e0df9a0b2e7
  • Loading branch information
jashkenas committed Apr 2, 2009
1 parent 6dd8e44 commit 613b7ba
Showing 1 changed file with 74 additions and 73 deletions.
147 changes: 74 additions & 73 deletions samples/processing_app/topics/simulate/multiple_particle_systems.rb
Expand Up @@ -5,10 +5,51 @@
# Each burst is one instance of a particle system with Particles and
# CrazyParticles (a subclass of Particle).

module Runnable
def run
self.reject! { |item| item.dead? }
self.each { |item| item.run }
end
end


class Vector
attr_accessor :x, :y

def initialize(x, y)
@x, @y = x, y
end

require 'ruby-processing'
def +(other)
return Vector.new(@x + other, @y + other) if other.is_a?(Numeric)
return Vector.new(@x + other.x, @y + other.y) if other.is_a?(Vector)
self
end

def heading
-1 * Math::atan2(-@y, @x)
end

def magnitude
@x * @x + @y * @y
end
end

class MultipleParticleSystems < Processing::App

class ParticleSystem < Array
include Runnable
alias_method :dead?, :empty?

def initialize(number, origin)
super()
@origin = origin
kind = rand < 0.5 ? Sketch::Particle : Sketch::CrazyParticle
number.times { self << kind.new(origin) }
end
end


class Sketch < Processing::App
def setup
smooth
color_mode(RGB, 255, 255, 255, 100)
Expand All @@ -24,28 +65,12 @@ def draw
end

def mouse_pressed
@particle_systems << ParticleSystem.new(rand(21) + 5, Vector.new(mouse_x, mouse_y))
origin = rand(21) + 5
vector = Vector.new(mouse_x, mouse_y)
@particle_systems << ParticleSystem.new(origin, vector)
end

module Runnable
def run
self.reject! { |item| item.dead? }
self.each { |item| item.run }
end
end

class ParticleSystem < Array
include Runnable
alias_method :dead?, :empty?

def initialize(number, origin)
super()
@origin = origin
kind = rand < 0.5 ? Particle : CrazyParticle
number.times { self << kind.new(origin) }
end
end



class Particle
def initialize(origin)
@origin = origin
Expand Down Expand Up @@ -76,30 +101,31 @@ def dead?
end

def render
$app.stroke(255, @lifespan)
$app.fill(100, @lifespan)
$app.ellipse(@origin.x, @origin.y, @radius, @radius)
stroke(255, @lifespan)
fill(100, @lifespan)
ellipse(@origin.x, @origin.y, @radius, @radius)
end

def render_velocity_vector
scale = 10
arrow_size = 4

$app.push_matrix

$app.translate(@origin.x, @origin.y)
$app.rotate(@velocity.heading)
push_matrix
translate(@origin.x, @origin.y)
rotate(@velocity.heading)

length = @velocity.magnitude * scale

$app.line 0, 0, length, 0
$app.line length, 0, length - arrow_size, arrow_size / 2
$app.line length, 0, length - arrow_size, -arrow_size / 2

$app.pop_matrix
line 0, 0, length, 0
line length, 0, length - arrow_size, arrow_size / 2
line length, 0, length - arrow_size, -arrow_size / 2
pop_matrix
end
end



class CrazyParticle < Particle
def initialize(origin)
super
Expand All @@ -123,44 +149,19 @@ def grow
end

def render_rotation_line
$app.push_matrix

$app.translate(@origin.x, @origin.y)
$app.rotate(@theta)

$app.stroke(255, @lifespan)

$app.line(0, 0, 25, 0)

$app.pop_matrix
end
end

class Vector
attr_accessor :x, :y

def initialize(x, y)
@x, @y = x, y
end

def +(other)
if other.is_a?(Numeric)
Vector.new(@x + other, @y + other)
elsif other.is_a?(Vector)
Vector.new(@x + other.x, @y + other.y)
else
self
end
end

def heading
-1 * Math::atan2(-@y, @x)
end

def magnitude
@x * @x + @y * @y
push_matrix

translate(@origin.x, @origin.y)
rotate(@theta)

stroke(255, @lifespan)

line(0, 0, 25, 0)

pop_matrix
end
end

end

$app = MultipleParticleSystems.new :width => 640, :height => 340, :title => 'MultipleParticleSystems'
Sketch.new :width => 640, :height => 340

0 comments on commit 613b7ba

Please sign in to comment.