6 Creating oscillations with trigonometry 103 Using Trigonometry for Rotations and Oscillations 105 Writing Functions to Draw Polygons 106 Drawing a Hexagon with Loops 107 Drawing an Equilateral Triangle 109 Making Sine Waves 110 Leaving a Trail 113 Using Python’s Built-in enumerate Function 114 Creating a Spirograph Program 116 Drawing the Smaller Circle 117 Rotating the Smaller Circle 117 Making Harmonographs 120 Writing the harmonograph Program 121 Filling the List Instantly 123 Two Pendulums Are Better Than One 124 Summary 126 7 Complex numbers 127 The Complex Coordinate System 128 Adding Complex Numbers 129 Multiplying a Complex Number by i 130 Multiplying Two Complex Numbers 131 Writing the magnitude Function 132 Creating the Mandelbrot Set 132 Writing the mandelbrot Function 135 Adding Color to the Mandelbrot Set 139 Creating the Julia Set 141 Writing the julia Function 141 Summary 143 8 Using Matrices for Computer Graphics 145 and systems of equations What Is a Matrix 146 Adding Matrices 146 Multiplying Matrices 147 Order Matters in Matrix Multiplication 151 Drawing 2D Shapes 151 Transforming Matrices 154 Transposing Matrices 156 Rotating Matrices in Real Time 160 Creating 3D Shapes 161 Creating the Rotation Matrix 162 Solving Systems of Equations with Matrices 166 Gaussian Elimination 167 Writing the gauss Function 168 Summary 172 Contents in Detail
Part 3 Blazing Your own trail 9 175 Building objects with Classes Bouncing Ball Program 177 Making the Ball Move 178 Making the Ball Bounce Off the Wall 179 Making Multiple Balls Without Classes 181 Creating Objects Using Classes 182 Grazing Sheep Program 186 Writing the Class for the Sheep 186 Programming Sheep to Move Around 187 Creating the energy Property 189 Creating Grass Using Classes 189 Making the Grass Brown when Eaten 192 Giving Each Sheep a Random Color 194 Programming Sheep to Reproduce 196 Letting the Grass Regrow 197 Providing an Evolutionary Advantage 198 Summary 200 10 Creating Fractals Using Recursion 201 The Length of a Coastline 202 What Is Recursion 203 Writing the factorial Function 203 Building a Fractal Tree 204 Koch Snowflake 209 Writing the segment Function 210 Sierpinski Triangle 214 Square Fractal 216 Dragon Curve 220 Summary 224 11 Cellular automata 225 Creating a Cellular Automaton 226 Writing a Cell Class 228 Resizing Each Cell 230 Making a CA Grow 231 Putting the Cells into a Matrix 232 Creating the Cell List 233 Python Lists Are Strange 234 List Index Notation 235 Letting Your CA Grow Automatically 238 Playing the Game of Life 238 The Elementary Cellular Automaton 241 Summary 246 Contents in Detail
12 solving Problems Using Genetic algorithms 247 Using a Genetic Algorithm to Guess Phrases 248 Writing the makeList Function 248 Testing the makeList Function 249 Writing the score Function 250 Writing the mutate Function 250 Generating a Random Number 251 Solving the Traveling Salesperson Problem TSP 254 Using Genetic Algorithms 254 Writing the calcLength Method 260 Testing the calcLength Method 261 Random Routes 262 Applying the Phrase-Guessing Mutation Idea 265 Mutating Two Numbers in a List 265 Crossing Over to Improve Routes 269 Summary 271 index 273 Contents in Detail
Acknowledgments I’d like to thank Don The Mathman Cohen for showing me how fun and challenging learning real math can be Seymour Papert for proving that coding belongs in math class Mark Miller for giving me a chance to put my ideas into action Hansel Lynn and Wayne Teng of theCoderSchool who let me continue to have fun coding with students and Ken Hawthorn for sharing my projects at his school Thank you to my No Starch editors Annie Choi Liz Chadwick and Meg Sneeringer for all your help making this a much better book and to Paddy Gaunt whose input is visible all over this book This book wouldn’t exist without you all Thank you to everybody who said no you gave me the energy to keep going Finally thank you to Lucy for always believing in me