forked from ashbb/shoes_tutorial_html
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added gallery 10: A Very Simple Boids
- Loading branch information
Showing
9 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# gallery10-image.rb | ||
class Shoes::Image | ||
[:vel, :pos].each do |m| | ||
define_method(m){style[m]} | ||
define_method("#{m}="){|arg| style m => arg} | ||
end | ||
|
||
[:x, :y].each_with_index{|m, i| define_method(m){pos[i].to_i}} | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# gallery10-rules.rb | ||
N = 12 | ||
BOUND = 3 | ||
|
||
module Rules | ||
def rule1 boid | ||
c = @boids.collect{|b| b.pos}.inject{|i, j| i + j} - boid.pos | ||
c *= 1 / (N - 1.0) | ||
(c - boid.pos) * 0.01 | ||
end | ||
|
||
def rule2 boid | ||
c = Vector[0, 0] | ||
@boids.each do |b| | ||
dist = b.pos - boid.pos | ||
(c -= b.pos - boid.pos) if (b != boid and dist[0].to_i.abs < 5 and dist[1].to_i.abs < 5) | ||
end | ||
c | ||
end | ||
|
||
def rule3 boid | ||
c = @boids.collect{|b| b.vel}.inject{|i, j| i + j} - boid.vel | ||
c *= 1 / (N - 1.0) | ||
(c - boid.vel) * (1 / 16.0) | ||
end | ||
|
||
def rule4 boid | ||
vx, vy = 0, 0 | ||
x, y = boid.pos[0], boid.pos[1] | ||
vx = BOUND if x < 20 | ||
vx = -BOUND if x > 580 | ||
vy = BOUND if y < 20 | ||
vy = -BOUND if y > 480 | ||
Vector[vx, vy] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# gallery10.rb | ||
# http://www.vergenet.net/~conrad/boids/pseudocode.html | ||
require 'matrix' | ||
require 'gallery10-image' | ||
require 'gallery10-rules' | ||
|
||
Shoes.app :title => 'A Very Simple Boids v0.1' do | ||
extend Rules | ||
@boids = [] | ||
N.times do | ||
x, y = rand(200), rand(200) | ||
vx, vy = rand(2), rand(2) | ||
@boids << image('../images/gallery10-kamome.png', | ||
:left => x, :top => y, :pos => Vector[x, y], :vel => Vector[vx, vy]) | ||
end | ||
|
||
animate 12 do | ||
@boids.each do |boid| | ||
boid.vel = boid.vel + rule1(boid) + rule2(boid) + rule3(boid) + rule4(boid) | ||
boid.pos = boid.pos + boid.vel | ||
boid.move boid.x, boid.y | ||
end | ||
end | ||
end |
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