Skip to content

Commit 6af4ddb

Browse files
author
monkstone
committed
organise toxiclibs examples
1 parent e1ab130 commit 6af4ddb

File tree

9 files changed

+126
-0
lines changed

9 files changed

+126
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# The Nature of Code
2+
# <http://www.shiffman.net/teaching/nature>
3+
# Spring 2010
4+
# Toxiclibs example: http://toxiclibs.org/
5+
6+
# Force directed graph
7+
# Heavily based on: http://code.google.com/p/fidgen/
8+
# A cluster is a grouping of Nodes
9+
class Cluster
10+
include Processing::Proxy
11+
attr_reader :nodes, :diameter, :physics
12+
13+
# We initialize a Cluster with a number of nodes, a diameter, and centerpoint
14+
def initialize(p, n, d, center)
15+
# Set the diameter
16+
@diameter, @physics = d, p
17+
# Create the nodes
18+
@nodes = (0..n).map { Node.new(center.add(TVec2D.randomVector)) }
19+
# Connect all the nodes with a Spring
20+
nodes[0..nodes.size - 2].each_with_index do |ni, i|
21+
nodes[i + 1..nodes.size - 1].each do |nj|
22+
# A Spring needs two particles, a resting length, and a strength
23+
physics.addSpring(Physics::VerletSpring2D.new(ni, nj, diameter, 0.01))
24+
end
25+
end
26+
end
27+
28+
def display
29+
# Show all the nodes
30+
nodes.each(&:display)
31+
end
32+
33+
# Draw all the internal connections
34+
def show_connections
35+
stroke(0, 150)
36+
stroke_weight(2)
37+
nodes[0..nodes.size - 2].each_with_index do |pi, i|
38+
nodes[i + 1..nodes.size - 1].each do |pj|
39+
line(pi.x, pi.y, pj.x, pj.y)
40+
end
41+
end
42+
end
43+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The Nature of Code
2+
# <http://www.shiffman.net/teaching/nature>
3+
# Spring 2010
4+
# Toxiclibs example: http://toxiclibs.org/
5+
6+
# Force directed graph
7+
# Heavily based on: http://code.google.com/p/fidgen/
8+
9+
# Notice how we are using inheritance here!
10+
# We could have just stored a reference to a VerletParticle object
11+
# inside the Node class, but inheritance is a nice alternative
12+
class Node < Physics::VerletParticle2D
13+
include Processing::Proxy
14+
15+
def initialize(pos)
16+
super(pos)
17+
end
18+
19+
# All we're doing really is adding a :display function to a VerletParticle
20+
def display
21+
fill(0, 150)
22+
stroke(0)
23+
stroke_weight(2)
24+
ellipse(x, y, 16, 16)
25+
end
26+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# The Nature of Code
2+
# Daniel Shiffman
3+
# http://natureofcode.com
4+
5+
# Force directed graph,
6+
# heavily based on: http://code.google.com/p/fidgen/
7+
8+
require 'toxiclibs'
9+
require_relative 'cluster'
10+
require_relative 'node'
11+
12+
attr_reader :physics, :cluster, :f, :show_physics, :show_particles
13+
14+
def setup
15+
size(640, 360)
16+
@f = createFont('Georgia', 12, true)
17+
18+
@show_physics = true
19+
@show_particles = true
20+
@show_physics = true
21+
@show_particles = true
22+
# Initialize the physics
23+
@physics = Physics::VerletPhysics2D.new
24+
@physics.setWorldBounds(Toxi::Rect.new(10, 10, width - 20, height - 20))
25+
26+
# Spawn a new random graph
27+
@cluster = Cluster.new(physics, 8, 100, TVec2D.new(width / 2, height / 2))
28+
end
29+
30+
def draw
31+
# Update the physics world
32+
physics.update
33+
background(255)
34+
# Display all points
35+
cluster.display if show_particles
36+
# If we want to see the physics
37+
cluster.show_connections if show_physics
38+
# Instructions
39+
fill(0)
40+
text_font(f)
41+
text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph", 10, 20)
42+
end
43+
44+
# Key press commands
45+
def key_pressed
46+
case key
47+
when 'c'
48+
@show_physics = !show_physics
49+
@show_particles = true if show_physics
50+
when 'p'
51+
@show_particles = !show_particles
52+
@show_particles = true unless show_physics
53+
when 'n'
54+
physics.clear
55+
@cluster = Cluster.new(physics, rand(3..20), rand(10..width / 2), TVec2D.new(width / 2, height / 2))
56+
end
57+
end

0 commit comments

Comments
 (0)