Skip to product information
Nature of Code (JavaScript Edition)
Nature of Code (JavaScript Edition)
Description
Book Introduction
Unravel the mysteries of nature while learning to code with JavaScript.

A revised JavaScript edition of 『Nature of Code』, which has inspired countless readers for over a decade.
Written by Daniel Shiffman of the "Coding Train" channel, this groundbreaking guide introduces both beginners and experienced programmers to the world where code meets playful creativity.
It gently unravels the mysteries of nature, such as vectors, Newton's laws of motion, and vibration, and even explores complex topics like evolution, genetic algorithms, neural networks, and machine learning.
You can experience the amazing pleasure of implementing your own physics engine and turning code into art.
  • You can preview some of the book's contents.
    Preview

index
About the Author, Reviewer, and Translator xii
Translator's Preface xiii
Beta Reader Review xiv
Recommendation xvi
Acknowledgments xviii
About this book xxiv

CHAPTER 0 Randomness 1
0.1 Random Walk 2
0.2 Random Walker Class 3
0.3 Probability and Nonuniform Distribution 10
0.4 Normal distribution of random numbers 14
0.5 Custom distribution of random numbers 18
A softer approach using 0.6 Perlin noise 21

CHAPTER 1 Vectors 33
1.1 The Importance of Vectors 34
1.2 Vectors in p5.js 37
1.3 Vector Addition 40
1.4 Additional Vector Mathematics 45
1.5 Length of vector 50
1.6 Vector Normalization 52
1.7 Movement Using Vectors 54
1.8 Acceleration 59

CHAPTER 2: FORCE 71
2.1 Forces and Newton's Laws of Motion 72
2.2 Accumulation of Force 78
2.3 Considering Mass 80
2.4 Building Power 82
2.5 Modeling Forces 88
2.6 n-body problem 110

CHAPTER 3: VIBRATION 117
3.1 Angle 118
3.2 Each movement 120
3.3 Trigonometric Functions 126
3.4 Direction of Movement 127
3.5 Polar and Cartesian Coordinates 131
3.6 Vibration Characteristics 134
3.7 Vibration using angular velocity 138
3.8 Wave 142
3.9 Spring force 147
3.10 Pendulum 154

CHAPTER 4 Particle Systems 165
4.1 The Importance of Particle Systems 166
4.2 Single Particle 168
4.3 Particle Array 172
4.4 Particle Emitter 178
4.5 Emitter System 181
4.6 Inheritance and Polymorphism 184
4.7 Particle Systems Using Forces 197
4.8 Particle system applying the force of the pushing object 200
4.9 Image Textures and Additive Blending 205

CHAPTER 5 Autonomous Agents 213
5.1 The Power of Choosing by One's Own Will 214
5.2 Vehicles and Steering 215
5.3 Flow field 232
5.4 Following the Path 239
5.5 Complex Systems 256
5.6 Algorithmic Efficiency: Why Is My Program So Slow? 273

CHAPTER 6 Physics Library 285
6.1 Why Use a Physics Library? 287
6.2 Importing the Matter.js Library 289
6.3 Matter.js Overview 291
6.4 Matter.js with p5.js 301
6.5 Static Matter.js Body 306
6.6 Polygons and Shape Groups 307
6.7 Matter.js Constraints 315
6.8 Applying Various Forces 323
6.9 Crash Event 327
6.10 A Brief Introduction: Integral Calculus 331
6.11 Berle Physics 333 with Toxiclibs.js
6.12 Delinquency Simulation 342
6.13 Gravitational and Repulsive Forces 355

CHAPTER 7 Cellular Automata 359
7.1 What are cellular automata? 360
7.2 Basic Cellular Automata 362
7.3 Wolfram's Classification System 380
7.4 Game of Life 382
7.5 Creating Object-Oriented Cells 391
7.6 Variations on Traditional Cellular Automata 394

CHAPTER 8 Fractals 399
8.1 What is a Fractal? 400
8.2 Recursion 403
8.3 Coke Curve 414
8.4 Tree 422
8.5 L-System 430

CHAPTER 9 Evolutionary Computing 439
9.1 Genetic Algorithms: The Wisdom of Nature 440
9.2 Why Use Genetic Algorithms? 441
9.3 How Genetic Algorithms Work 444
9.4 Coding Genetic Algorithms 452
9.5 Customizing the Genetic Algorithm 464
9.6 Evolving Power: Smart Rocket 471
9.7 Interactive Selection 484
9.8 Ecosystem Simulation 489

CHAPTER 10: NEURAL NETWORKS 499
10.1 Introduction to Artificial Neural Networks 501
10.2 Perceptron 505
10.3 Adding a 'Network' to a Neural Network 521
10.4 Machine Learning with ml5.js 524
10.5 Building a Gesture Classifier 532

CHAPTER 11: NEUROEVOLUTION 545
11.1 Reinforcement Learning 547
11.2 Evolution of Neural Networks: The NEAT Algorithm 552
11.3 Coding Flappy Bird 553
11.4 Neuro-Evolving Flappy Bird 557
11.5 Steering with Neuroevolution 568
11.6 Neuroevolutionary Ecosystems 576

Conclusion 587
Appendix: Lifeform Design 589
Source of the illustration 593
Search 596

