Learning Processing, Second Edition, is a friendly start-up guide to Processing, a free, open-source alternative to expensive software and daunting programming languages. Requiring no previous experience, this book is for the true programming beginner. It teaches the basic building blocks of programming needed to create cutting-edge graphics applications including interactive art, live video processing, and data visualization. Step-by-step examples, thorough explanations, hands-on exercises, and sample code, supports your learning curve.
A unique lab-style manual, the book gives graphic and web designers, artists, and illustrators of all stripes a jumpstart on working with the Processing programming environment by providing instruction on the basic principles of the language, followed by careful explanations of select advanced techniques. The book has been developed with a supportive learning experience at its core. From algorithms and data mining to rendering and debugging, it teaches object-oriented programming from the ground up within the fascinating context of interactive visual media.
This book is ideal for graphic designers and visual artists without programming background who want to learn programming. It will also appeal to students taking college and graduate courses in interactive media or visual computing, and for self-study.
- A friendly start-up guide to Processing,a free, open-source alternative to expensive software and daunting programming languages
- No previous experience requiredthis book is for the true programming beginner!
- Step-by-step examples, thorough explanations, hands-on exercises, andsample codesupports your learning curve
|Series:||Morgan Kaufmann Series in Computer Graphics Series|
|Edition description:||New Edition|
|Product dimensions:||6.00(w) x 8.90(h) x 0.70(d)|
Read an Excerpt
Learning ProcessingA Beginner's Guide to Programming Images, Animation, and Interaction
By Daniel Shiffman
MORGAN KAUFMANN PUBLISHERSCopyright © 2008 Elsevier Inc.
All right reserved.
"A journey of a thousand miles begins with a single step." —Lao-tzu In this chapter:
– Specifying pixel coordinates.
– Basic shapes: point, line, rectangle, ellipse.
– Color: grayscale, "RGB."
– Color transparency.
Note that we are not doing any programming yet in this chapter! We are just dipping our feet in the water and getting comfortable with the idea of creating onscreen graphics with text-based commands, that is, "code"!
1.1 Graph Paper
This book will teach you how to program in the context of computational media, and it will use the development environment Processing (http://www.processing.org) as the basis for all discussion and examples. But before any of this becomes relevant or interesting, we must first channel our eighth grade selves, pull out a piece of graph paper, and draw a line. The shortest distance between two points is a good old fashioned line, and this is where we begin, with two points on that graph paper.
Figure 1.1 shows a line between point A (1,0) and point B (4,5). If you wanted to direct a friend of yours to draw that same line, you would give them a shout and say "draw a line from the point one-zero to the point four-five, please." Well, for the moment, imagine your friend was a computer and you wanted to instruct this digital pal to display that same line on its screen.The same command applies (only this time you can skip the pleasantries and you will be required to employ a precise formatting). Here, the instruction will look like this:
Congratulations, you have written your first line of computer code! We will get to the precise formatting of the above later, but for now, even without knowing too much, it should make a fair amount of sense. We are providing a command (which we will refer to as a "function") for the machine to follow entitled "line." In addition, we are specifying some arguments for how that line should be drawn, from point A (0,1) to point B (4,5). If you think of that line of code as a sentence, the function is a verb and the arguments are the objects of the sentence. The code sentence also ends with a semicolon instead of a period.
The key here is to realize that the computer screen is nothing more than a fancier piece of graph paper. Each pixel of the screen is a coordinate—two numbers, an "x" (horizontal) and a "y" (vertical)—that determine the location of a point in space. And it is our job to specify what shapes and colors should appear at these pixel coordinates.
Nevertheless, there is a catch here. The graph paper from eighth grade ("Cartesian coordinate system") placed (0,0) in the center with the y-axis pointing up and the x-axis pointing to the right (in the positive direction, negative down and to the left).The coordinate system for pixels in a computer window, however, is reversed along the y-axis. (0,0) can be found at the top left with the positive direction to the right horizontally and down vertically. See Figure 1.3.
1.2 Simple Shapes
The vast majority of the programming examples in this book will be visual in nature. You may ultimately learn to develop interactive games, algorithmic art pieces, animated logo designs, and (insert your own category here) with Processing, but at its core, each visual program will involve setting pixels. The simplest way to get started in understanding how this works is to learn to draw primitive shapes. This is not unlike how we learn to draw in elementary school, only here we do so with code instead of crayons.
Let's start with the four primitive shapes shown in Figure 1.4.
For each shape, we will ask ourselves what information is required to specify the location and size (and later color) of that shape and learn how Processing expects to receive that information. In each of the diagrams below (Figures 1.5 through 1.11), assume a window with a width of 10 pixels and height of 10 pixels. This isn't particularly realistic since when we really start coding we will most likely work with much larger windows (10 × 10 pixels is barely a few millimeters of screen space). Nevertheless for demonstration purposes, it is nice to work with smaller numbers in order to present the pixels as they might appear on graph paper (for now) to better illustrate the inner workings of each line of code.
A point is the easiest of the shapes and a good place to start. To draw a point, we only need an x and y coordinate as shown in Figure 1.5. A line isn't terribly difficult either. A line requires two points, as shown in Figure 1.6.
Once we arrive at drawing a rectangle, things become a bit more complicated. In Processing, a rectangle is specified by the coordinate for the top left corner of the rectangle, as well as its width and height (see Figure 1.7).
However, a second way to draw a rectangle involves specifying the centerpoint, along with width and height as shown in Figure 1.8. If we prefer this method, we first indicate that we want to use the "CENTER" mode before the instruction for the rectangle itself. Note that Processing is case-sensitive. Incidentally, the default mode is "CORNER," which is how we began as illustrated in Figure 1.7.
Finally, we can also draw a rectangle with two points (the top left corner and the bottom right corner). The mode here is "CORNERS" (see Figure 1.9).
Once we have become comfortable with the concept of drawing a rectangle, an ellipse is a snap. In fact, it is identical to rect() with the difference being that an ellipse is drawn where the bounding box (as shown in Figure 1.11) of the rectangle would be. The default mode for ellipse() is "CENTER", rather than "CORNER" as with rect(). See Figure 1.10.
It is important to acknowledge that in Figure 1.10, the ellipses do not look particularly circular. Processing has a built-in methodology for selecting which pixels should be used to create a circular shape. Zoomed in like this, we get a bunch of squares in a circle-like pattern, but zoomed out on a computer screen, we get a nice round ellipse. Later, we will see that Processing gives us the power to develop our own algorithms for coloring in individual pixels (in fact, we can already imagine how we might do this using "point" over and over again), but for now, we are content with allowing the "ellipse" statement to do the hard work.
Certainly, point, line, ellipse, and rectangle are not the only shapes available in the Processing library of functions. In Chapter 2, we will see how the Processing reference provides us with a full list of available drawing functions along with documentation of the required arguments, sample syntax, and imagery. For now, as an exercise, you might try to imagine what arguments are required for some other shapes (Figure 1.12):
triangle() arc() quad() curve()
1.3 Grayscale Color
As we learned in Section 1.2, the primary building block for placing shapes onscreen is a pixel coordinate. You politely instructed the computer to draw a shape at a specific location with a specific size. Nevertheless, a fundamental element was missing—color.
In the digital world, precision is required. Saying "Hey, can you make that circle bluish-green?" will not do. Therefore, color is defined with a range of numbers. Let's start with the simplest case: black and white or grayscale. In grayscale terms, we have the following: 0 means black, 255 means white. In between, every other number—50, 87, 162, 209, and so on—is a shade of gray ranging from black to white. See Figure 1.13.
Understanding how this range works, we can now move to setting specific grayscale colors for the shapes we drew in Section 1.2. In Processing, every shape has a stroke() or a fill() or both. The stroke() is the outline of the shape, and the fill() is the interior of that shape. Lines and points can only have stroke(), for obvious reasons.
If we forget to specify a color, Processing will use black (0) for the stroke() and white (255) for the fill() by default. Note that we are now using more realistic numbers for the pixel locations, assuming a larger window of size 200 × 200 pixels. See Figure 1.14.
By adding the stroke() and fill() functions before the shape is drawn, we can set the color. It is much like instructing your friend to use a specific pen to draw on the graph paper. You would have to tell your friend before he or she starting drawing, not after.
There is also the function background(), which sets a background color for the window where shapes will be rendered.
stroke() or fill() can be eliminated with the noStroke() or noFill() functions.
Our instinct might be to say "stroke(0)" for no outline, however, it is important to remember that 0 is not "nothing", but rather denotes the color black. Also, remember not to eliminate both—with noStroke() and noFill(), nothing will appear!
If we draw two shapes at one time, Processing will always use the most recently specified stroke() and fill(), reading the code from top to bottom. See Figure 1.17 .
1.4 RGB Color
A nostalgic look back at graph paper helped us learn the fundamentals for pixel locations and size. Now that it is time to study the basics of digital color, we search for another childhood memory to get us started. Remember finger painting? By mixing three "primary" colors, any color could be generated. Swirling all colors together resulted in a muddy brown. The more paint you added, the darker it got.
Digital colors are also constructed by mixing three primary colors, but it works differently from paint. First, the primaries are different: red, green, and blue (i.e., "RGB" color). And with color on the screen, you are mixing light, not paint, so the mixing rules are different as well.
Red + green = yellow Red + blue = purple Green + blue = cyan (blue-green) Red + green + blue = white No colors = black
This assumes that the colors are all as bright as possible, but of course, you have a range of color available, so some red plus some green plus some blue equals gray, and a bit of red plus a bit of blue equals dark purple.
While this may take some getting used to, the more you program and experiment with RGB color, the more it will become instinctive, much like swirling colors with your fingers. And of course you can't say "Mix some red with a bit of blue," you have to provide an exact amount. As with grayscale, the individual color elements are expressed as ranges from 0 (none of that color) to 255 (as much as possible), and they are listed in the order R, G, and B. You will get the hang of RGB color mixing through experimentation, but next we will cover some code using some common colors.
Note that this book will only show you black and white versions of each Processing sketch, but everything is documented online in full color at http://www.learningprocessing.com with RGB color diagrams found specifically at: http://learningprocessing.com/color.
Excerpted from Learning Processing by Daniel Shiffman Copyright © 2008 by Elsevier Inc.. Excerpted by permission of MORGAN KAUFMANN PUBLISHERS. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Table of Contents
Lesson 1: The Beginning
Chapter 1: Pixels
Chapter 2: Processing
Chapter 3: Interaction
Lesson 2: Everything You Need to Know
Chapter 4: Variables
Chapter 5: Conditionals
Chapter 6: Loops
Lesson 3: Organization
Chapter 7: Functions
Chapter 8: Objects
Lesson 4: More of the Same
Chapter 9: Arrays
Lesson 5: Putting It All Together
Chapter 10: Algorithms
Chapter 11: Debugging
Chapter 12: Libraries
Lesson 6: The World Revolves Around You
Chapter 13: Mathematics
Chapter 14: Translation and Rotation (in 3D!)
Lesson 7: Pixels Under Microscope
Chapter 15: Images
Chapter 16: Video
Lesson 8: The Outside World
Chapter 17: Text
Chapter 18: Data Input
Chapter 19: Data Streams
Lesson 9: Making Noise
Chapter 20: Sound
Chapter 21: Exporting
Lesson 10: Beyond Processing
Chapter 22: Advanced Object-Oriented Programming
Chapter 23: Java
Appendix: Common Errors