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.

322 <strong>Learning</strong> <strong>Processing</strong><br />

Example 17-8: Characters along a curve<br />

// The message <strong>to</strong> be displayed<br />

String message = " text along a curve " ;<br />

PFont f;<br />

// The radius of a circle<br />

float r = 100;<br />

void setup() {<br />

size(320,320);<br />

f = createFont( " Georgia ",40,true);<br />

textFont(f);<br />

textAlign(CENTER); The text must be centered!<br />

smooth();<br />

}<br />

void draw() {<br />

background(255);<br />

// Start in the center and draw the circle<br />

translate(width / 2, height / 2);<br />

noFill();<br />

stroke(0);<br />

ellipse(0, 0, r*2, r*2);<br />

// We must keep track of our position along the curve<br />

float arclength = 0;<br />

// For every box<br />

for (int i = 0; i < message.length(); i + + ) {<br />

// The character and its width<br />

char currentChar = message.charAt(i);<br />

float w = textWidth(currentChar);<br />

fi g. 17.13<br />

Instead of a constant width, we<br />

check the width of each character.<br />

// Each box is centered so we move half the width<br />

arclength + = w/2;<br />

// Angle in radians is the arclength divided by the radius<br />

// Starting on the left side of the circle by adding PI<br />

float theta = PI + arclength / r;<br />

Polar <strong>to</strong> Cartesian conversion<br />

pushMatrix();<br />

allows us <strong>to</strong> fi nd the point along<br />

// Polar <strong>to</strong> cartesian coordinate conversion the curve. See Chapter 13 for a<br />

translate(r*cos(theta), r*sin(theta));<br />

review of this concept.<br />

// Rotate the box<br />

rotate(theta + PI/2); // rotation is offset by 90 degrees<br />

// Display the character<br />

fill(0);<br />

text(currentChar,0,0);<br />

popMatrix();<br />

// Move halfway again<br />

arclength + = w/2;<br />

}<br />

}

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

Saved successfully!

Ooh no, something went wrong!