8.581
Bearbeitungen
Jneug (Diskussion | Beiträge) |
Jneug (Diskussion | Beiträge) |
||
Zeile 208: | Zeile 208: | ||
<syntaxhighlight lang="java" line="1"> | <syntaxhighlight lang="java" line="1"> | ||
// Position/Größe des Rechtecks | // Position/Größe des Rechtecks | ||
float x = 130; | float x = 130.0; | ||
float y = 100; | float y = 100.0; | ||
float w = 60; | float w = 60.0; | ||
float h = 40; | float h = 40.0; | ||
void setup() { | void setup() { | ||
Zeile 229: | Zeile 229: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Eine Vorgehensweise wäre, zwei lokale Variablen <code>x2</code> und <code>y2</code> anzulegen, in die | Eine Vorgehensweise wäre, zwei lokale Variablen <code>x2</code> und <code>y2</code> anzulegen, in die Du das Ergebnis der Umrechnung von <code>mouseX</code> und <code>mouseY</code> in das System des Rechtecks vornehmen. Es handelt sich um eine klassische Anwendung des Dreisatz'. | ||
'''Achtung''': Du musst hier mit Dezimalzahlen (Datentyp <code>float</code>) arbeiten. Damit der Computer das weiß, muss du die Zahlen mit einer Nachkommastelle schreiben, zum Beispiel <code>200.0</code> statt <code>200</code>. Das problem wird deutlich, wenn du folgendes kleines Programm ausprobierst: | |||
<syntaxhighlight lang="java" line="1"> | |||
void draw() { | |||
print("Ohne Kommazahl: "); | |||
println(mouseX / 100); | |||
print("Mit Kommazahl: "); | |||
println(mouseX / 100.0); | |||
} | |||
</syntaxhighlight> | |||
{{Zusatzaufgabe:End}} | {{Zusatzaufgabe:End}} | ||
Zeile 252: | Zeile 263: | ||
ellipse(mouseX, mouseY, 10, 10); // Mauszeiger | ellipse(mouseX, mouseY, 10, 10); // Mauszeiger | ||
float | float x2 = (mouseX/200.0)*w + x; | ||
float | float y2 = (mouseY/150.0)*h + y; | ||
ellipse( | ellipse(x2, y2, 2, 2); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Lösung:End}} | {{Lösung:End}} |
Bearbeitungen