Mathematische Berechnung machen einen großen Teil der Programmierung aus. Processing liefert dir einige Funktionen & Konstanten mit, die gewisse Berechnungen für dich übernehmen. Hier findest du eine Auswahl der wichtigsten:
Konstanten
Pi
PI ist eine mathem. Konstante mit dem Wert 3.1415927.
Zwei Pi
TWO_PI ist eine mathem. Konstante mit dem Wert 6.2831855.
Halbes Pi
HALF_PI ist eine mathem. Konstante mit dem Wert 1.5707964.
Viertel Pi
QUARTER_PI ist eine mathem. Konstante mit dem Wert 0.7853982.
Beispiel:
float x = width/2;
float y = height/2;
float d = width * 0.8;
arc(x, y, d, d, 0, QUARTER_PI); // achtel Kreis
arc(x, y, d-20, d-20, 0, HALF_PI); // viertel Kreis
arc(x, y, d-40, d-40, 0, PI); // halber Kreis
arc(x, y, d-60, d-60, 0, TWO_PI); // ganzer Kreis
Exponentialrechnung
exp()
Liefert das Ergebnis der Eulerischen Zahl e hoch den Exponenten, der angegeben wird.
float v1 = exp(1.0);
println(v1); // Prints "2.7182817"
Syntax
exp(n);
Parameter
n: float Exponent
Rückgabetyp
float
pow()
Berechnet n (Zahl) hoch e (Exponent)
float a = pow( 1, 3); // a = 1*1*1 = 1
float b = pow( 3, 5); // b = 3*3*3*3*3 = 243
float c = pow( 3,-5); // c = 1 / 3*3*3*3*3 = .0041
float d = pow(-3, 5); // d = -3*-3*-3*-3*-3 = -243
Syntax
pow( n, e);
Parameter
n: float Basiszahl, e: float Exponent
Rückgabetyp
float
sqrt()
Berechnet die Quadratwurzel einer Zahl
float a = sqrt(6561); // a = 81
float b = sqrt(625); // b = 25
float c = sqrt(1); // c = 1
rect(0, 25, a, 10);
rect(0, 45, b, 10);
rect(0, 65, c, 10);
Syntax
sqrt(n);
Parameter
n: float Zahl, nicht negativ!
Rückgabetyp
float
Zahlen runden
round()
Berechnet die Ganzzahl, die am nächsten liegt
int rx = round(9.2); // rx = 9
int ry = round(9.5); // ry = 10
int rz = round(9.9); // rz = 10
Syntax
round(n)
Parameter
n: float Zahl, die gerundet werden soll
Rückgabetyp
int
ceil()
Berechnet die nächst größere Ganzzahl
float x = 8.22;
int a = ceil(x); // a = 9
Syntax
ceil(n)
Parameter
n: float Zahl, die gerundet werden soll
Rückgabetyp
int
floor()
Berechnet die nächst kleinere Ganzzahl
float x = 2.88;
int a = floor(x); // a = 2
Syntax
floor(n)
Parameter
n: float Zahl, die gerundet werden soll werden soll
Rückgabetyp
int
abs()
Berechnet den absoluten Wert einer Zahl. Dieser Wert ist immer positiv.
int a = abs(153); // a = 153
int b = abs(-15); // b = 15
float c = abs(12.234); // c = 12.234
float d = abs(-9.23); // d = 9.23
Syntax
abs(n)
Parameter
n: float Zahl, die gerundet werden soll
Rückgabetyp
int
Trigonometrie
sin()
Berechnet den Sinus-Wert eines Winkels. Nimmt Werte im Bogenmaß (0 bis 2*PI).
float a = 0.0;
float inc = TWO_PI/25.0;
for (int i = 0; i < 100; i=i+4) {
line(i, 50, i, 50 + sin(a) * 40.0);
a = a + inc;
}
Syntax
sin(angle)
Parameter
angle: float im Bogenmaß
Rückgabetyp
float
cos()
Berechnet den Cosinus-Wert eines Winkels. Nimmt Werte im Bogenmaß (0 bis 2*PI).
float a = 0.0;
float inc = TWO_PI/25.0;
for (int i = 0; i < 25; i++) {
line(i*4, 50, i*4, 50 + cos(a) * 40.0);
a = a + inc;
}
Syntax
cos(angle)
Parameter
angle: float im Bogenmaß
Rückgabetyp
float
tan()
Berechnet das Verhältnis des Sinus und des Cosinnus eines Winkels. Nimmt Werte im Bogenmaß (0 bis 2*PI).
float a = 0.0;
float inc = TWO_PI/50.0;
for (int i = 0; i < 100; i = i+2) {
line(i, 50, i, 50 + tan(a) * 2.0);
a = a + inc;
}
Syntax
tan(angle)
Parameter
angle: float im Bogenmaß
Rückgabetyp
float
Winkel
radians()
Konvertiert eine Gradzahl ins Bogenmaß. Gradzahl und Bogenmaß sind 2 verschiedene Arten dasselbe zu messen. Ein Kreis besteht 360° oder 2*PI im Bogenmaß.
float deg = 45.0;
float rad = radians(deg);
println(deg + " Grad sind " + rad + " im Bogenmaß");
Syntax
radians(degrees)
Parameter
degrees: float Gradzahl die in Bogenmaß konvertiert werden soll
Rückgabetyp
float
degrees()
Konvertiert eine Zahl im Bogenmaß in eine Gradzahl.
float rad = PI/4;
float deg = degrees(rad);
println(rad + " im Bogenmaß sind " + deg + " Grad");
Syntax
degrees(radians)
Parameter
radians: float Zahl im Bogenmaß die in Grad konvertiert werden soll
Rückgabetyp
float
Weitere hilfreiche Funktionen
random()
Generiert Zufallszahlen. Kann einen oder zwei Parameter annehmen. Wird nur high angegeben, werden Zahlen >= 0 und < high. Werden high und low angegeben, werden Zahlen >= low und < high generiert.
float grayscale = random(255);
background(grayscale);
float r = random(255);
float g = random(255);
float b = random(255);
background(r, g, b);
random(high);
random(low, high)
Parameter
high: float obere Grenze
low: float untere Grenze
Rückgabetyp
float
dist()
Berechnet den Abstand zwischen zwei Punkten
void draw() {
background(255);
line(0, 0, mouseX, mouseY);
float d = dist(0, 0, mouseX, mouseY);
fill(0);
text(d, mouseX, mouseY);
}
Syntax
dist(x1, y1, x2, y2)
Parameter
x1, y1, x2, y2: float Werte der Positionen
Rückgabetyp
float
atan2()
Berechnet den Winkel (im Bogenmaß) von einem Punkt zum Koordinatenursprung, gemessen von der positiven x-Achse. Werte werden als float von PI bis -PI zurückgegeben. Die Funktion atan2() wird oft verwendet, um die Geometrie an der Position des Cursors auszurichten.
void draw() {
background(204);
translate(width/2, height/2);
float a = atan2(mouseY-height/2, mouseX-width/2);
rotate(a);
rect(-30, -5, 60, 10);
}
Syntax
Hinweis: Die y-Koordinate des Punkts ist der erste Parameter, da die Berechnung der Tangente so strukturiert ist.
atan2(y, x)
Parameter
y,x: float Werte
Rückgabetyp
float
map()
Bildet eine Zahl von einem Bereich auf einen anderen ab.
size(200, 200);
float value = 25;
float m = map(value, 0, 100, 0, width);
ellipse(m, 200, 10, 10);
Syntax
map(value, start1, stop1, start2, stop2)
Parameter
value: aktueller Wert (float)
start1: untere Grenze des aktuellen Werts (float)
stop1: obere Grenze des aktuellen Werts (float)
start2: untere Grenze des Ziel-Werts (float)
stop2: obere Grenze des Ziel-Werts (float)
Rückgabetyp
float
Zusammenfassung
- Es gibt in Processing viele verschiedene vordefinierte mathematische Funktionen, die wir einfach verwenden können.
- Eine Übersicht übe alle mathematischen Funktionen von Processing findet ihr unter https://processing.org/reference/#math
- Ebenso gibt es vier mathematische Konstanten in Bezug auf PI.