Table of Contents
Preface ix
1 Hello 1
Sketching and Prototyping 2
Flexibility 3
Giants 3
Family Tree 5
Join In 5
2 Starting to Code 7
Python Mode 8
Your First Program 9
Example 2-1 Draw an Ellipse 9
Example 2-2 Make Circles 10
Show 10
Save and New 11
Share 11
Examples and Reference 12
3 Draw 13
The Display Window 13
Example 3-1 Draw a Window 14
Example 3-2 Draw a Point 14
Basic Shapes 14
Example 3-3 Draw a Line 16
Example 3-4 Draw Basic Shapes 16
Example 3-5 Draw a Rectangle 16
Example 3-6 Draw an Ellipse 17
Example 3-7 Draw Part of an Ellipse 18
Example 3-8 Draw with Degrees 19
Drawing Order 20
Example 3-9 Control Your Drawing Order 20
Example 3-10 Put It in Reverse 20
Shape Properties 21
Example 3-11 Set Stroke Weight 21
Example 3-12 Set Stroke Caps 21
Example 3-13 Set Stroke Joins 22
Drawing Modes 22
Example 3-14 On the Corner 23
Color 23
Example 3-15 Paint with Grays 25
Example 3-16 Control Fill and Stroke 25
Example 3-17 Draw with Color 26
Example 3-18 Set Transparency 27
Custom Shapes 28
Example 3-19 Draw an Arrow 28
Example 3-20 Close the Gap 28
Example 3-21 Create Some Creatures 29
Comments 30
Robot 1: Draw 31
4 Variables 35
First Variables 35
Example 4-1 Reuse the Same Values 35
Example 4-2 Change Values 36
Making Variables 36
Processing Variables 37
Example 4-3 Adjust the Size, See What Follows 37
A Little Math 37
Example 4-4 Basic Arithmetic 38
Repetition 39
Example 4-5 Do the Same Thing Over and Over 39
Example 4-6 Use a for Loop 40
Example 4-7 Flex Your for Loop's Muscles 41
Example 4-8 Fanning Out the Lines 41
Example 4-9 Kinking the Lines 42
Example 4-10 Embed One for Loop in Another 42
Example 4-11 Rows and Columns 43
Example 4-12 Pins and Lines 44
Example 4-13 Halftone Dots 44
Robot 2 Variables 45
5 Response 47
Once and Forever 47
Example 5-1 The draw() Function 47
Example 5-2 The setup() Function 48
Example 5-3 Global Variables 49
Follow 49
Example 5-4 Track the Mouse 49
Example 5-5 The Dot Follows You 50
Example 5-6 Draw Continuously 50
Example 5-7 Set Thickness on the Fly 51
Example 5-8 Easing Does It 52
Example 5-9 Smooth Lines with Easing 53
Modifying Global Variables 54
Click 55
Example 5-10 Click the Mouse 56
Example 5-11 Detect When Not Clicked 57
Example 5-12 Multiple Mouse Buttons 58
Location 59
Example 5-13 Find the Cursor 60
Example 5-14 The Bounds of a Circle 61
Example 5-15 The Bounds of a Rectangle 62
Type 64
Example 5-16 Tap a Key 65
Example 5-17 Draw Some Letters 65
Example 5-18 Check for Specific Keys 66
Example 5-19 Move with Arrow Keys 67
Map 68
Example 5-20 Map Values to a Range 68
Example 5-21 Map with the map() Function 69
Robot 3 Response 70
6 Translate, Rotate, Scale 73
Translate 73
Example 6-1 Translating Location 74
Example 6-2 Multiple Translations 74
Rotate 75
Example 6-3 Corner Rotation 76
Example 6-4 Center Rotation 76
Example 6-5 Translation, Then Rotation 77
Example 6-6 Rotation, Then Translation 78
Example 6-7 An Articulating Arm 79
Scale 80
Example 6-8 Scaling 80
Example 6-9 Keeping Strokes Consistent 81
Push and Pop 81
Example 6-10 Isolating Transformations 82
Robot 4 Translate, Rotate, Scale 83
7 Media 85
Images 86
Example 7-1 Load an Image 86
Example 7-2 Load More Images 87
Example 7-3 Mousing Around with Images 88
Example 7-4 Transparency with a GIF 90
Example 7-5 Transparency with a PNG 90
Fonts 91
Example 7-6 Drawing with Fonts 92
Example 7-7 Draw Text in a Box 93
Example 7-8 Store Text in a String 93
Shapes 94
Example 7-9 Draw with Shapes 94
Example 7-10 Scaling Shapes 95
Example 7-11 Creating a New Shape 95
Robot 5 Media 97
8 Motion 99
Frames 99
Example 8-1 See the Frame Rate 99
Example 8-2 Set the Frame Rate 100
Speed and Direction 100
Example 8-3 Move a Shape 101
Example 8-4 Wrap Around 101
Example 8-5 Bounce Off the Wall 103
Tweening 104
Example 8-6 Calculate Tween Positions 104
Random 105
Example 8-7 Generate Random Values 105
Example 8-8 Draw Randomly 106
Example 8-9 Move Shapes Randomly 106
Timers 108
Example 8-10 Time Passes 108
Example 8-11 Triggering Timed Events 108
Circular 109
Example 8-12 Sine Wave Values 111
Example 8-13 Sine Wave Movement 111
Example 8-14 Circular Motion 112
Example 8-15 Spirals 113
Robot 6 Motion 114
9 Functions 117
Function Basics 118
Example 9-1 Roll the Dice 118
Example 9-2 Another Way to Roll 119
Make a Function 120
Example 9-3 Draw the Owl 120
Example 9-4 Two's Company 121
Example 9-5 An Owl Function 122
Example 9-6 Increasing the Surplus Population 124
Example 9-7 Owls of Different Sizes 124
Return Values 125
Example 9-8 Return a Value 126
Robot 7 Functions 127
10 Objects 129
Fields and Methods 130
Define a Class 131
Create Objects 135
Example 10-1 Make an Object 136
Example 10-2 Make Multiple Objects 136
Code in Tabs 137
Robot 8 Objects 139
11 Lists 141
From Variables to Lists 141
Example 11-1 Many Variables 141
Example 11-2 Too Many Variables 142
Example 11-3 Lists, Not Variables 143
List Operations 144
Example 11-4 Declare and Append to a List 146
Example 11-5 Compact List Initialization 146
Example 11-6 Revisiting the First Example 146
Repetition and Lists 147
Example 11-7 Filling a List in a for Loop 147
Example 11-9 Track Mouse Movements 148
Lists of Objects 149
Example 11-10 Managing Many Objects 149
Example 11-11 A New Way to Manage Objects 150
Example 11-12 Sequences of Images 151
Robot 9 Lists 152
12 Data and Dictionaries 155
Data Summary 155
Dictionaries 156
Example 12-1 (Keyboard) Keys as (Dictionary) Keys 157
Lists of Dictionaries 158
Example 12-2 The Planets 161
CSV Files 162
Example 12-3 Read the Data 162
Getting the Right Type 163
Example 12-4 Draw the Table 164
Example 12-5 29,740 Cities 166
Json 168
Example 12-6 Read a JSON File 169
Example 12-7 Visualize Data from a JSON File 170
Network Data and APIs 172
Example 12-8 Parsing the Weather Data 175
Example 12-9 Chaining Square Brackets 175
Robot 10 Data 176
13 Extend 179
Sound 180
Example 13-1 Play a Sample 181
Example 13-2 Listen to a Microphone 182
Example 13-3 Create a Sine Wave 184
Image and PDF Export 185
Example 13-4 Saving Images 185
Example 13-5 Draw to a PDF 187
Hello, Arduino 188
Example 13-6 Read a Sensor 189
Example 13-7 Read Data from the Serial Port 191
Example 13-8 Visualizing the Data Stream 192
Example 13-9 Another Way to Look at the Data 193
A Coding Tips 197
B Data Types 203
C Order of Operations 205
D Variable Scope 207
E Processing, Python, and Java 211
Index 215