Detailed image
Detailed Image 1

Into the book
It's Saturday morning.
After a refreshing jog, a delicious bowl of cereal, and a quiet moment at the computer with a cup of warm chamomile tea, I went for a walk.
Looking at the calendar, I saw that it was soon to be an old friend's birthday, so I decided to make a congratulatory card with p5.js.
How about a greeting card with flying confetti? I decided to create a greeting card where confetti of various shapes and movements explodes on the screen at once: purple, pink, star-shaped, square, fast-flying, slow-falling.
/ This is a particle system.
This is a particle system that handles particles called confetti.
We can redesign the Particle class to include variables that store color, shape, movement, etc.

--- p.185

All Flappy Bird games made so far have been controlled by clicking the mouse.
Now, let's make computers play games themselves and teach them how to do so through neuroevolution.
ml5.js has built-in neural evolution functionality, making it relatively easy to implement.
The first step is to implant a 'brain' in the bird so that it can decide for itself whether to flap its wings or not.

--- p.557

A good random number generator produces numbers that are unrelated and show no apparent pattern.
However, when programming natural, lifelike movements, a little randomness is enough.
Rather, completely uniform randomness can be unnatural.
The Perlin noise algorithm, created by Ken Perlin, was created with these aspects in mind.
This algorithm ensures that consecutive numbers have similar values.
This smooths out the transitions between numbers, giving them a more natural feel than if they were completely random.
That's why Perlin noise is often used to create natural patterns such as clouds, landscapes, and marble patterns.

--- p.21

The following diagram provides ideas that may be helpful when creating an ecosystem based on the contents of this book.
Watch how this picture evolves as you learn new concepts and techniques.
/ Since the main purpose of this book is to demonstrate algorithms and behavioral principles, most of the examples consist of simple shapes such as circles.
But you may have more creative ideas than this.
So, challenge yourself to design with the elements you draw.
If you're new to translating your designs into code, check out this guide by Jana Marsh, the book's illustrator.
This guide in the appendix teaches you how to draw with code.

--- p.32

Let's take another look at the code for the attract() method.
Do you see the division sign? Every time you perform a division operation, you should ask yourself, "What happens if the distance is very small, or (even worse!) zero?"
Dividing by zero is impossible, and dividing by a very small number like 0.0001 is essentially the same as multiplying by 10,000! This might not be a problem in the real-world gravity formula, but p5.js isn't the real world.
In p5.js, the mover can get too close to the attractor, and the force can become so strong that the mover flies off the screen.
/ Conversely, let's assume the mover is 500 pixels away from the attractor.
Since we square the distance, this is equivalent to dividing the force by 250,000.
Then the force may be so weak that it may seem almost ineffective.

--- p.106

Not as interesting as the Sierpinski triangle.
As mentioned earlier, most of the 256 basic rule sets don't produce very nice patterns.
But it's truly fascinating that even simple cells with just two states can create the amazing patterns we see in nature.
For example, the snail shell in Figure 7.18 has a shape similar to the pattern created by Wolfram's Rule 30.
This shows how useful cellular automata are for mimicking natural patterns and creating new ones.
/ So before we dive into the details of what different sets of rules produce, let's learn how to create and display a basic Wolfram cellular automata using p5.js.
--- p.369

Publisher's Review
A special adventure with Daniel Shipman, the world's funniest coding YouTuber.

What if you could recreate the awe-inspiring flocks of birds or the hypnotic dance of fireflies with code? "The Nature of Code," written by Daniel Schiffman of the popular YouTube channel "Coding Train," has transformed countless readers into creators, breaking down the barriers between science, art, and technology and encouraging them to view code not simply as a tool, but as a canvas for limitless creativity.
The recently published 『Nature of Code (JavaScript Edition)』 is the latest revised edition that changes the example code language from Processing to JavaScript (p5.js) and adds additional content.


Starting with the concept of vectors, we'll explore Newton's laws of motion, oscillations, and trigonometry to create our own simple physics engine in JavaScript.
Based on this, we simulate complex systems, steering, and flocking, and further explain complex topics such as evolution and genetic algorithms, neural networks, machine learning, and neuroevolutionary systems in an easy-to-understand manner.
The process of transforming common natural phenomena into visually stunning works of art using object-oriented programming is truly thrilling.
All of the book's example code is available for testing directly on the book's official website.


Everyone, get on board the 'Coding Train' and embark on a special adventure of creative coding.
You'll learn the fundamentals of coding while discovering the joy of turning code into art.
You will see nature in a new way and be inspired by its wonders to create your own.
Whether you're a beginner or an experienced programmer, this book will guide you into the amazing world where code and creativity intersect.


Key Contents
● Physics Engine: Simulates the push and pull of gravity.
● Flocking: Create a captivating flock dance.
● Pruning Trees: Create vivid, organic branch structures.
● Neural Networks: Build intelligent systems that learn and adapt.
● Cellular Automata: Discover the magic of self-organizing patterns.
● Evolutionary Algorithms: Experience natural selection directly in code.
GOODS SPECIFICS
- Date of issue: July 22, 2025
- Page count, weight, size: 640 pages | 1,230g | 188*245*30mm
- ISBN13: 9791194587316

You may also like

카테고리