Skip to content

Commit

Permalink
added gallery 10: A Very Simple Boids
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbb committed May 25, 2009
1 parent 7213fe5 commit 309f35b
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Shoes Tutorial Note
===================
**- For the Shoes App Rookie Creators -**

May 24th, 2009 by ashbb (Satoshi Asakawa), citizen428 (Michael Kohl), kotp (Victor H. Goff III)
May 25th, 2009 by ashbb (Satoshi Asakawa), citizen428 (Michael Kohl), kotp (Victor H. Goff III)

Table of contents
-----------------
Expand Down Expand Up @@ -84,7 +84,7 @@ Table of contents
10. [01000 Acknowledgment](http://github.com/ashbb/shoes_tutorial_html/tree/master/mdowns/01000_Acknowledgment.mdown)
11. Fancy Gallery
- [01110 Fancy Gallery 1-5 (gallery1.rb, gallery1-1.rb, gallery2.rb, gallery3.rb, gallery4.rb, gallery4-1.rb, gallery5.rb)](http://github.com/ashbb/shoes_tutorial_html/tree/master/mdowns/01110_Fancy_Gallery_1-5.mdown)
- [01120 Fancy Gallery 6-10 (gallery6.rb, gallery7.rb, gallery8.rb, gallery9.rb)](http://github.com/ashbb/shoes_tutorial_html/tree/master/mdowns/01120_Fancy_Gallery_6-10.mdown)
- [01120 Fancy Gallery 6-10 (gallery6.rb, gallery7.rb, gallery8.rb, gallery9.rb, gallery10.rb, gallery10-rules.rb, gallery10-image.rb)](http://github.com/ashbb/shoes_tutorial_html/tree/master/mdowns/01120_Fancy_Gallery_6-10.mdown)
12. Built-in Samples

- [01201 simple-accordion (simple-accordion.rb)](http://github.com/ashbb/shoes_tutorial_html/tree/master/mdowns/01201_simple-accordion.mdown)
Expand Down
1 change: 1 addition & 0 deletions changelog.mdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Change log:
-----------
May 25th, 2009: Added gallery 10.
May 24th, 2009: Added a new chapter 00611
May 17th, 2009: Added a new chapter 00610
May 09th, 2009: Added a new chapter 00609
Expand Down
Binary file added images/gallery10-kamome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gallery10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions mdowns/01120_Fancy_Gallery_6-10.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,108 @@ Look at the [online demo](http://www.rin-shun.com/rubylearning/shoes/scatter_pac
![gallery9.png](http://github.com/ashbb/shoes_tutorial_html/raw/master/images/gallery9.png)




Gallery No.10
-------------
A Very Simple Boids Program.

Paul Harris introduced the following fancy Shoes app in the Shoes course 4th batch.

[Hungry Boids](http://the-shoebox.org/apps/45)

I read the code and found the link to this web site:
[Boids Pseudocode](http://www.vergenet.net/~conrad/boids/pseudocode.html)

So simple! Most impressive indeed!

I attempted coding a tiny Shoes app just by following the algorithm.

Look at [online demo](http://www.rin-shun.com/rubylearning/shoes/a_very_simple_boids.swf.html).

# 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


4 rules:


# 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


Helper methods for writing the simple code:


# 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

**gallery10.png**

![gallery10.png](http://github.com/ashbb/shoes_tutorial_html/raw/master/images/gallery10.png)



9 changes: 9 additions & 0 deletions src/gallery10-image.rb
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
36 changes: 36 additions & 0 deletions src/gallery10-rules.rb
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
24 changes: 24 additions & 0 deletions src/gallery10.rb
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
2 changes: 1 addition & 1 deletion tools/table_of_contents.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Shoes Tutorial Note
===================
**- For the Shoes App Rookie Creators -**

May 24th, 2009 by ashbb (Satoshi Asakawa), citizen428 (Michael Kohl), kotp (Victor H. Goff III)
May 25th, 2009 by ashbb (Satoshi Asakawa), citizen428 (Michael Kohl), kotp (Victor H. Goff III)

Table of contents
-----------------
Expand Down

0 comments on commit 309f35b

Please sign in to comment.