PDF (Ebook) Learn Python Visually: Creative Coding with Processing.py by Tristan Bunn ISBN 9781718500969, 9781718500976, 1718500963, 1718500971 download
PDF (Ebook) Learn Python Visually: Creative Coding with Processing.py by Tristan Bunn ISBN 9781718500969, 9781718500976, 1718500963, 1718500971 download
com
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
ebooknice.com
(Ebook) Cambridge IGCSE and O Level History Workbook 2C -
Depth Study: the United States, 1919-41 2nd Edition by
Benjamin Harrison ISBN 9781398375147, 9781398375048,
1398375144, 1398375047
https://ebooknice.com/product/cambridge-igcse-and-o-level-history-
workbook-2c-depth-study-the-united-states-1919-41-2nd-edition-53538044
ebooknice.com
https://ebooknice.com/product/learn-data-analysis-with-python-lessons-
in-coding-44859412
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.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
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
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
Processing
Java Toolbar
Filename Tabs
Code editor
Message area
Console area
Console
Next, activate Python Mode. Click the Java button and select Add Mode
from the drop-down menu; then, from the Contribution Manager window
that appears, choose Python Mode for Processing. Finally, click Install. You
can now change between Python and Java mode by using the drop-down
menu. Switch to Python (Figure 1-3).
Processing
Python
Figure 1-3: The button to the right indicates that Python Mode is activated.
NOTE For a demonstration of what Processing can do, take a look at the Python
examples (FileExamples) included with Processing. To run any example,
use the play () button.
Hello, World! 3
Your First Sketch
Processing refers to programs as sketches. Given the visual and artistic nature
of what you are likely to produce, it’s a fitting term. Select FileNew to cre
ate a new sketch, or use the associated keyboard shortcut (listed alongside
the menu entry).
Enter the following lines of code:
size(500, 500)
print('Hello, World!')
I’ll go through the specifics of this code in a bit. For now, save the
sketch by using FileSave As and name it hello_world.
You will notice that Processing creates a new folder named hello_world;
within it, there are two files: hello_world.pyde and sketch.properties (Figure 1-4).
Depending on your system’s configuration, you may or may not see the file
extension (.pyde). To reopen any sketch, locate and open the .pyde file.
hello_world
hello_world.pyde
sketch.properties
You may want to add other assets to your sketch folders, such as images
and fonts, but more on that later.
Next, click the play () button to execute the code. Better yet, use the
associated keyboard shortcut: CTRL-R for Windows and Linux, or -R for
macOS. A gray 500 × 500 pixel display window should appear. In the console,
which is the black area at the bottom of the editor, Processing should dis
play Hello, World! (Figure 1-5).
Now let’s get back to the code you entered in this file; it uses two
Processing functions: size() and print(). Functions are named instructions,
sort of like dog commands for computers. Some commands are simple, like
“sit,” but something like “fetch” may involve specifying what it is that Fido
should retrieve.
Python functions consist of a function name followed by opening and
closing parentheses, which is where you provide arguments. In the case of my
dog-command analogy, “ball” could be an argument for “fetch.” The size()
function (Figure 1-6) takes two arguments: the first represents the width of
your sketch, and the second is the height.
In this case, the display window is 500 pixels wide by 500 pixels high.
4 Chapter 1
hello_world
File Edit Sketch Tools He
hello_world
1 size(500, 500)
2 print('Hello, World!')
Hello, World!
Console
Figure 1-5: Display window (left) and an editor with ′Hello, World!′ displayed in
the console (right)
size(500, 500)
Arguments
The print() function writes to the console. This function takes a single
argument: the phrase 'Hello, World!' Because this is text—or technically,
string data—you need to wrap it in quotation marks. You can use single or
double quotes, but be sure to use the same type for both opening and clos
ing quotes.
Python classifies every value according to a data type, which deter
mines how the value is handled and the operations you can perform on
it. For example, you can perform arithmetic operations—like division or
subtraction—on numeric data types but not on strings. In this chapter,
you’ll deal with three data types:
String Text data, like ‘Hello, World!’
Integer Numbers without decimal points, such as 1, –27, or 422
Floating-point Numbers that include a decimal point, such as 1.618
Hello, World! 5
What separates Processing code from standard Python code are some
of its functions; the size() function, for example, is Processing-specific. In
other words, it won’t work outside the Processing environment. The print()
function, on the other hand, is a built-in element of the standard Python
programming language. It works in Processing’s Python Mode and any
other Python program.
Throughout this book, I usually refer to Processing-exclusive features in
the context of Processing, and standard Python features with the term Python.
If this is confusing, think of them as one and the same. At this point, distin
guishing Processing from Python isn’t crucial; you will understand the dif
ferences in time.
The type, number, and order of arguments vary according to the function,
and some functions don’t require arguments. For a complete list of Processing
functions and the arguments they require, as well as many standard Python
elements, refer to the Processing.py reference at https://py.processing.org/
reference/.
This book covers a lot of that online content, but the reference should be
your go-to source for in-depth descriptions of each function.
Comments
If you want Python to ignore any part of your code, you can comment it
out. This feature is useful for leaving notes, in plain English, to yourself
or anybody else editing your code. Let’s add a few comments to your
hello_world file:
6 Chapter 1
Exploring the Variety of Random
Documents with Different Content
place. I was convinced that the greatest calamity that ever befell the
benighted nations of the ancient world was in their having passed
away without a knowledge of the actual existence of Duluth; that
their fabled Atlantis, never seen save by the hallowed vision of the
inspired poesy, was, in fact, but another name for Duluth; that the
golden orchard of the Hesperides, was but a poetical synonym for the
beer-gardens in the vicinity of Duluth. I was certain that Herodotus
had died a miserable death, because in all his travels and with all his
geographical research he had never heard of Duluth. I knew that if
the immortal spirit of Homer could look down from another heaven
than that created by his own celestial genius upon the long lines of
pilgrims from every nation of the earth to the gushing fountain of
poesy opened by the touch of his magic wand, if he could be
permitted to behold the vast assemblage of grand and glorious
productions of the lyric art called into being by his own inspired
strains, he would weep tears of bitter anguish that, instead of
lavishing all the stores of his mighty genius upon the fall of Illion, it
had not been his more blessed lot to crystalize in deathless song the
rising glories of Duluth. Yes, sir, had it not been for this map, kindly
furnished me by the legislature of Minnesota, I might have gone
down to my obscure and humble grave in an agony of despair,
because I could nowhere find Duluth. Had such been my melancholy
fate, I have no doubt that with the last feeble pulsation of my
breaking heart, with the last faint exhalation of my fleeting breath, I
should have whispered, “Where is Duluth?”
But, thanks to the beneficence of that band of ministering angels
who have their bright abodes in the far-off capital of Minnesota, just
as the agony of my anxiety was about to culminate in the frenzy of
despair, this blessed map was placed in my hands; and as I unfolded
it a resplendent scene of ineffable glory opened before me, such as I
imagined burst upon the enraptured vision of the wandering peri
through the opening gates of Paradise. There, there, for the first
time, my enchanted eye rested upon the ravishing word, “Duluth!”
This map, sir, is intended, as it appears from its title, to illustrate the
position of Duluth in the United States; but if gentlemen will
examine it, I think they will concur with me in the opinion, that it is
far too modest in its pretensions. It not only illustrates the position
of Duluth in the United States, but exhibits its relations with all
created things. It even goes further than this. It hits the shadowy vale
of futurity, and affords us a view of the golden prospects of Duluth
far along the dim vista of ages yet to come.
If gentlemen will examine it, they will find Duluth not only in the
center of the map, but represented in the center of a series of
concentric circles one hundred miles apart, and some of them as
much as four thousand miles in diameter, embracing alike, in their
tremendous sweep the fragrant savannas of the sunlit South and the
eternal solitudes of snow that mantle the ice-bound North. How
these circles were produced is perhaps one of those primordial
mysteries that the most skilled paleologist will never be able to
explain. But the fact is, sir, Duluth is pre-eminently a central point,
for I am told by gentlemen who have been so reckless of their own
personal safety as to venture away into those awful regions where
Duluth is supposed to be, that it is so exactly in the center of the
visible universe that the sky comes down at precisely the same
distance all around it.
I find, by reference to this map, that Duluth is situated somewhere
near the western end of Lake Superior, but as there is no dot or other
mark indicating its exact location, I am unable to say whether it is
actually confined to any particular spot, or whether “it is just lying
around there loose.” I really cannot tell whether it is one of those
ethereal creations of intellectual frostwork, more intangible than the
rose-tinted clouds of a summer sunset; one of those airy exhalations
of the speculator’s brain which, I am told, are very flitting in the form
of towns and cities along those lines of railroad, built with
government subsidies, luring the unwary settler as the mirage of the
desert lures the famishing traveler on, and ever on, until it fades
away in the darkening horizon; or whether it is a real, bona fide,
substantial city, all “staked off,” with the lots marked with their
owners’ names, like that proud commercial metropolis recently
discovered on the desirable shores of San Domingo. But, however
that may be, I am satisfied Duluth is there, or thereabouts, for I see it
stated here on the map that it is exactly thirty-nine hundred and
ninety miles from Liverpool, though I have no doubt, for the sake of
convenience, it will be moved back ten miles, so as to make the
distance an even four thousand.
Then, sir, there is the climate of Duluth, unquestionably the most
salubrious and delightful to be found anywhere on the Lord’s earth.
Now, I have always been under the impression, as I presume other
gentlemen have, that in the region around Lake Superior it was cold
enough for at least nine months in the year to freeze the smoke-stack
off a locomotive. But I see it represented on this map that Duluth is
situated exactly half way between the latitudes of Paris and Venice,
so that gentlemen who have inhaled the exhilarating air of the one,
or basked in the golden sunlight of the other, may see at a glance that
Duluth must be the place of untold delight, a terrestrial paradise,
fanned by the balmy zephyrs of an eternal spring, clothed in the
gorgeous sheen of ever blooming flowers, and vocal with the silvery
melody of nature’s choicest songsters. In fact sir, since I have seen
this map, I have no doubt that Byron was vainly endeavoring to
convey some faint conception of the delicious charms of Duluth
when his poetic soul gushed forth, in the rippling strains of that
beautiful rhapsody—
“Know ye the land of the cedar and the vine,
Whence the flowers ever blossom, the beams ever shine;
Where the light wings of Zephyr, oppressed with perfume,
Wax faint o’er the gardens of Gul in her bloom;
Where the citron and olive are fairest of fruit,
And the voice of the nightingale never is mute;
Where the tints of the earth and the hues of the sky,
In color though varied, in beauty may vie?”
Sir, the great conflict now raging in the Old World has presented a
phenomenon in military science unprecedented in the annals of
mankind, a phenomenon that has reversed all the traditions of the
past as it has disappointed all the expectations of the present. A great
and warlike people, renowned alike for their skill and valor, have
been swept away before the triumphant advance of an inferior foe,
like autumn stubble before a hurricane of fire. For aught I know the
next flash of electric fire that simmers along the ocean cable may tell
us that Paris, with every fibre quivering with the agony of impotent
despair, writhes beneath the conquering heel of her loathed invader.
Ere another moon shall wax and wane, the brightest star in the
galaxy of nations may fall from the zenith of her glory never to rise
again. Ere the modest violets of early spring shall ope their
beauteous eyes, the genius of civilization may chant the wailing
requiem of the proudest nationality the world has ever seen, as she
scatters her withered and tear-moistened lilies o’er the bloody tomb
of butchered France. But, sir, I wish to ask if you honestly and
candidly believe that the Dutch would have overrun the French in
that kind of style if General Sheridan had not gone over there, and
told King William and Von Moltke how he had managed to whip the
Piegan Indians.
And here, sir, recurring to this map, I find in the immediate
vicinity of the Piegans “vast herds of buffalo” and “immense fields of
rich wheat lands.” [Here the hammer fell.]
[Many cries: “Go on!” “go on!”]
The Speaker—Is there any objection to the gentleman from
Kentucky continuing his remarks? The chair hears none. The
gentleman will proceed.
Mr. Knott—I was remarking, sir, upon these vast “wheat fields”
represented on this map in the immediate neighborhood of the
buffaloes and Piegans, and was about to say that the idea of there
being these immense wheat fields in the very heart of a wilderness,
hundreds and hundreds of miles beyond the utmost verge of
civilization, may appear to some gentlemen as rather incongruous, as
rather too great a strain on the “blankets” of veracity. But to my mind
there is no difficulty in the matter whatever. The phenomenon is very
easily accounted for. It is evident, sir, that the Piegans sowed that
wheat there and ploughed it in with buffalo bulls. Now, sir, this
fortunate combination of buffaloes and Piegans, considering their
relative positions to each other and to Duluth, as they are arranged
on this map, satisfies me that Duluth is destined to be the best
market of the world. Here, you will observe, (pointing to the map),
are the buffaloes, directly between the Piegans and Duluth; and here,
right on the road to Duluth, are the Creeks. Now, sir, when the
buffaloes are sufficiently fat from grazing on those immense wheat
fields, you see it will be the easiest thing in the world for the Piegans
to drive them on down, stay all night with their friends, the Creeks,
and go into Duluth in the morning. I think I see them, now, sir, a vast
herd of buffaloes, with their heads down, their eyes glaring, their
nostrils dilated, their tongues out, and their tails curled over their
backs, tearing along toward Duluth, with about a thousand Piegans
on their grass-bellied ponies, yelling at their heels! On they come!
And as they sweep past the Creeks, they join in the chase, and away
they all go, yelling, bellowing, ripping and tearing along, amid clouds
of dust, until the last buffalo is safely penned in the stock-yards at
Duluth.
Sir, I might stand here for hours and hours, and expatiate with
rapture upon the gorgeous prospects of Duluth, as depicted upon this
map. But human life is too short, and the time of this house far too
valuable to allow me to linger longer upon this delightful theme. I
think every gentleman upon this floor is as well satisfied as I am that
Duluth is destined to become the commercial metropolis of the
universe and that this road should be built at once. I am fully
persuaded that no patriotic representative of the American people,
who has a proper appreciation of the associated glories of Duluth and
the St. Croix, will hesitate a moment that every able-bodied female in
the land, between the ages of eighteen and forty-five, who is in favor
of “woman’s rights,” should be drafted and set to work upon this
great work without delay. Nevertheless, sir, it grieves my very soul to
be compelled to say that I cannot vote for the grant of lands provided
for in this bill.
Ah, sir, you can have no conception of the poignancy of my
anguish that I am deprived of that blessed privilege! There are two
insuperable obstacles in the way. In the first place my constituents,
for whom I am acting here, have no more interest in this road than
they have in the great question of culinary taste now, perhaps,
agitating the public mind of Dominica, as to whether the illustrious
commissioners, who recently left this capital for that free and
enlightened republic, would be better fricasseed, boiled, or roasted,
and, in the second place, these lands, which I am asked to give away,
alas, are not mine to bestow! My relation to them is simply that of
trustee to an express trust. And shall I ever betray that trust? Never,
sir! Rather perish Duluth! Perish the paragon of cities! Rather let the
freezing cyclones of the bleak northwest bury it forever beneath the
eddying sands of the raging St. Croix.
Henry Carey’s Speech on the Rates of
Interest.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com