Programming 101 the how and why of programming revealed using the processing programming language

332 98 0
Programming 101  the how and why of programming revealed using the processing programming language

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming 101 The How and Why of Programming Revealed Using the Processing Programming Language — Jeanine Meyer Programming 101 The How and Why of Programming Revealed Using the Processing Programming Language Jeanine Meyer Programming 101 Jeanine Meyer Mt Kisco, New York, USA ISBN-13 (pbk): 978-1-4842-3696-3 https://doi.org/10.1007/978-1-4842-3697-0 ISBN-13 (electronic): 978-1-4842-3697-0 Library of Congress Control Number: 2018946534 Copyright © 2018 by Jeanine Meyer This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Louise Corrigan Development Editor: James Markham Coordinating Editor: Nancy Chen Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/ rights-permissions Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484236963 For more detailed information, please visit http://www.apress.com/source-code Printed on acid-free paper To my family, who inspire and teach me Table of Contents About the Author��������������������������������������������������������������������������������������������������� xiii About the Technical Reviewers�������������������������������������������������������������������������������xv Acknowledgments�������������������������������������������������������������������������������������������������xvii Introduction������������������������������������������������������������������������������������������������������������xix Chapter 1: Basics����������������������������������������������������������������������������������������������������� Programming Concepts���������������������������������������������������������������������������������������������������������������� Programming Languages and Natural Languages������������������������������������������������������������������ Values and Variables��������������������������������������������������������������������������������������������������������������� Functions��������������������������������������������������������������������������������������������������������������������������������� Specifying Positions and Angles��������������������������������������������������������������������������������������������� Colors�������������������������������������������������������������������������������������������������������������������������������������� Development Environment������������������������������������������������������������������������������������������������������ Role of Planning���������������������������������������������������������������������������������������������������������������������� Under the Covers�������������������������������������������������������������������������������������������������������������������������� Processing Programming Features����������������������������������������������������������������������������������������������� Processing Development Environment������������������������������������������������������������������������������������ Functions������������������������������������������������������������������������������������������������������������������������������� 10 Angles������������������������������������������������������������������������������������������������������������������������������������ 11 Implementing Hello, World���������������������������������������������������������������������������������������������������������� 13 Implementing the Daddy Logo���������������������������������������������������������������������������������������������������� 21 Planning��������������������������������������������������������������������������������������������������������������������������������� 21 Daddy Logo Program������������������������������������������������������������������������������������������������������������� 24 Things to Look Up����������������������������������������������������������������������������������������������������������������������� 27 How to Make This Your Own������������������������������������������������������������������������������������������������������� 27 What You Learned����������������������������������������������������������������������������������������������������������������������� 28 What’s Next��������������������������������������������������������������������������������������������������������������������������������� 29 v Table of Contents Chapter 2: Interactions������������������������������������������������������������������������������������������� 31 Programming Concepts�������������������������������������������������������������������������������������������������������������� 33 Events������������������������������������������������������������������������������������������������������������������������������������ 33 Randomness�������������������������������������������������������������������������������������������������������������������������� 34 Displaying Images from Files������������������������������������������������������������������������������������������������ 34 Calculations, Including Built-in Functions����������������������������������������������������������������������������� 34 Looping���������������������������������������������������������������������������������������������������������������������������������� 35 Processing Programming Features��������������������������������������������������������������������������������������������� 35 Under the Covers������������������������������������������������������������������������������������������������������������������������ 39 Polygon Sketch Operation Overview������������������������������������������������������������������������������������������� 40 Implementing the Polygon Sketch���������������������������������������������������������������������������������������������� 42 Planning��������������������������������������������������������������������������������������������������������������������������������� 42 Polygon Sketch Program������������������������������������������������������������������������������������������������������� 46 Coin Toss Sketch Operation Overview����������������������������������������������������������������������������������������� 48 Implementing the Coin Toss Sketch�������������������������������������������������������������������������������������������� 50 Planning��������������������������������������������������������������������������������������������������������������������������������� 50 Things to Look Up����������������������������������������������������������������������������������������������������������������������� 53 How to Make This Your Own������������������������������������������������������������������������������������������������������� 54 What You Learned����������������������������������������������������������������������������������������������������������������������� 55 What’s Next��������������������������������������������������������������������������������������������������������������������������������� 55 Chapter 3: Animation Using Arrays and Parallel Structures����������������������������������� 57 More on the Sketches����������������������������������������������������������������������������������������������������������������� 57 Programming Concepts�������������������������������������������������������������������������������������������������������������� 63 Animation������������������������������������������������������������������������������������������������������������������������������ 63 Logical Operations����������������������������������������������������������������������������������������������������������������� 63 Arrays������������������������������������������������������������������������������������������������������������������������������������ 64 Parallel Structures����������������������������������������������������������������������������������������������������������������� 64 Compound Statements���������������������������������������������������������������������������������������������������������� 64 Pseudo-Random Processing������������������������������������������������������������������������������������������������� 64 Processing Programming Features��������������������������������������������������������������������������������������������� 65 vi Table of Contents Implementing a Bouncing Ball���������������������������������������������������������������������������������������������������� 69 Planning��������������������������������������������������������������������������������������������������������������������������������� 69 Program��������������������������������������������������������������������������������������������������������������������������������� 70 Implementing a Set of Three Bouncing Balls������������������������������������������������������������������������������ 71 Planning��������������������������������������������������������������������������������������������������������������������������������� 72 Program��������������������������������������������������������������������������������������������������������������������������������� 72 Implementing Pentagon Bouncing���������������������������������������������������������������������������������������������� 74 Planning��������������������������������������������������������������������������������������������������������������������������������� 74 Implementing Bouncing Polygons����������������������������������������������������������������������������������������������� 76 Planning��������������������������������������������������������������������������������������������������������������������������������� 76 Program��������������������������������������������������������������������������������������������������������������������������������� 77 Under the Covers������������������������������������������������������������������������������������������������������������������������ 79 Things to Look Up����������������������������������������������������������������������������������������������������������������������� 80 How to Make This Your Own������������������������������������������������������������������������������������������������������� 80 What You Learned����������������������������������������������������������������������������������������������������������������������� 81 What’s Next��������������������������������������������������������������������������������������������������������������������������������� 81 Chapter 4: Classes�������������������������������������������������������������������������������������������������� 83 Programming Concepts�������������������������������������������������������������������������������������������������������������� 83 Classes���������������������������������������������������������������������������������������������������������������������������������������� 83 Phases of Operations������������������������������������������������������������������������������������������������������������� 84 Tolerance or Margin��������������������������������������������������������������������������������������������������������������� 85 Processing Programming Features��������������������������������������������������������������������������������������������� 85 Classes���������������������������������������������������������������������������������������������������������������������������������� 85 Dynamic Arrays��������������������������������������������������������������������������������������������������������������������� 86 Tolerance and OK So Far Coding������������������������������������������������������������������������������������������� 88 Bouncing Objects Overview�������������������������������������������������������������������������������������������������������� 88 Implementing the Bouncing Objects������������������������������������������������������������������������������������������� 90 Planning��������������������������������������������������������������������������������������������������������������������������������� 90 Program��������������������������������������������������������������������������������������������������������������������������������� 92 Make Path and Travel Path Overview������������������������������������������������������������������������������������������ 96 vii Table of Contents Implementing the Make Path and Travel Path�������������������������������������������������������������������������� 100 Planning������������������������������������������������������������������������������������������������������������������������������� 100 Program������������������������������������������������������������������������������������������������������������������������������� 102 Jigsaw Overview����������������������������������������������������������������������������������������������������������������������� 105 Implementing the Jigsaw��������������������������������������������������������������������������������������������������������� 107 Planning������������������������������������������������������������������������������������������������������������������������������� 108 Program������������������������������������������������������������������������������������������������������������������������������� 109 Under the Covers���������������������������������������������������������������������������������������������������������������������� 120 Things to Look Up��������������������������������������������������������������������������������������������������������������������� 121 How to Make This Your Own����������������������������������������������������������������������������������������������������� 122 What You Learned��������������������������������������������������������������������������������������������������������������������� 122 What’s Next������������������������������������������������������������������������������������������������������������������������������� 122 Chapter 5: More Interactions�������������������������������������������������������������������������������� 123 More on the Sketches��������������������������������������������������������������������������������������������������������������� 123 Programming Concepts������������������������������������������������������������������������������������������������������������ 123 Ballistic Motion�������������������������������������������������������������������������������������������������������������������� 124 Character (char) Data Type vs String Data Type����������������������������������������������������������������� 125 Use of Files�������������������������������������������������������������������������������������������������������������������������� 125 Case Statement������������������������������������������������������������������������������������������������������������������� 125 Elapsed Time����������������������������������������������������������������������������������������������������������������������� 125 Regular Expressions������������������������������������������������������������������������������������������������������������ 125 Processing Programming Features������������������������������������������������������������������������������������������� 126 The char Data Type�������������������������������������������������������������������������������������������������������������� 126 The keyPressed Function, key, and keyCode����������������������������������������������������������������������� 126 Table Files���������������������������������������������������������������������������������������������������������������������������� 127 The Case Statement������������������������������������������������������������������������������������������������������������ 127 The millis and Other Time Functions����������������������������������������������������������������������������������� 129 Under the Covers���������������������������������������������������������������������������������������������������������������������� 130 Slingshot Operation Overview��������������������������������������������������������������������������������������������������� 132 viii Table of Contents Implementing the Slingshot Sketch������������������������������������������������������������������������������������������ 133 Planning������������������������������������������������������������������������������������������������������������������������������� 133 Programming the Slingshot Sketch������������������������������������������������������������������������������������� 136 Snake Operation Overview�������������������������������������������������������������������������������������������������������� 145 Implementing the Snake Sketch����������������������������������������������������������������������������������������������� 146 Planning������������������������������������������������������������������������������������������������������������������������������� 147 Programming the Snake Sketch������������������������������������������������������������������������������������������ 148 Image Test Operation Overview������������������������������������������������������������������������������������������� 156 Implementing the Image Test���������������������������������������������������������������������������������������������� 159 Things to Look Up��������������������������������������������������������������������������������������������������������������������� 164 How to Make This Your Own����������������������������������������������������������������������������������������������������� 164 What You Learned��������������������������������������������������������������������������������������������������������������������� 165 What’s Next������������������������������������������������������������������������������������������������������������������������� 165 Chapter 6: Images, Graphics, and  Building on Prior Work���������������������������������� 167 More on the Sketches��������������������������������������������������������������������������������������������������������������� 167 Programming Concepts������������������������������������������������������������������������������������������������������������ 168 Error Handling��������������������������������������������������������������������������������������������������������������������� 168 Images as Arrays of Pixels�������������������������������������������������������������������������������������������������� 168 Transformations������������������������������������������������������������������������������������������������������������������� 169 Processing Programming Features������������������������������������������������������������������������������������������� 169 Getting a File from the Web������������������������������������������������������������������������������������������������� 169 Pixel Processing������������������������������������������������������������������������������������������������������������������ 170 The beginShape and endShape Vertex Functions��������������������������������������������������������������� 171 Transformations������������������������������������������������������������������������������������������������������������������� 171 Under the Covers���������������������������������������������������������������������������������������������������������������������� 172 Image to Grayscale Operation Overview����������������������������������������������������������������������������������� 172 Implementing the Image to Grayscale�������������������������������������������������������������������������������������� 180 Planning������������������������������������������������������������������������������������������������������������������������������� 180 Programming the Image to Grayscale��������������������������������������������������������������������������������� 181 Origami Flower Operation Overview����������������������������������������������������������������������������������������� 186 ix Table of Contents Implementing the Origami Flower Sketch�������������������������������������������������������������������������������� 187 Planning������������������������������������������������������������������������������������������������������������������������������� 187 Programming the Origami Flower��������������������������������������������������������������������������������������� 189 Things to Look Up��������������������������������������������������������������������������������������������������������������������� 193 How to Make This Your Own����������������������������������������������������������������������������������������������������� 193 What You Learned��������������������������������������������������������������������������������������������������������������������� 194 What’s Next������������������������������������������������������������������������������������������������������������������������������� 194 Chapter 7: Using Files for Making a Holiday Card������������������������������������������������ 195 Programming Concepts������������������������������������������������������������������������������������������������������������ 196 Files������������������������������������������������������������������������������������������������������������������������������������� 196 Libraries������������������������������������������������������������������������������������������������������������������������������ 197 Fonts������������������������������������������������������������������������������������������������������������������������������������ 197 Callbacks����������������������������������������������������������������������������������������������������������������������������� 198 Feedback to Users��������������������������������������������������������������������������������������������������������������� 198 Processing Programming Features������������������������������������������������������������������������������������������� 198 Use of the Sound Library����������������������������������������������������������������������������������������������������� 198 Making and Saving an Image of the Current Window��������������������������������������������������������� 199 Use of Java Input/Output Library����������������������������������������������������������������������������������������� 200 Subclasses�������������������������������������������������������������������������������������������������������������������������� 201 Show Fonts Sketch Operation Overview����������������������������������������������������������������������������������� 202 Implementing the Show Fonts Sketch�������������������������������������������������������������������������������������� 203 Programming the Show Fonts Sketch��������������������������������������������������������������������������������� 204 Make Card Sketch Operation Overview������������������������������������������������������������������������������������ 205 Implementing the Make Card Sketch���������������������������������������������������������������������������������������� 210 Planning������������������������������������������������������������������������������������������������������������������������������� 210 Programming the Make Card Sketch����������������������������������������������������������������������������������� 212 Under the Covers���������������������������������������������������������������������������������������������������������������������� 218 Things to Look Up��������������������������������������������������������������������������������������������������������������������� 219 How to Make This Your Own����������������������������������������������������������������������������������������������������� 219 What You Learned��������������������������������������������������������������������������������������������������������������������� 219 What’s Next������������������������������������������������������������������������������������������������������������������������������� 220 x Appendix A Publishing on the Web Figure A-2 is one possible result of clicking Figure A-2.  Fearless girls 304 Appendix A Publishing on the Web Figure A-3 shows the other possibility Figure A-3.  Raging bull The second example features a 3D helix and provides the user a way to rotate the helix The opening screen is shown in Figure A-4 Figure A-4.  Rainbow helix 305 Appendix A Publishing on the Web The coding for the rotation is done the same way as demonstrated in the rotating cube example of Chapter 10 If the user drags the mouse horizontally, the helix will appear to spin I have prepared and uploaded to my web site the ProcessingJS files for these two examples and others You can visit ­http://faculty.purchase.edu/jeanine.meyer/ processing/ to view other examples Note that you can view the HTML source through the browser Some of these examples will be familiar to you from the chapters I mplementation The approach is to use a file produced by the ProcessingJS community to translate the PDE program into JavaScript We, the Processing programmers, prepare an HTML file that references the PDE program along with a file downloaded from the ProcessingJS site Our PDE code is essentially converted into JavaScript, making use of functions in the processing.min.js file You need to acquire a web site and have a File Transfer Protocol (FTP) program such as Filezilla to upload the files The following four tasks can be performed in any order I assume you will use one specific folder on the web site Download the file processing.min.js from the ProcessingJS.org web site and upload it to the folder on the web site in which you are going to store your work You not have to repeat this for subsequent examples, assuming you use the same folder If you want to use more than one folder, the links must obey the HTML rules for relative links Prepare your sketch in the usual way and upload the PDE file to your web site in the same folder Upload all the images from the data subfolder for your sketch to your web site; not upload the data folder itself See later for the fix necessary for multiple images Prepare the HTML file To this, you need to use a text editor, such as Sublime, TextEdit, TextWrangler, or NotePad You not use the Processing PDE and you not use a word processing program The HTML file will be the file that you tell your public 306 Appendix A Publishing on the Web about to go to your sketch They enter the full web address (URL) in a browser or click on links you have set up as hyperlinks from another HTML file The basic format—change what is in italics—is as follows: what you want to appear in the tab

Ngày đăng: 04/03/2019, 11:47

Từ khóa liên quan

Mục lục

  • Table of Contents

  • About the Author

  • About the Technical Reviewers

  • Acknowledgments

  • Introduction

  • Chapter 1: Basics

    • Programming Concepts

      • Programming Languages and Natural Languages

      • Values and Variables

      • Functions

      • Specifying Positions and Angles

      • Colors

      • Development Environment

      • Role of Planning

      • Under the Covers

      • Processing Programming Features

        • Processing Development Environment

        • Functions

        • Angles

        • Implementing Hello, World

        • Implementing the Daddy Logo

          • Planning

          • Daddy Logo Program

          • Things to Look Up

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan