Processing: An Introduction to Programming Processing: An Introduction to Programming by Jeffrey L. Nyhoff Larry R. Nyhoff CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2017 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper International Standard Book Number-13: 978-1-4822-5595-9 (Paperback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to pub- lish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copy- right.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑ in‑ Publication Data Names: Nyhoff, Jeffrey, author. | Nyhoff, Larry R., author. Title: Processing : an introduction to programming / Jeffrey L. Nyhoff, Larry R. Nyhoff. Description: Boca Raton : CRC Press, 2017. Identifiers: LCCN 2016041238 | ISBN 9781482255959 (pbk. : alk. paper) Subjects: LCSH: Processing (Computer program language) | Computer programming--Study and teaching. Classification: LCC QA76.73.P75 N94 2017 | DDC 005.1071--dc23 LC record available at https://lccn.loc.gov/2016041238 Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com To Sharlene, Rebecca, Megan, and SaraKate. Contents Foreword, xv Preface: Why We Wrote This Book and For Whom It Is Written , xvii Acknowledgments, xxi Introduction: Welcome to Computer Programming, xxiii Chapter 1 ▪ Basic Drawing in Processing 1 Starting a New Program 1 Saving a Program 2 Retrieving a Program 3 Entering Code into the Text Editor 4 Basic Drawing with Graphical Elements 8 Setting the “Canvas” Size: A Closer Look at the size() Function 8 Drawing Points: The point() Function 10 Drawing Line Segments: The line() Function 11 Drawing Rectangles: The rect() Function 13 Drawing Ellipses: The ellipse() Function 17 Drawing Triangles: The triangle() Function 20 Drawing Quadrilaterals: The quad() Function 22 Drawing Arcs: The arc() Function 26 Summary 29 The Processing Reference 31 More about Graphical Elements 31 Stacking Order 32 Changing Line Thickness: The strokeWeight() Function 33 Working with Color: RGB 35 Resetting the Canvas: The background() Function 37 vii viii ◾ Contents Changing the Fill Color: The fill() and noFill() Functions 38 Changing the Stroke Color: The stroke() and noStroke() Functions 41 Inline Comments 43 Grayscale 43 Transparency 45 Summary 46 Exercises 47 Chapter 2 ▪ Types, Expressions, and Variables 53 Values 53 Numeric Values 53 Integers: The int Type 54 Numbers with Decimal Points: The float Type 54 Arithmetic with int Values and float Values 54 int Arithmetic 55 Integer Division 56 Calculating the Remainder with the Modulo Operator: % 57 float Arithmetic 58 float Fractions 59 The Trouble with Fractions on Computers 60 Evaluating Expressions 63 Order of Operations 63 Using Parentheses 65 Variables 65 Predefined Variables: width and height 67 Benefits of Using Variables 72 Creating and Using Our Own Variables 73 Variable Names 74 Variable Types 75 Declaring a Variable 76 Assigning a Value to a Variable 77 Combining Declaration and Initialization 80 Reusing a Variable 80 Type Mismatches 83 Why Not Use Only the float Type? 87 Expressions in Assignment Statements 88 Contents ◾ ix Using a Variable on the Right-Hand Side of an Assignment Statement 91 Being Careful with Integer Division 95 Reassigning a New Value to a Variable 98 Constants 106 Predefined Constants 106 Defining Our Own Constants 108 Nonnumeric Types 110 Individual Characters: The char Type 110 Multiple Characters: The String Type 114 String Concatenation 116 Summary 118 Exercises 119 Chapter 3 ▪ More about Using processing’ s Built-In Functions 125 More about Console Output: The print() and println() Functions 125 Displaying Multiple Items to the Console 126 Graphical Text in Processing 127 The text() Function 128 The textsize() Function 128 The textAlign() Function 129 Matching the Type of an Argument to the Type of a Parameter 130 Two Kinds of Functions 132 void Functions 132 Functions That Return a Value 134 Determining a Function’ s Return Type Using Processing’ s Reference 137 Example: Calculating a Hypotenuse with the sqrt() Function 138 The pow() Function 140 Calculating the Area of a Square Using the pow() Function 141 Calculating the Hypotenuse with the pow() Function 144 The random() Function 145 The round() Function 146 Using the round() Function 147 More Conversion Functions: int() and float() 147 int() and random() Example: Choosing a Random Integer to Simulate Rolling a Die 148 int() and random() Example: Random Side Length for a Square 149