Beispiel 1
Den Code unseres Einstiegs-Beispiels können wir mit Hilfe von Methoden sauberer strukturieren:
void setup() { size(640, 480); background(#AACCEE); sonne(); rasen(); haus(); wolken(); } void sonne() { fill(#FFFF00); strokeWeight(2); stroke(#AAAA00); circle(0, 0, 250); sonnenstrahlen(5); } void sonnenstrahlen(int anzahlStrahlen) { noFill(); strokeWeight(5); stroke(#AAAA00); for (int i = 0; i < anzahlStrahlen; i++) { line(150, 0, 250, 0); rotate((PI / 2) / (anzahlStrahlen - 1)); } resetMatrix(); } void haus() { mauern(); tuer(); dach(); } void mauern() { fill(#FFFFFF); stroke(#AAAAAA); strokeWeight(2); rect(260, 300, 160, 160); } void tuer() { fill(#675400); stroke(#483B00); strokeWeight(2); rect(340, 400, 30, 60); } void dach() { fill(#FF0000); stroke(#770000); strokeWeight(2); triangle(260, 300, 420, 300, 340, 250); } void rasen() { fill(#00AA00); stroke(#00FF00); strokeWeight(2); rect(0, 400, 640, 80); } void wolken() { PShape wolke = loadShape("cloud01.svg"); shape(wolke, 400, 10, 200, 100); shape(wolke, 350, 70, 150, 100); shape(wolke, 450, 90, 150, 100); }
Beispiel 2
Im Beispiel unten werden die x- und y-Koordinaten von Linien abhängig von der Zeit mit Hilfe von den vier Methoden float x1(float t)
, float y1(float t)
, float x2(float t)
und float y2(float t)
berechnet.
float t; final int num_lines = 10; void setup() { background(20); size(500, 500); } void draw() { stroke(255); strokeWeight(5); translate(width / 2, height / 2); background(20); for (int i = 0; i < num_lines; i++) { line(x1(t + i), y1(t + i), x2(t + i), y2(t + i)); } t += 0.5; } float x1(float t) { return sin(t / 10) * 100 + cos(t / 20) * 50; } float y1(float t) { return cos(t / 10) * 200 + cos(t / 15) * 20; } float x2(float t) { return sin(t / 10) * 30 + cos(t / 20) * 40; } float y2(float t) { return cos(t / 10) * 150 - cos(t / 25) * 30; }