|
| 1 | +# |
| 2 | +# <p>Force directed graph, |
| 3 | +# heavily based on: <a href="http:#code.google.com/p/fidgen/">fid.gen</a><br/> |
| 4 | +# <a href="http:#www.shiffman.net/teaching/nature/toxiclibs/">The Nature of Code</a><br/> |
| 5 | +# Spring 2010</p> |
| 6 | +# |
| 7 | +# Copyright (c) 2010 Daniel Shiffman |
| 8 | +# |
| 9 | +# This demo & library is free software you can redistribute it and/or |
| 10 | +# modify it under the terms of the GNU Lesser General Public |
| 11 | +# License as published by the Free Software Foundation either |
| 12 | +# version 2.1 of the License, or (at your option) any later version. |
| 13 | +# |
| 14 | +# http:#creativecommons.org/licenses/LGPL/2.1/ |
| 15 | +# |
| 16 | +# This library is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | +# Lesser General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU Lesser General Public |
| 22 | +# License along with this library if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | +# |
| 25 | +require 'toxiclibs' |
| 26 | +require_relative 'cluster' |
| 27 | +require_relative 'node' |
| 28 | + |
| 29 | +attr_reader :physics, :clusters, :show_physics, :show_particles, :f |
| 30 | + |
| 31 | +def setup |
| 32 | + size(640, 360) |
| 33 | + @f = create_font('Georgia', 12, true) |
| 34 | + @show_physics = true |
| 35 | + @show_particles = true |
| 36 | + # Initialize the physics |
| 37 | + @physics = Physics::VerletPhysics2D.new |
| 38 | + physics.setWorldBounds(Toxi::Rect.new(10, 10, width - 20, height - 20)) |
| 39 | + # Spawn a new random graph |
| 40 | + new_graph |
| 41 | +end |
| 42 | + |
| 43 | +# Spawn a new random graph |
| 44 | +def new_graph |
| 45 | + # Clear physics |
| 46 | + physics.clear |
| 47 | + center = TVec2D.new(width / 2, height / 2) |
| 48 | + @clusters = (0..8).map { Cluster.new(physics, rand(3..8), rand(20..100), center) } |
| 49 | + # All clusters connect to all clusters |
| 50 | + clusters.each_with_index do |ci, i| |
| 51 | + clusters[i + 1..clusters.size - 1].each do |cj| |
| 52 | + ci.connect(cj) |
| 53 | + end |
| 54 | + end |
| 55 | +end |
| 56 | + |
| 57 | +def draw |
| 58 | + # Update the physics world |
| 59 | + physics.update |
| 60 | + background(255) |
| 61 | + # Display all points |
| 62 | + clusters.each(&:display) if show_particles |
| 63 | + # If we want to see the physics |
| 64 | + if show_physics |
| 65 | + clusters.each_with_index do |ci, i| |
| 66 | + ci.internal_connections |
| 67 | + # Cluster connections to other clusters |
| 68 | + clusters[1 + i..clusters.size - 1].each do |cj| |
| 69 | + ci.show_connections(cj) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + # Instructions |
| 74 | + fill(0) |
| 75 | + text_font(f) |
| 76 | + text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph", 10, 20) |
| 77 | +end |
| 78 | + |
| 79 | +# Key press commands |
| 80 | +def key_pressed |
| 81 | + case key |
| 82 | + when 'c' |
| 83 | + @show_physics = !show_physics |
| 84 | + @show_particles = true unless show_physics |
| 85 | + when 'p' |
| 86 | + @show_particles = !show_particles |
| 87 | + @show_physics = true unless show_particles |
| 88 | + when 'n' |
| 89 | + new_graph |
| 90 | + end |
| 91 | +end |
0 commit comments