06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

When using the OPENGL mode, you must also import the OPENGL library.<br />

14.3<br />

Vertex Shapes<br />

Translation and Rotation (in 3D!) 233<br />

Th is can be done by selecting the SKETCH ➝ IMPORT LIBRARY menu option or by manually<br />

adding the following line of code <strong>to</strong> the <strong>to</strong>p of your sketch (see Chapter 12 for a detailed explanation<br />

on libraries):<br />

import processing.opengl.*;<br />

Exercise 14-2: Run any <strong>Processing</strong> sketch in P3D, then switch <strong>to</strong> OPENGL. Notice any<br />

diff erence?<br />

Up until now, our ability <strong>to</strong> draw <strong>to</strong> the screen has been limited <strong>to</strong> a small list of primitive twodimensional<br />

shapes: rectangles, ellipses, triangles, lines, and points. For some projects, however, creating<br />

your own cus<strong>to</strong>m shapes is desirable. Th is can be done through the use of the functions beginShape( ) ,<br />

endShape( ) , and vertex( ) .<br />

Consider a rectangle. A rectangle in <strong>Processing</strong> is defi ned as a reference point, as well as a width and height.<br />

rect(50,50,100,100);<br />

But we could also consider a rectangle <strong>to</strong> be a polygon (a closed shape bounded by line segments)<br />

made up of four points. Th e points of a polygon are called vertices (plural) or vertex (singular). Th e<br />

following code draws exactly the same shape as the rect( ) function by setting the vertices of the rectangle<br />

individually. See Figure 14.5 .<br />

beginShape();<br />

vertex(50,50);<br />

vertex(150,50);<br />

vertex(150,150);<br />

vertex(50,150);<br />

endShape(CLOSE);<br />

beginShape( ) indicates that we are going <strong>to</strong> create a cus<strong>to</strong>m shape made up of some fi g. 14.5<br />

number of vertex points: a single polygon. vertex( ) specifi es the points for each vertex in<br />

the polygon and endShape( ) indicates that we are fi nished adding vertices. Th e argument “ CLOSE ” inside<br />

of endShape(CLOSE) declares that the shape should be closed, that is, that the last vertex point should<br />

connect <strong>to</strong> the fi rst.<br />

Th e nice thing about using a cus<strong>to</strong>m shape over a simple rectangle is fl exibility. For example, the sides are<br />

not required <strong>to</strong> be perpendicular. See Figure 14.6 .<br />

stroke(0);<br />

fill(175);<br />

beginShape();<br />

vertex(50,50);<br />

vertex(150,25);<br />

vertex(150,175);<br />

vertex(25,150);<br />

endShape(CLOSE);<br />

fi g. 14.6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!