Learn Python Visually Creative Coding With Processing Py 1st Edition Tristan Bunn Download PDF
Learn Python Visually Creative Coding With Processing Py 1st Edition Tristan Bunn Download PDF
com
https://textbookfull.com/product/learn-python-
visually-creative-coding-with-processing-py-1st-
edition-tristan-bunn/
https://textbookfull.com/product/make-getting-started-with-processing-
py-allison-parrish/
textbookfull.com
https://textbookfull.com/product/coding-art-the-four-steps-to-
creative-programming-with-the-processing-language-yu-zhang/
textbookfull.com
https://textbookfull.com/product/coding-art-the-four-steps-to-
creative-programming-with-the-processing-language-1st-edition-yu-
zhang-mathias-funk/
textbookfull.com
https://textbookfull.com/product/transactions-on-computational-
collective-intelligence-xxxv-ngoc-thanh-nguyen/
textbookfull.com
Fight or Flight A Brother s Best Friend Novel 1st Edition
Cameron Hart [Hart
https://textbookfull.com/product/fight-or-flight-a-brother-s-best-
friend-novel-1st-edition-cameron-hart-hart/
textbookfull.com
https://textbookfull.com/product/big-bucks-the-explosion-of-the-art-
market-in-the-21st-century-adam/
textbookfull.com
https://textbookfull.com/product/tease-me-dragons-love-
curves-02-0-sassy-ever-after-universe-49-0-1st-edition-aidy-award/
textbookfull.com
https://textbookfull.com/product/environmental-assessment-on-energy-
and-sustainability-by-data-envelopment-analysis-1st-edition-toshiyuki-
sueyoshi/
textbookfull.com
Graph Structures for Knowledge Representation and
Reasoning Madalina Croitoru
https://textbookfull.com/product/graph-structures-for-knowledge-
representation-and-reasoning-madalina-croitoru/
textbookfull.com
FULL
COLOR
LE ARN PY THON
V I S U A L LY
C R E A T I V E C O D I N G W I T H
P R O C E S S I N G . P Y
TRISTAN BUNN
LEARN PYTHON VISUALLY
LEARN PYTHON
V I S U A L LY
C R E AT I V E CO D I N G W I T H P RO C E S S I N G .P Y
b y Tr is t a n B u nn
San Francisco
LEARN PYTHON VISUALLY. Copyright © 2021 by Tristan Bunn.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
25 24 23 22 21 123456789
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900; [email protected]
www.nostarch.com
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in it.
About the Author
Tristan Bunn kicked off his web design career back in the days of
PlayStation 1, grunge music, and dial-up modems. Since then, he’s worked
on a diverse range of digital projects for varied clients. He’s currently
involved in lecturing, research, and work that blends code, interaction,
interface design, and creativity. Tristan has years of experience teaching
coding for art, games, web, and other creative technologies.
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
CONTE NT S IN DE TA IL
ACKNOWLEDGMENTS XIII
INTRODUCTION XV
Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
What Is Python Mode for Processing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
What Are Algorithms? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
What Is Creative Coding? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Where Can I Find Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Source Code and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Let’s Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
1
HELLO, WORLD! 1
Processing Installation and Python Mode Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Your First Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Fills and Strokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Color Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2D Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
triangle() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
ellipse() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
quad() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
line() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Challenge #1: Rainbow Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Challenge #2: Disk Usage Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2
DRAWING MORE COMPLICATED SHAPES 29
Displaying a Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Drawing Curves Using Catmull-Rom Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Curving Lines with curve() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Changing Curves with curveTightness() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Drawing Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Using the bezier() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Positioning Anchor and Control Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Drawing Shapes Using Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Bézier Vertices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using Vector Graphics Software for Generating Shapes . . . . . . . . . . . . . . . . . . . . . . . . 50
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3
INTRODUCTION TO STRINGS AND WORKING WITH TEXT 53
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Creating Strings in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Using Concatenation and String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Working with String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Slice Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Typography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Text Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4
CONDITIONAL STATEMENTS 69
Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Challenge #3: Four-Square Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5
ITERATION AND RANDOMNESS 85
Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using Iteration to Draw Concentric Circles . . . . . . . . . . . . . . . . . . . . . . . . . . 86
while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Challenge #4: Create Line Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Randomness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
random() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Random Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Truchet Tiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
x Contents in Detail
6
MOTION AND TRANSFORMATION 105
Perceiving Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Adding Motion to Processing Sketches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
The draw() and setup() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Saving Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Challenge #5: DVD Screensaver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Processing Transformation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
rotate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
scale() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
shearX() and shareY() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
pushMatrix() and popMatrix() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Challenge #6: Analog Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
7
WORKING WITH LISTS AND READING DATA 133
Introducing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Creating and Accessing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Modifying Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Combining Loops and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Drawing Shapes by Using a List of Color Values . . . . . . . . . . . . . . . . . . . . . 140
Looping with enumerate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Creating Lists of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Challenge #7: Breakout Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Reading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Challenge #8: Games Sales Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
8
DICTIONARIES AND JSON 159
Introducing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Accessing Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Modifying Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Nesting Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Combining Loops and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Iterating Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Iterating Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Iterating Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Working with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Understanding JSON Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Using Web APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Reading in JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Challenge #9: Coffee Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Contents in Detail xi
9
FUNCTIONS AND PERIODIC MOTION 175
Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating a Simple Speech Bubble Function . . . . . . . . . . . . . . . . . . . . . . . . . 176
Drawing Compound Shapes Using a Function . . . . . . . . . . . . . . . . . . . . . . . 179
Adding Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Using Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Setting Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Mixing Positional and Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 186
Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Defining Functions for Periodic Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
An Introduction to Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Circular and Elliptical Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Lissajous Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Creating Screensaver-Like Patterns with Lissajous Curves . . . . . . . . . . . . . . . . 203
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
10
OBJECT-ORIENTED PROGRAMMING AND PVECTOR 207
Working with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Defining a New Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Creating an Instance from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Adding Attributes to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Adding Methods to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Splitting Your Python Code into Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Programming Movement with Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The PVector Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Moving an Amoeba with PVector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Adding Many Amoebas to the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Challenge #10: Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
11
MOUSE AND KEYBOARD INTERACTION 239
Mouse Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Mouse Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Creating a Paint App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Keyboard Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Adding Keyboard Shortcuts to the Paint App . . . . . . . . . . . . . . . . . . . . . . . . 251
Challenge #11: Adding Paint App Features . . . . . . . . . . . . . . . . . . . . . . 252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
AFTERWORD 255
INDEX 259
xii
ACKNOWLEDGMENTS
I’ve been a fan of No Starch Press books for years, and I’m thrilled to have
them publish my first book. I’d like to thank everybody there, in particular
my editors, Jill Franklin and Annie Choi, for their invaluable feedback and
direction. Thanks to Paddy Gaunt, the technical reviewer, for checking over
all my code and offering some excellent suggestions to improve it.
Additionally, I’d like to thank the creators, maintainers, and community
surrounding Processing and its related projects, and also the developers of
the Python programming language. You’ve inspired my students and me, and
it’s a privilege to share your hard work with everybody who reads this book.
INTRODUCTION
xvi Introduction
understand. Second, it’s a general-purpose language, suitable for program-
ming artificial intelligence (AI), games, simulations, web applications, and
just about everything in between.
Processing, which has been around since the early 2000s, is composed
of a programming language and an editor for writing and compiling code.
It provides a collection of special commands that allow you to draw, animate,
and handle user input by using code. The creators, Casey Reas and Ben Fry,
developed Processing to make programming more accessible for designers
and artists, although its thriving user base has grown to include researchers,
hobbyists, and educators.
Java is the basis for the original Processing programming language,
but other variants have since appeared, including JavaScript (p5.js) and
Ruby (JRubyArt) versions. In 2010, Jonathan Feinberg created Processing.py,
which you can think of as a sort of extension for Processing that allows you
to write Python instead of Java-esque code.
Both Python and Processing are open source and won’t cost you a cent.
What’s more, you can use them on just about any platform, including Linux,
macOS, and Microsoft Windows.
Introduction xvii
What Is Creative Coding?
Creative coding is computer programming for creative output. This broad term
encompasses, but is not limited to, computer-generated audio and visual art,
interactive installations, experimental games, and data visualizations.
Take, for example, Frederic Brodbeck’s Cinemetrics project. Using Python,
Brodbeck developed a program that analyzes DVD movie data to generate
visual fingerprints of films. The fingerprint is an open ring formed from many
segments; a single segment represents a span of 10 shots, and the concentric
bands show the color breakdown for each of those segments. The diagonal
length of each segment indicates the amount of motion. Figure 1 is a
Cinemetrics fingerprint for the film Quantum of Solace (2008).
beginning
end
amount of
motion
1 segment
= 10 shots
chapter
color palette
Figure 1: Quantum of Solace fingerprint, created by Frederic Brodbeck. Screenshot from http://cinemetrics.site/.
xviii Introduction
Figure 2: Fingerprints comparing (left to right) 2001: A Space Odyssey, The Simpsons Movie, and a soccer
match. Screenshots from http://cinemetrics.site/.
Introduction xix
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Figure 3: A Processing.py adaption of Lieven Menschaert’s NodeBox script Aquatics!
xx Introduction
Online Resources
If you’re getting nowhere, reach out to online communities. You’ll find a
dedicated category for Processing.py in the official Processing forum at
https://discourse.processing.org/. You’ll often discover that somebody has already
encountered and received a solution for the challenge you’re facing; if not, go
ahead and create a new topic. Incidentally, the author of this book is known
to lurk about in this friendly and welcoming corner of the internet.
The official Python Mode reference is available at https://py.processing
.org/reference/. Each entry includes a description and brief code example. It’s
handy to keep this web page open while you work in the Processing develop-
ment environment.
Introduction xxi
Chapter 4: Conditional Statements This is where you really begin to
think like a programmer. In this chapter, you’ll introduce control flow
to your programs. In other words, you’ll learn how to write programs
that can make decisions, executing different actions to respond to dif-
ferent situations.
Chapter 5: Iteration and Randomness In this chapter, you’ll learn how
to write programs that can repeat an operation a specified number of
times or until a certain requirement is met. Toward the end of the chap-
ter, you’ll experiment with randomness and creating tiled patterns.
Chapter 6: Motion and Transformation This chapter focuses primar-
ily on adding motion to your Processing programs and transforming the
drawing space. You’ll also learn how to save frames as images and how to
get time values from your computer. You’ll use these skills to create an
animated screensaver and analog clock.
Chapter 7: Working with Lists and Reading Data Python lists will
unlock powerful ways to manage and manipulate values in collections.
You’ll explore techniques for data visualization. You’ll also learn to read
in list data from external files. For the final task, you’ll render a chart by
using a CSV file.
Chapter 8: Dictionaries and JSON Dictionaries are similar to lists
in that they store collections of items. With dictionaries, however, you
access items by using a key (usually a word) instead of referring to the
item position. Once again, you’ll get to use your new dictionary skills
for data visualization. You’ll also learn to work with JSON data.
Chapter 9: Functions and Periodic Motion You’ll use functions to
divide a program into named sections of reusable code. This will make
your code more modular, and easier to read and modify. You’ll also delve
into some trigonometry for generating elliptical and wave-type motions.
Chapter 10: Object-Oriented Programming and PVector You can use
object-oriented programming to structure programs by modeling real-
world objects. In this chapter, you’ll employ an object-oriented approach
to building an amoeba simulation. You’ll also learn to program the
amoebas’ motion by using Processing’s PVector class.
Chapter 11: Mouse and Keyboard Interaction In this chapter, you’ll
add interactivity to your programs. Processing can handle input from
various devices, but here you’ll focus on mouse and keyboard input to
build a paint app. In the process, you’ll learn about event functions and
how to control Processing’s draw loop behavior.
Let’s Go!
The speed at which you progress through these chapters is likely to be influ-
enced by your prior experience in similar areas. If you’ve done any type of
programming before, Python or otherwise, you’ll encounter some familiar
concepts. That said, it’s not a race! Enjoy the ride, stop for breaks, and if
you’re feeling really inspired, feel free to head off-road.
xxii Introduction
1
H E L L O, W O R L D !
Once you’re finished, open the newly extracted folder. Figure 1-1 shows
an abridged listing of what you can expect to see in your file manager. Next,
locate and run the executable file named processing. On macOS, you’ll just
have a single file named processing.
processing-x.x.x
core
java
lib
modes
tools
processing
... ...
The application layout may vary slightly among systems and Processing
versions, but the key elements are outlined in Figure 1-2. If you’re a Mac user,
you’ll find the menu bar in its usual position at the top of your screen. Note
that the upper right button in the Processing interface is labeled Java. This is
because Processing comes bundled with Java mode as the default.
2 Chapter 1
Random documents with unrelated
content Scribd suggests to you:
trade friendly societies was proposed but eventually rejected. A
particularly odious feature of the 1799 Act, under which defendants
were required to give evidence against themselves under severe
penalties for refusal, was left unaltered. A series of interesting
clauses providing for the reference of wage disputes to arbitration—
copied from the contemporary Act relating to the cotton trade[119]—
aroused great opposition, as tending “to fix wages” and as involving
the recognition of the Trade Union representative, but they were
finally adopted; without, so far as we are aware, ever being put in
force. [120]
The general Combination Act of 1800 was not merely the codification
of existing laws, or their extension from particular trades to the
whole field of industry. It represented a new and momentous
departure. Hitherto the central or local authority had acted as a
court of appeal on all questions affecting the work and wages of the
citizen. If the master and journeyman failed to agree as to what
constituted a fair day’s wage for a fair day’s work, the higgling of the
market was peremptorily superseded by the authoritative
determination, presumably on grounds of social expediency, of the
standard of remuneration. Probably the actual fixing of wages by
justices of the peace fell very rapidly into disuse as regards the
majority of industries, although formal orders are found in the
minutes of Quarter Sessions during the first quarter of the
nineteenth century, and deep traces of the practice long survived in
the customary rates of hiring. Towards the end of the eighteenth
century, at any rate, free bargaining between the capitalist and his
workmen became practically the sole method of fixing wages. Then
it was that the gross injustice of prohibiting combinations of
journeymen became apparent. “A single master,” said Lord Jeffrey,
“was at liberty at any time to turn off the whole of his workmen at
once—100 or 1000 in number—if they would not accept of the
wages he chose to offer. But it was made an offence for the whole of
the workmen to leave that master at once if he refused to give the
wages they chose to require.”[121] What was even more oppressive
in practice was the employers’ use of the threat of prosecution to
prevent even the beginnings of resistance among the workmen to
any reduction of wages or worsening of conditions.
It is true that the law forbade combinations of employers as well as
combinations of journeymen. Even if it had been impartially carried
out, there would still have remained the inequality due to the fact
that, in the new system of industry, a single employer was himself
equivalent to a very numerous combination. But the hand of justice
was not impartial. The “tacit, but constant” combination of
employers to depress wages, to which Adam Smith refers, could not
be reached by the law. Nor was there any disposition on the part of
the magistrates or the judges to find the masters guilty, even in
cases of flagrant or avowed combination. No one prosecuted the
master cutlers who, in 1814, openly formed the Sheffield Mercantile
and Manufacturing Union, having for its main rule that no merchant
or manufacturer should pay higher prices for any article of Sheffield
make than were current in the preceding year, with a penalty of
£100 for each contravention of this illegal agreement.[122] During
the whole epoch of repression, whilst thousands of journeymen
suffered for the crime of combination, there is no case on record in
which an employer was punished for the same offence.
To the ordinary politician a combination of employers and a
combination of workmen seemed in no way comparable. The former
was, at most, an industrial misdemeanour: the latter was in all cases
a political crime. Under the shadow of the French Revolution, the
English governing classes regarded all associations of the common
people with the utmost alarm. In this general terror lest
insubordination should develop into rebellion were merged both the
capitalist’s objection to high wages and the politician’s dislike of
Democratic institutions. The Combination Laws, as Francis Place tells
us, “were considered as absolutely necessary to prevent ruinous
extortions of workmen, which, if not thus restrained, would destroy
the whole of the Trade, Manufactures, Commerce, and Agriculture of
the nation.... This led to the conclusion that the workmen were the
most unprincipled of mankind. Hence the continued ill-will,
suspicion, and in almost every possible way the bad conduct of
workmen and their employers towards one another. So thoroughly
was this false notion entertained that whenever men were
prosecuted to conviction for having combined to regulate their
wages or the hours of working, however heavy the sentence passed
on them was, and however rigorously it was inflicted, not the
slightest feeling of compassion was manifested by anybody for the
unfortunate sufferers. Justice was entirely out of the question: they
could seldom obtain a hearing before a magistrate, never without
impatience or insult; and never could they calculate on even an
approximation to a rational conclusion.... Could an accurate account
be given of proceedings, of hearings before magistrates, trials at
sessions and in the Court of King’s Bench, the gross injustice, the
foul invective, and terrible punishments inflicted would not, after a
few years have passed away, be credited on any but the best
evidence.” [123]
It must not, however, be supposed that every combination was
made the subject of prosecution, or that the Trade Union leader of
the period passed his whole life in gaol. Owing to the extremely
inefficient organisation of the English police, and the absence of any
public prosecutor, a combination was usually let alone until some
employer was sufficiently inconvenienced by its operations to be
willing himself to set the law in motion. In many cases we find
employers apparently accepting or conniving at their men’s
combinations.[124] The master printers in London not only
recognised the very ancient institution of the “chapel,” but evidently
found it convenient, at any rate from 1785 onwards, to receive and
consider proposals from the journeymen as an organised body. In
1804 we even hear of a joint committee consisting of an equal
number of masters and journeymen, authorised by their respective
bodies to frame regulations for the future payment of labour, and
resulting in the elaborate “scale” of 1805, signed by both masters
and men.[125] The London coopers had a recognised organisation in
1813, in which year a list of prices was agreed upon by
representatives of the masters and men. This list was revised in
1816 and 1819, without any one thinking of a prosecution.[126] The
Trade Union was openly reformed in 1821 as the Philanthropic
Society of Coopers. The London brushmakers in 1805 had “A List of
Prices agreed upon between the Masters and Journeymen,” which is
still extant. The framework knitters, and also the tailors of the
various villages in Nottinghamshire, were, from 1794 to 1810, in the
habit of freely meeting together, both masters and men, “to consider
of matters relative to the trade,” the conferences being convened by
public advertisement.[127] The minute books of the local Trade Union
of the carpenters of Preston for the years 1807 to 1824 chronicle an
apparently unconcealed and unmolested existence, in
correspondence with other carpenters’ societies throughout
Lancashire. The accounts contain no items for the expense of
defending their officers against prosecutions, whereas there are
several payments for advertisements and public meetings, and, be it
added, a very large expenditure in beer. And there is a lively
tradition among the aged block printers of Glasgow that, in their
fathers’ time, when their very active Trade Union exacted a fee of
seven guineas from each new apprentice, this money was always
straightway drunk by the men of the print-field, the employer taking
his seat at the head of the table, and no work being done by any
one until the fund was exhausted. The calico-printers’ organisation
appears, at the early part of the nineteenth century, to have been
one of the strongest and most complete of the Unions. In an
impressive pamphlet of 1815 the men are thus appealed to by the
employers: “We have by turns conceded what we ought all manfully
to have resisted, and you, elated with success, have been led on
from one extravagant demand to another, till the burden is become
too intolerable to be borne. You fix the number of our apprentices,
and oftentimes even the number of our journeymen. You dismiss
certain proportions of our hands, and will not allow others to come
in their stead. You stop all Surface Machines, and go the length even
to destroy the rollers before our face. You restrict the Cylinder
Machine, and even dictate the kind of pattern it is to print. You
refuse, on urgent occasions, to work by candlelight, and even
compel our apprentices to do the same. You dismiss our overlookers
when they don’t suit you; and force obnoxious servants into our
employ. Lastly, you set all subordination and good order at defiance,
and instead of showing deference and respect to your employers,
treat them with personal insult and contempt.”[128] Notwithstanding
all this, no systematic attempt appears to have been made to put
down the calico-printers’ combination, and only one or two isolated
prosecutions can be traced. In Dublin, too, the cabinetmakers in the
early part of the present century were combined in a strong union
called the Samaritan Society, exclusively for trade purposes; “but
though illegal, the employers do not seem to have looked upon it
with any great aversion; and when on one occasion the chief
constable had the men attending a meeting arrested, the employers
came forward to bail them. Indeed, they professed that their object,
though primarily to defend their own interests against the masters,
was also to defend the interests of the masters against unprincipled
journeymen. Many of the masters on receiving the bill of a
journeyman were in the habit of sending it to the trades’ society
committee to be taxed, after which the word Committee was
stamped upon it. One case was mentioned, when between two and
three pounds were knocked off a bill of about eight pounds by the
trade committee.” [129] And both in London and Edinburgh the
journeymen openly published, without fear of prosecution, elaborate
printed lists of piecework prices, compiled sometimes by a
committee of the men’s Trade Union, sometimes by a joint
committee of employers and employed.[130]“The London
Cabinetmakers’ Union Book of Prices,” of which editions were
published in 1811 and 1824, was a costly and elaborate work, with
many plates, published “by a Committee of Masters and Journeymen
... to prevent those litigations which have too frequently existed in
the trade.” Various supplements and “index keys” to this work were
published; and other similar lists exist. So lax was the administration
of the law that George White, the energetic clerk to Hume’s
Committee, asserted that the Act of 1800 had “been in general a
dead letter upon those artisans upon whom it was intended to have
an effect—namely, the shoemakers, printers, papermakers,
shipbuilders, tailors, etc., who have had their regular societies and
houses of call, as though no such Act was in existence; and in fact it
would be almost impossible for many of those trades to be carried
on without such societies, who are in general sick and travelling
relief societies; and the roads and parishes would be much pestered
with these travelling trades, who travel from want of employment,
were it not for their societies who relieve what they call tramps.”
[131]