Mathematical Artwork in Python Collin J
Total Page:16
File Type:pdf, Size:1020Kb
Mathematical Artwork in Python Collin J. Delker Mathematical Artwork in Python © 2020 Collin J. Delker, All Rights Reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. Editing and Code Testing: David G. Delker https://codeismycanvas.art Albuquerque, New Mexico, USA Cover Image: Newton Polynomial fractal with orbit traps of a thin strip along the real axis and a small circle. CONTENTS: 1 Introduction 1 1.1 History ........................................... 2 1.2 Use of Python ....................................... 3 1.3 Saving your Images ................................... 5 1.4 Organization ....................................... 6 2 Math and Python Background 7 2.1 Numerical Computations ................................ 7 2.2 Trigonometric functions ................................ 9 2.3 Polar and Rectangular Coordinates .......................... 10 2.4 Coordinate Formulas .................................. 10 2.5 Complex Numbers .................................... 11 2.6 Histograms ........................................ 12 3 Computer Graphics 15 3.1 Raster Graphics ..................................... 15 3.2 Vector Graphics ...................................... 17 3.3 Representing Color ................................... 19 4 Parametric Methods 23 4.1 Pendulums ......................................... 24 4.2 Harmonographs ..................................... 27 4.3 Spirograph ........................................ 31 4.4 Non-physical Curves ................................... 33 4.5 Fancier coloring ..................................... 35 4.6 Using Many Shapes ................................... 37 4.7 Parametric Drawings .................................. 40 4.8 Examples Gallery ..................................... 47 4.9 Experiments ........................................ 51 5 Randomness 53 5.1 Random number generation .............................. 54 5.2 Making Randomized Curves .............................. 56 5.3 Physics with Randomness ............................... 62 5.4 Textured Lines ...................................... 67 5.5 Textured Shapes and Backgrounds .......................... 71 5.6 Gallery ........................................... 75 5.7 Experiments ........................................ 79 6 Orbits and Escape Fractals 81 6.1 Iterative systems ..................................... 81 i 6.2 Escape Time ........................................ 86 6.3 Variations ......................................... 92 6.4 Refining .......................................... 95 6.5 Exploring ......................................... 100 6.6 Gallery ........................................... 100 6.7 Experiments ........................................ 102 7 Orbit Traps 103 7.1 Trap shapes ........................................ 109 7.2 Trap coloring methods ................................. 113 7.3 Multiple Traps ...................................... 118 7.4 Recurrence functions .................................. 119 7.5 Tips ............................................. 120 7.6 Examples ......................................... 121 7.7 Experiments ........................................ 123 8 Orbit Density Fractals 125 8.1 Attractors ......................................... 125 8.2 Coloring techniques ................................... 131 8.3 Orbits that Escape .................................... 137 8.4 Examples Gallery ..................................... 142 8.5 Experiments ........................................ 143 9 The Buddhabrot 145 9.1 Optimizations ....................................... 150 9.2 Iteration Depth ...................................... 151 9.3 Noise ............................................ 153 9.4 Coloring .......................................... 155 9.5 Zooming .......................................... 157 9.6 Minimum Iterations ................................... 157 9.7 Gallery ........................................... 163 9.8 Experiments ........................................ 166 10 Color Theory 167 10.1 Hue, Saturation, Lightness ............................... 167 10.2 Color Wheels ....................................... 168 10.3 Palettes and Schemes .................................. 171 10.4 Gradients ......................................... 174 10.5 Experiments ........................................ 177 11 Composition 179 11.1 Simplicity ......................................... 179 11.2 Rule of Thirds ....................................... 180 11.3 Leading Lines and Curves ............................... 181 11.4 Balance ........................................... 182 11.5 Framing .......................................... 183 11.6 Avoid Mergers ...................................... 184 12 Polishing the Image 185 12.1 Antialiasing ........................................ 185 12.2 Color Limitations ..................................... 187 12.3 File Formats ........................................ 188 12.4 Preparing for print .................................... 189 12.5 Workflow .......................................... 193 ii 13 APPENDIX: Code 195 13.1 Python Setup ....................................... 195 13.2 Color Conversions .................................... 196 13.3 Interactive Jupyter Fractals .............................. 199 14 APPENDIX: About this Book 201 14.1 Writing a book in Jupyter ................................ 201 14.2 About the Author ..................................... 203 Bibliography 205 iii iv CHAPTER ONE INTRODUCTION When I was in college, I decided I should learn some Python programming. Most engineering students at the time used MATLAB for programming and data analysis work, but because of licensing issues, MATLAB was only available in the university’s compter labs, and I was too cheap to buy the student version. One cold weekend, I really wasn’t keen on trekking off through a foot of snow to the computer lab just to finish some data analysis I was working on, but I kept hearing good things about the Python language. Rather than venturing out in the cold, I stayed in my cozy bedroom office and decided to see what Python could do. Since most of my research work involved visualizing measurement data with different kinds of plots, I decided the best way to learn how to use Python for data and plotting was to figure out how to use it to make fractals. After installing Python and a few libraries on my laptop, it just took an hour or two to learn enough code to make my first Mandelbrot set, one of the original fractal images first discovered, show up on the screen. Amazed at how easyitwas to do things in Python, I quickly picked it up and used it for all my research data processing needs. Like almost every student, I found plenty of excuses to procrastinate on school work and do other things that weren’t about getting closer to graduating. Before long I was experimenting with other fractal variations and eventually set up my first website, on the university server, with some of the images I created. While a lot of computer programmers experiment with making fractals and like to use fractal computations to run benchmark tests of their code and hardware, the artistic side of fractal design is often neglected. Search the internet for fractals and, while there are many beautiful ones out there, you’ll find even more that look like plots from a journal paper - maybe mathe- matically correct but having no regard for composition, color, or aesthetics. This book aims to bridge the gaps between the math, the programming, and the design of algorithmically- based art. The artist needs aspects of all three to make the best images. We won’t go deeply into any one of these three aspects, but hopefully I will provide enough background on each of them to make some interesting images that can be considered pieces of art. The other goal of this book is to show how this style of art can be made from its roots as mathematical equations. There are many canned software programs, both stand-alone ap- plications and programming libraries, that make it easy to create fractals, but they become magic black-box machines where the user punches in a few random numbers and out comes an image. When I have used these, I was left unsatisfied with having no understanding of what really was going on behind the scenes. What’s the fun in making art based on math when we don’t actually understand the math? This book avoids any pre-compiled libraries and gets down to the basics of the equations themselves. By implementing the code from the ground up, we will know exactly what is going on and how to adjust it to do what we want. Prerequisites for this book include a basic understanding of math, including algebra and a lit- tle trigonometry. Some of the art is based on simple physics, such as the relationship between 1 Mathematical Artwork in Python distance, velocity, and acceleration, but both the math and physics should be understandable at a high-school level. Familiarity with the Python programming language is also assumed, although enough background and examples are given that this book could also be a good com- panion for advancing the reader’s Python skills, just as the first