The Nature of Code
Dedication
Acknowledgments
A.1 A little bit of history
A.2 Kickstarter
Preface
P.1 What is this book?
P.2 A word about Processing
P.3 What do you need to know?
P.4 What are you using to read this book?
P.5 The “story” of this book
Part I: Inanimate objects
Part II: It’s alive!
Part III: Intelligence
P.6 This book as a syllabus
P.7 The Ecosystem Project
P.8 Where do I find the code online and submit feedback?
Table of Contents
Introduction
I.1 Random Walks
I.2 The Random Walker Class
Pseudo-Random Numbers
Exercise I.1
I.3 Probability and Non-Uniform Distributions
Exercise I.2
Exercise I.3
I.4 A Normal Distribution of Random Numbers
Calculating Mean and Standard Deviation
Exercise I.4
Exercise I.5
I.5 A Custom Distribution of Random Numbers
Exercise I.6
I.6 Perlin Noise (A Smoother Approach)
Noise Detail
Mapping Noise
Exercise I.7
Two-Dimensional Noise
Exercise I.8
Exercise I.9
Exercise I.10
I.7 Onward
Chapter 1. Vectors
1.1 Vectors, You Complete Me
1.2 Vectors for Processing Programmers
1.3 Vector Addition
Exercise 1.1
Exercise 1.2
Exercise 1.3
1.4 More Vector Math
Vector subtraction
Basic Number Properties with Vectors
Vector multiplication
More Number Properties with Vectors
1.5 Vector Magnitude
1.6 Normalizing Vectors
1.7 Vector Motion: Velocity
1.8 Vector Motion: Acceleration
Acceleration Algorithms!
Exercise 1.4
Exercise 1.5
Exercise 1.6
1.9 Static vs. Non-Static Functions
Exercise 1.7
1.10 Interactivity with Acceleration
Exercise 1.8
The Ecosystem Project
Chapter 2. Forces
2.1 Forces and Newton’s Laws of Motion
Newton’s First Law
Newton’s Third Law
Newton’s Third Law (as seen through the eyes of Processing)
2.2 Forces and Processing—Newton’s Second Law as a Function
Newton’s Second Law
Weight vs. Mass
2.3 Force Accumulation
Exercise 2.1
2.4 Dealing with Mass
Units of Measurement
Exercise 2.2
2.5 Creating Forces
Exercise 2.3
2.6 Gravity on Earth and Modeling a Force
Dealing with formulae
2.7 Friction
Exercise 2.4
2.8 Air and Fluid Resistance
Exercise 2.5
Exercise 2.6
Exercise 2.7
2.9 Gravitational Attraction
Exercise 2.8
Exercise 2.9
2.10 Everything Attracts (or Repels) Everything
Exercise 2.10
The Ecosystem Project
Chapter 3. Oscillation
3.1 Angles
What is PI?
Exercise 3.1
3.2 Angular Motion
Exercise 3.2
3.3 Trigonometry
3.4 Pointing in the Direction of Movement
Exercise 3.3
3.5 Polar vs. Cartesian Coordinates
Exercise 3.4
Exercise 3.5
3.6 Oscillation Amplitude and Period
Exercise 3.6
3.7 Oscillation with Angular Velocity
Exercise 3.7
Exercise 3.8
3.8 Waves
Exercise 3.9
Exercise 3.10
Exercise 3.11
3.9 Trigonometry and Forces: The Pendulum
Exercise 3.12
Exercise 3.13
Exercise 3.14
3.10 Spring Forces
Exercise 3.15
Exercise 3.16
The Ecosystem Project
Chapter 4. Particle Systems
4.1 Why We Need Particle Systems
4.2 A Single Particle
Exercise 4.1
Exercise 4.2
4.3 The ArrayList
4.4 The Particle System Class
Exercise 4.3
Exercise 4.4
4.5 A System of Systems
Exercise 4.5
Exercise 4.6
4.6 Inheritance and Polymorphism: An Introduction
4.7 Inheritance Basics
4.8 Particles with Inheritance
Exercise 4.7
4.9 Polymorphism Basics
4.10 Particle Systems with Polymorphism
Exercise 4.8
4.11 Particle Systems with Forces
4.12 Particle Systems with Repellers
Exercise 4.9
Exercise 4.10
4.13 Image Textures and Additive Blending
Exercise 4.11
Exercise 4.12
Exercise 4.13
Exercise 4.14
The Ecosystem Project
Chapter 5. Physics Libraries
5.1 What Is Box2D and When Is It Useful?
5.2 Getting Box2D in Processing
5.3 Box2D Basics
SETUP
DRAW
Core elements of a Box2D world:
5.4 Living in a Box2D World
5.5 Building a Box2D Body
Step 1: Define a body.
Step 2: Configure the body definition.
Step 3: Create the body.
Step 4: Set any other conditions for the body’s starting state.
5.6 Three’s Company: Bodies and Shapes and Fixtures
Step 1: Define a shape.
Step 2: Create a fixture.
Step 3: Attach the shape to the body with the fixture.
Exercise 5.1
5.7 Box2D and Processing: Reunited and It Feels So Good
Step 1: Add Box2D to our main program (i.e. setup() and draw()).
Step 2: Link every Processing Box object with a Box2D Body object.
Exercise 5.2
5.8 Fixed Box2D Objects
5.9 A Curvy Boundary
Step 1: Define a body.
Step 2: Define the Shape.
Step 3: Configure the Shape.
Step 4: Attach the Shape to the body with a Fixture.
Exercise 5.3
5.10 Complex Forms
Exercise 5.4
Exercise 5.5
5.11 Feeling Attached—Box2D Joints
Exercise 5.6
Step 1: Make sure you have two bodies ready to go.
Step 2: Define the joint.
Step 3: Configure the joint’s properties.
Step 4: Create the joint.
Exercise 5.7
Exercise 5.8
Exercise 5.9
5.12 Bringing It All Back Home to Forces
Exercise 5.10
5.13 Collision Events
Step 1: Contact, could you tell me what two things collided?
Step 2: Fixtures, could you tell me which body you are attached to?
Step 3: Bodies, could you tell me which Particles you are associated with?
Exercise 5.11
Exercise 5.12
5.14 A Brief Interlude—Integration Methods
5.15 Verlet Physics with toxiclibs
Getting toxiclibs
Core Elements of VerletPhysics
Vectors with toxiclibs
Building the toxiclibs physics world
5.16 Particles and Springs in toxiclibs
5.17 Putting It All Together: A Simple Interactive Spring
5.18 Connected Systems, Part I: String
Exercise 5.13
5.19 Connected Systems, Part II: Force-Directed Graph
Exercise 5.14
Exercise 5.15
5.20 Attraction and Repulsion Behaviors
Exercise 5.16
Exercise 5.17
The Ecosystem Project
Chapter 6. Autonomous Agents
6.1 Forces from Within
6.2 Vehicles and Steering
Why Vehicle?
6.3 The Steering Force
Exercise 6.1
Exercise 6.2
Exercise 6.3
6.4 Arriving Behavior
6.5 Your Own Desires: Desired Velocity
Exercise 6.4
Exercise 6.5
6.6 Flow Fields
Exercise 6.6
Exercise 6.7
Exercise 6.8
6.7 The Dot Product
Exercise 6.9
6.8 Path Following
6.9 Path Following with Multiple Segments
Exercise 6.10
Exercise 6.11
6.10 Complex Systems
6.11 Group Behaviors (or: Let’s not run into each other)
Exercise 6.12
Exercise 6.13
6.12 Combinations
Exercise 6.14
6.13 Flocking
Exercise 6.15
Exercise 6.16
Exercise 6.17
Exercise 6.18
Exercise 6.19
6.14 Algorithmic Efficiency (or: Why does my $@(*%! run so slowly?)
6.15 A Few Last Notes: Optimization Tricks
1) Magnitude squared (or sometimes distance squared)
2) Sine and cosine lookup tables
3) Making gajillions of unnecessary PVector objects
Exercise 6.20
Exercise 6.21
The Ecosystem Project
Chapter 7. Cellular Automata
7.1 What Is a Cellular Automaton?
7.2 Elementary Cellular Automata
7.3 How to Program an Elementary CA
7.4 Drawing an Elementary CA
Exercise 7.1
Exercise 7.2
Exercise 7.3
Exercise 7.4
7.5 Wolfram Classification
Exercise 7.5
7.6 The Game of Life
7.7 Programming the Game of Life
Exercise 7.6
Exercise 7.7
Exercise 7.8
7.8 Object-Oriented Cells
7.9 Variations of Traditional CA
Exercise 7.9
Exercise 7.10
Exercise 7.11
Exercise 7.12
Exercise 7.13
Exercise 7.14
Exercise 7.15
The Ecosystem Project
Chapter 8. Fractals
8.1 What Is a Fractal?
8.2 Recursion
8.3 The Cantor Set with a Recursive Function
Exercise 8.1
8.4 The Koch Curve and the ArrayList Technique
The “Monster” Curve
Exercise 8.2
Exercise 8.3
Exercise 8.4
Exercise 8.5
8.5 Trees
Exercise 8.6
Exercise 8.7
Exercise 8.8
Exercise 8.9
Exercise 8.10
Exercise 8.11
8.6 L-systems
Exercise 8.12
Exercise 8.13
Exercise 8.14
The Ecosystem Project
Chapter 9. The Evolution of Code
9.1 Genetic Algorithms: Inspired by Actual Events
9.2 Why Use Genetic Algorithms?
Exercise 9.1
9.3 Darwinian Natural Selection
9.4 The Genetic Algorithm, Part I: Creating a Population
9.5 The Genetic Algorithm, Part II: Selection
9.6 The Genetic Algorithm, Part III: Reproduction
9.7 Code for Creating the Population
Step 1: Initialize Population
Step 2: Selection
Exercise 9.2
Exercise 9.3
Step 3: Reproduction
Exercise 9.4
Exercise 9.5
9.8 Genetic Algorithms: Putting It All Together
Exercise 9.6
9.9 Genetic Algorithms: Make Them Your Own
Key #1: Varying the variables
Key #2: The fitness function
Exercise 9.7
Key #3: Genotype and Phenotype
9.10 Evolving Forces: Smart Rockets
9.11 Smart Rockets: Putting It All Together
Exercise 9.8
Exercise 9.9
Exercise 9.10
Exercise 9.11
Exercise 9.12
9.12 Interactive Selection
Exercise 9.14
9.13 Ecosystem Simulation
Genotype and Phenotype
Selection and Reproduction
The Ecosystem Project
Chapter 10. Neural Networks
10.1 Artificial Neural Networks: Introduction and Application
10.2 The Perceptron
10.3 Simple Pattern Recognition Using a Perceptron
10.4 Coding the Perceptron
Exercise 10.1
Exercise 10.2
10.5 A Steering Perceptron
Exercise 10.3
Exercise 10.4
10.6 It’s a “Network,” Remember?
10.7 Neural Network Diagrams
10.8 Animating Feed Forward
Exercise 10.5
Exercise 10.6
Exercise 10.7
The Ecosystem Project
The end
Further Reading
Books
Papers and Articles
Index