Compare commits
22 Commits
Abgabe1
...
Abgabe3-Na
Author | SHA1 | Date | |
---|---|---|---|
95a4907dd2
|
|||
dfbdd6dc9d
|
|||
f156da4803
|
|||
cda144aa2a
|
|||
4bb1d6a36c
|
|||
01b2fb8989
|
|||
9925835a1e
|
|||
f24ad9bcaf
|
|||
cf4c1ad9d0
|
|||
8c67e157d5
|
|||
07ef38dab3 | |||
78d70f8a43
|
|||
9ce478b88e
|
|||
e52a1323c7
|
|||
a52a479910 | |||
324626f5eb | |||
fcacaa8657
|
|||
dca869995a
|
|||
5158aa2cbe
|
|||
f6c662e704 | |||
06d7e6dfaf | |||
322c15576b |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,8 @@
|
||||
# modified from https://github.com/javaee-samples/javaee8-samples/blob/master/.gitignore
|
||||
|
||||
.idea/
|
||||
/*.iml
|
||||
|
||||
# Directories #
|
||||
build/
|
||||
out/
|
||||
|
3
.idea/.gitignore
generated
vendored
3
.idea/.gitignore
generated
vendored
@ -1,3 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
11
.idea/libraries/CodeDraw.xml
generated
11
.idea/libraries/CodeDraw.xml
generated
@ -1,11 +0,0 @@
|
||||
<component name="libraryTable">
|
||||
<library name="CodeDraw">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/CodeDraw.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/CodeDraw.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
6
.idea/misc.xml
generated
6
.idea/misc.xml
generated
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11.0.2" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/angaben.iml" filepath="$PROJECT_DIR$/angaben.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
124
.idea/uiDesigner.xml
generated
124
.idea/uiDesigner.xml
generated
@ -1,124 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -3,7 +3,7 @@
|
||||
## Allgemeine Anmerkungen
|
||||
Ihre Lösung für dieses Aufgabenblatt ist bis Montag, 21.3.2022 11:00 Uhr durch `git commit` und `push` abzugeben. Mit der Angabe werden folgende Dateien mitgeliefert, die sie gemäß der Angabe verändern müssen: `Simulation.java`, `Vector3.java`, `Body.java` und zum Testen `Aufgabe1Test.java`. Die zusätzliche Datei `CodeDraw.jar` wird nur zum Zeichnen verwendet und sollte nicht entfernt oder verändert werden. Die zusätzliche Datei `SpaceDraw.java` enthält Methoden, die in der Simulation benötigt werden. Diese Methoden können Sie auch in Ihrer Lösung aufrufen.
|
||||
|
||||
Vorgegebene Programmteile dürfen nur an den Stellen verändert werden, die mit TODO markiert sind. Zusätzliche Klassen, Interfaces, Methoden und Variablen dürfen aber eingefügt werden. Wenn Sie zusätzlich zu den gefragten Klassen weitere Klassen definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte mit späteren Aufgabenblättern zu vermeiden.
|
||||
Vorgegebene Programmteile dürfen nur an den Stellen verändert werden, die mit `TODO` markiert sind. Zusätzliche Klassen, Interfaces, Methoden und Variablen dürfen aber eingefügt werden. Wenn Sie zusätzlich zu den gefragten Klassen weitere Klassen definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte mit späteren Aufgabenblättern zu vermeiden.
|
||||
|
||||
## Verwendung in IntelliJ
|
||||
Diese Aufgabenstellung ist ein vollständiges IntelliJ-Projekt, das Sie bereits in IntelliJ öffnen können. Sie müssen daher kein eigenes Projekt anlegen. Öffnen Sie nach dem Klonen des Repos in IntelliJ einfach den entsprechenden Ordner. Gegebenenfalls muss noch folgender Schritt ausgeführt werden:
|
||||
|
65
angabe/Aufgabenblatt2.md
Normal file
65
angabe/Aufgabenblatt2.md
Normal file
@ -0,0 +1,65 @@
|
||||
# Aufgabenblatt 2
|
||||
|
||||
## Allgemeine Anmerkungen
|
||||
Ihre Lösung für dieses Aufgabenblatt ist bis Montag, 28.3. 11h durch `git commit` und `push`
|
||||
abzugeben. Mit der Angabe werden folgende Dateien mitgeliefert: `BodyQueue.java`, `BodyForceMap.java` und `Aufgabe2Test.java`. Diese Klassen dürfen nur an den Stellen verändert werden, die mit `TODO` markiert sind. Zusätzliche Klassen, Interfaces, Methoden und Variablen dürfen aber eingefügt werden. Wenn Sie zusätzlich zu den gefragten Klassen, weitere Klassen definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte mit späteren Aufgabenblättern zu vermeiden.
|
||||
|
||||
## Ziel
|
||||
Ziel der Aufgabe ist die Implementierung einer linearen und einer assoziativen Datenstruktur (siehe Skriptum Seiten 50-59).
|
||||
|
||||
## Beschreibung der gegebenen Dateien
|
||||
- `BodyQueue` ist das Gerüst für eine Implementierung einer linearen Datenstruktur zur Verwaltung
|
||||
von Objekten des Typs `Body`.
|
||||
- `BodyForceMap` ist das Gerüst für eine Implementierung einer assoziativen Datenstruktur, die
|
||||
einen Himmelskörper mit der auf ihn wirkenden Kraft assoziiert.
|
||||
- `Aufgabe2Test` ist eine vorgegebene Klasse, die Sie zum Testen Ihrer Implementierung verwenden
|
||||
sollten. Bei einer fehlerfreien Implementierung sollten bei der Ausführung dieser Klasse keine Exceptions geworfen werden und alle Tests als erfolgreich ("successful") ausgegeben werden. Sie müssen diese Klasse nicht verändern, können aber eigene Testfälle hinzufügen.
|
||||
|
||||
## Aufgaben
|
||||
|
||||
Ihre Aufgaben sind folgende:
|
||||
|
||||
1. Fügen Sie in der Klasse `Body` eine öffentliche Objektmethode `mass()` hinzu, die die Masse des Himmelkörpers zurückliefert.
|
||||
2. Vervollständigen Sie die Klassendefinitionen in `BodyQueue.java` gemäß der Kommentare in den Dateien. Die Implementierung soll mit Hilfe eines Arrays erfolgen. Bei der Erzeugung soll das Array die Länge haben, die im Konstruktor angegeben wird. Diese wird verdoppelt, sobald alle Plätze belegt sind. Benutzen Sie keine vorgefertigten Klassen aus dem Java-Collection-Framework!
|
||||
3. Vervollständigen Sie die Klassendefinition in `BodyForceMap.java`. Die Implementierung soll mit Hilfe eines Arrays erfolgen. Benutzen Sie keine vorgefertigten Klassen aus dem Java-Collection-Framework! Implementieren Sie diese Klasse so, dass die Einträge im Array nach der Masse der Himmelskörper absteigend sortiert sind. Das erhöht zwar den Aufwand beim erstmaligen Eintragen eines Schlüssels-Werte-Paars (Methode `put`), da alle Positionen ab der Einfügeposition verschoben werden müssen, es ermöglicht aber eine schnellere Suche nach dem Schlüssel (Methode `get`) mittels binärer Suche. Folgendes Beispiel zeigt die binäre Suche nach der Einfügeposition in einem absteigend sortierten Array. Mit einer entsprechenden zusätzlichen Überprüfung kann auf diese Weise auch der Schlüssel gefunden werden (z.B. `if (keys[middle] == toFind)` ...). Geordnet sind die Einträge nach der Masse, den Schlüssel bildet aber der Himmelskörper.
|
||||
|
||||
```
|
||||
Body[] keys; // assume descending order according to mass
|
||||
Body toInsert;
|
||||
...
|
||||
|
||||
int left = 0;
|
||||
int right = size - 1;
|
||||
|
||||
while (left <= right) {
|
||||
int middle = left + ((right - left) / 2);
|
||||
if (keys[middle].mass() < toInsert.mass()) {
|
||||
right = middle - 1;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// index where to insert: right + 1
|
||||
```
|
||||
4. Bauen Sie die bereits bestehende Klasse `Simulation` so um, dass keine Kollisionen von Himmelskörpern mehr berücksichtigt werden. Dadurch soll der Programmcode vereinfacht werden. Die Anzahl der Himmelskörper ändert sich im Laufe der Simulation somit nicht. Testen Sie die Simulation.
|
||||
5. Ändern Sie nun die Klasse `Simulation` so, dass zur Verwaltung der Himmelskörper anstelle des Arrays Objekte der Klassen `BodyQueue` und `BodyForceMap` verwendet werden. Das heißt beispielsweise, dass die Zugriffe auf die Himmelskörper der Simulation über Methoden von `BodyQueue` erfolgen müssen. Anstelle des Arrays `forceOnBody` soll ein Objekt des Typs `BodyForceMap` benutzt werden.
|
||||
6. Testen Sie die Simulation wieder. Das Verhalten der Simulation sollte unverändert sein. Je nach Implementierung der Klassen `BodyQueue` und `BodyForceMap` ist es möglich (und kein Problem), dass die Simulation jetzt langsamer läuft, als nach dem Umbau in Schritt 4.
|
||||
7. (Freiwillige Zusatzaufgabe ohne Bewertung:) Testen Sie die Simulation mit den folgenden fünf
|
||||
Himmelskörpern:
|
||||
```
|
||||
Body sun = new Body(1.989e30,new Vector3(0,0,0),new Vector3(0,0,0));
|
||||
Body earth = new Body(5.972e24,new Vector3(-1.394555e11,5.103346e10,0),new Vector3(-10308.53,-28169.38,0));
|
||||
Body mercury = new Body(3.301e23,new Vector3(-5.439054e10,9.394878e9,0),new Vector3(-17117.83,-46297.48,-1925.57));
|
||||
Body venus = new Body(4.86747e24,new Vector3(-1.707667e10,1.066132e11,2.450232e9),new Vector3(-34446.02,-5567.47,2181.10));
|
||||
Body mars = new Body(6.41712e23,new Vector3(-1.010178e11,-2.043939e11,-1.591727E9),new Vector3(20651.98,-10186.67,-2302.79));
|
||||
```
|
||||
|
||||
#### _Punkteaufteilung_
|
||||
|
||||
- Implementierung von `BodyQueue`: 2 Punkte
|
||||
- Implementierung von `BodyForceMap`: 2 Punkte
|
||||
- Anpassung von `Simulation`: 1 Punkt
|
||||
- Gesamt: 5 Punkte
|
||||
|
||||
|
50
angabe/Aufgabenblatt3.md
Normal file
50
angabe/Aufgabenblatt3.md
Normal file
@ -0,0 +1,50 @@
|
||||
# Aufgabenblatt 3
|
||||
|
||||
## Allgemeine Anmerkungen
|
||||
|
||||
Ihre Lösung für dieses Aufgabenblatt ist bis Montag, 4.4. 11h durch `git commit` und `push` abzugeben. Mit der Angabe werden folgende Dateien mitgeliefert: [Simulation3](../src/Simulation3.java), [BodyLinkedList](../src/BodyLinkedList.java), [BodyForceTreeMap](../src/BodyForceTreeMap.java) und [Aufgabe3Test](../src/Aufgabe3Test.java).
|
||||
Zusätzliche Klassen, Interfaces, Methoden und Variablen dürfen aber eingefügt werden. Wenn Sie zusätzlich zu den gefragten Klassen weitere Klassen definieren, achten Sie darauf, dass die Klassennamen mit `My` beginnen, um Konflikte mit späteren Aufgabenblättern zu vermeiden.
|
||||
|
||||
## Ziel
|
||||
|
||||
Ziel der Aufgabe ist die Implementierung einer Liste für eine lineare und eines Baums für eine assoziative Datenstruktur (siehe Skriptum Seiten 60-69).
|
||||
|
||||
## Beschreibung der gegebenen Dateien
|
||||
|
||||
- [BodyLinkedList](../src/BodyLinkedList.java) ist das Gerüst für eine Implementierung einer linearen Datenstruktur zur
|
||||
Verwaltung von Objekten des Typs `Body`.
|
||||
- [BodyForceTreeMap](../src/BodyForceTreeMap.java) ist das Gerüst für eine Implementierung einer assoziativen Datenstruktur, die einen Himmelskörper mit der auf ihn wirkenden Kraft assoziiert.
|
||||
- [Aufgabe3Test](../src/Aufgabe3Test.java) ist eine vorgegebene Klasse, die Sie zum Testen Ihrer Implementierung verwenden sollten.
|
||||
Bei einer fehlerfreien Implementierung sollten bei der Ausführung dieser Klasse keine Exceptions geworfen werden und alle Tests als erfolgreich ("successful") ausgegeben werden. Sie müssen diese Klasse nicht verändern, können aber eigene Testfälle hinzufügen.
|
||||
- [Simulation3](../src/Simulation3.java) ist ein Gerüst für eine ausführbare Klasse. Hier soll die Simulation analog
|
||||
zur Klasse `Simulation` implementiert werden (damit Sie Ihre [ursprüngliche Datei](../src/Simulation.java)
|
||||
nicht überschreiben müssen).
|
||||
|
||||
## Aufgaben
|
||||
|
||||
Ihre Aufgaben sind folgende:
|
||||
|
||||
1. Vervollständigen Sie die Klassendefinitionen in [BodyLinkedList](../src/BodyLinkedList.java) gemäß der Kommentare in den Dateien. Die Implementierung soll mit Hilfe einer verketteten Liste erfolgen. Sie können selbst entscheiden, ob Sie eine einfach oder doppelt verkettete Liste implementieren wollen. Benutzen Sie keine Arrays oder vorgefertigten Klassen aus dem Java-Collection-Framework!
|
||||
2. Vervollständigen Sie die Klassendefinition in [BodyForceTreeMap](../src/BodyForceTreeMap.java). Die Implementierung
|
||||
soll mit Hilfe eines binären Suchbaums erfolgen, in dem die Himmelskörper nach deren Masse sortiert sind. Die eigentlichen Schlüssel sind somit Objekte vom Typ `Body`, die interne Ordnung im Suchbaum erfolgt jedoch durch deren Masse. Benutzen Sie keine Arrays oder vorgefertigten Klassen aus dem Java-Collection-Framework!
|
||||
3. Vervollständigen Sie die gegebene Klasse [Simulation3](../src/Simulation3.java) unter der Verwendung der Klassen
|
||||
[BodyLinkedList](../src/BodyLinkedList.java) und [BodyForceTreeMap](../src/BodyForceTreeMap.java), so dass sich diese wie die bereits bestehende Klasse
|
||||
[Simulation](../src/Simulation.java) verhält. Kollisionen sollen wieder berücksichtigt werden. Die Zugriffe auf die
|
||||
Himmelskörper der Simulation sollen über Methoden von [BodyLinkedList](../src/BodyLinkedList.java) erfolgen. Die Klasse [BodyForceTreeMap](../src/BodyForceTreeMap.java) soll zur Verwaltung der Kräfte benutzt werden.
|
||||
|
||||
Allgemeiner Hinweis: bei einigen Methoden sind Vorbedingungen (_pre-conditions_) angegeben. Diese Vorbedingungen müssen innerhalb der Methode NICHT überprüft werden, sondern stellen Zusicherungen dar, auf die die Methode sich verlassen kann. Diese Regel gilt allgemein auch für zukünftige Aufgabenblätter.
|
||||
|
||||
### Denkanstöße (ohne Bewertung)
|
||||
|
||||
1. Haben Sie bei der Implementierung darauf geachtet, dass die Zugriffe möglichst effizient
|
||||
erfolgen können (Z.B. ohne die Liste beim Zugriff wiederholt durchlaufen zu müssen)? Was ist in dem Zusammenhang der Vorteil der verketteten Liste?
|
||||
2. Wofür eignen sich eher die Queue-Methoden `addFirst`, `addLast`, `pollFirst` bzw.
|
||||
`pollLast` und wofür eher die List-Methoden `get`?
|
||||
|
||||
#### _Punkteaufteilung_
|
||||
|
||||
- Implementierung von `BodyLinkedList`: 2 Punkte
|
||||
- Implementierung von `BodyForceTreeMap`: 2 Punkte
|
||||
- Implementierung von `Simulation3`: 1 Punkt
|
||||
- Gesamt: 5 Punkte
|
||||
|
12
angaben.iml
12
angaben.iml
@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="CodeDraw" level="project" />
|
||||
</component>
|
||||
</module>
|
@ -1,17 +1,17 @@
|
||||
import java.awt.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Aufgabe1Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//test classes Body and Vector3
|
||||
|
||||
@Test
|
||||
public void testEP2() {
|
||||
// create two bodies
|
||||
Body sun = new Body(1.989e30,new Vector3(0,0,0),new Vector3(0,0,0));
|
||||
Body earth = new Body(5.972e24,new Vector3(-1.394555e11,5.103346e10,0),new Vector3(-10308.53,-28169.38,0));
|
||||
Body sun = new Body(SolSystem.SUN);
|
||||
Body earth = new Body(SolSystem.EARTH);
|
||||
|
||||
testValue(earth.distanceTo(sun), 1.4850000175024106E11);
|
||||
testValue(sun.distanceTo(earth), 1.4850000175024106E11);
|
||||
assertEquals(1.4850000175024106E11, earth.distanceTo(sun));
|
||||
assertEquals(1.4850000175024106E11, sun.distanceTo(earth));
|
||||
|
||||
for (int i = 0; i < 3600 * 24; i++) {
|
||||
Vector3 f1 = earth.gravitationalForce(sun);
|
||||
@ -22,36 +22,9 @@ public class Aufgabe1Test {
|
||||
}
|
||||
|
||||
// a dummy body to check the correct position after 24h of movement
|
||||
Body targetPositionEarth = new Body(1, new Vector3(-1.403250141841815E11,
|
||||
4.859202658875631E10, 0.0), new Vector3(0,0,0));
|
||||
Body targetPositionEarth = new Body(1, new Vector3(-1.403250141841815E11, 4.859202658875631E10, 0.0), new Vector3(0, 0, 0));
|
||||
|
||||
// check distance to target position (should be zero)
|
||||
testValue(earth.distanceTo(targetPositionEarth), 0);
|
||||
}
|
||||
|
||||
public static void testComparison(Object first, Object second, boolean expected) {
|
||||
boolean real = first == second;
|
||||
|
||||
if (real == expected) {
|
||||
System.out.println("Successful comparison");
|
||||
} else {
|
||||
System.out.println("Comparison NOT successful! Expected value: " + expected + " / Given value: " + real);
|
||||
}
|
||||
}
|
||||
|
||||
public static void testValue(Object given, Object expected) {
|
||||
if (given == expected) {
|
||||
System.out.println("Successful test");
|
||||
} else {
|
||||
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
|
||||
}
|
||||
}
|
||||
|
||||
public static void testValue(double given, double expected) {
|
||||
if (given < expected + (expected+1)/1e12 && given > expected - (expected+1)/1e12) {
|
||||
System.out.println("Successful test");
|
||||
} else {
|
||||
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
|
||||
}
|
||||
assertEquals(0, earth.distanceTo(targetPositionEarth));
|
||||
}
|
||||
}
|
||||
|
51
src/Aufgabe2Test.java
Normal file
51
src/Aufgabe2Test.java
Normal file
@ -0,0 +1,51 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Aufgabe2Test {
|
||||
|
||||
@Test
|
||||
public void testEP2() {
|
||||
// create three bodies
|
||||
Body sun = new Body(SolSystem.SUN);
|
||||
Body earth = new Body(SolSystem.EARTH);
|
||||
Body mercury = new Body(SolSystem.MERCURY);
|
||||
|
||||
// check basic functions of 'BodyQueue'
|
||||
BodyQueue bq = new BodyQueue(2);
|
||||
bq.add(mercury);
|
||||
bq.add(sun);
|
||||
bq.add(earth);
|
||||
assertEquals(3, bq.size());
|
||||
|
||||
assertEquals(mercury, bq.poll());
|
||||
assertEquals(sun, bq.poll());
|
||||
assertEquals(earth, bq.poll());
|
||||
|
||||
assertEquals(0, bq.size());
|
||||
|
||||
bq.add(mercury);
|
||||
bq.add(sun);
|
||||
assertEquals(2, bq.size());
|
||||
|
||||
// check constructor of 'BodyQueue'
|
||||
BodyQueue bqCopy = new BodyQueue(bq);
|
||||
assertNotEquals(bq, bqCopy);
|
||||
assertEquals(bqCopy.poll(), bq.poll());
|
||||
bq.add(earth);
|
||||
assertEquals(2, bq.size());
|
||||
assertEquals(1, bqCopy.size());
|
||||
|
||||
// check basic functions of 'BodyForceMap'
|
||||
BodyForceMap bfm = new BodyForceMap(5);
|
||||
bfm.put(earth, earth.gravitationalForce(sun));
|
||||
bfm.put(sun, sun.gravitationalForce(earth));
|
||||
|
||||
assertEquals(0, bfm.get(earth).distanceTo(earth.gravitationalForce(sun)));
|
||||
assertEquals(0, bfm.get(sun).distanceTo(sun.gravitationalForce(earth)));
|
||||
|
||||
bfm.put(earth, new Vector3(0, 0, 0));
|
||||
assertEquals(0, bfm.get(earth).distanceTo(new Vector3(0, 0, 0)));
|
||||
assertNull(bfm.get(mercury));
|
||||
}
|
||||
}
|
79
src/Aufgabe3Test.java
Normal file
79
src/Aufgabe3Test.java
Normal file
@ -0,0 +1,79 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Aufgabe3Test {
|
||||
|
||||
@Test
|
||||
public void testEP2() {
|
||||
// create five bodies
|
||||
Body sun = new Body(SolSystem.SUN);
|
||||
Body earth = new Body(SolSystem.EARTH);
|
||||
Body mercury = new Body(SolSystem.MERCURY);
|
||||
Body venus = new Body(SolSystem.VENUS);
|
||||
Body mars = new Body(SolSystem.MARS);
|
||||
|
||||
// check basic functions of 'BodyLinkedList'
|
||||
BodyLinkedList bl = new BodyLinkedList();
|
||||
|
||||
bl.addLast(mercury);
|
||||
bl.addLast(sun);
|
||||
bl.addLast(earth);
|
||||
assertEquals(3, bl.size());
|
||||
|
||||
assertEquals(mercury, bl.getFirst());
|
||||
assertEquals(earth, bl.getLast());
|
||||
|
||||
assertEquals(mercury, bl.get(0));
|
||||
assertEquals(sun, bl.get(1));
|
||||
assertEquals(earth, bl.get(2));
|
||||
|
||||
assertEquals(2, bl.indexOf(earth));
|
||||
assertEquals(1, bl.indexOf(sun));
|
||||
assertEquals(0, bl.indexOf(mercury));
|
||||
|
||||
assertEquals(mercury, bl.pollFirst());
|
||||
assertEquals(earth, bl.pollLast());
|
||||
assertEquals(sun, bl.pollFirst());
|
||||
|
||||
assertEquals(0, bl.size());
|
||||
assertNull(bl.getFirst());
|
||||
|
||||
bl.addFirst(earth);
|
||||
bl.addFirst(venus);
|
||||
bl.addFirst(sun);
|
||||
bl.add(1, mercury);
|
||||
bl.add(4, mars);
|
||||
|
||||
assertEquals(5, bl.size());
|
||||
|
||||
assertEquals(sun, bl.get(0));
|
||||
assertEquals(mercury, bl.get(1));
|
||||
assertEquals(venus, bl.get(2));
|
||||
assertEquals(earth, bl.get(3));
|
||||
assertEquals(mars, bl.get(4));
|
||||
|
||||
// check constructor of 'BodyLinkedList'
|
||||
BodyLinkedList blCopy = new BodyLinkedList(bl);
|
||||
assertNotEquals(bl, blCopy);
|
||||
assertEquals(blCopy.pollFirst(), bl.pollFirst());
|
||||
bl.addFirst(sun);
|
||||
assertEquals(5, bl.size());
|
||||
assertEquals(4, blCopy.size());
|
||||
|
||||
// check basic functions of 'BodyForceTreeMap'
|
||||
BodyForceTreeMap bfm = new BodyForceTreeMap();
|
||||
bfm.put(earth, earth.gravitationalForce(sun));
|
||||
bfm.put(sun, sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus)));
|
||||
bfm.put(venus, venus.gravitationalForce(sun));
|
||||
bfm.put(mars, mars.gravitationalForce(sun));
|
||||
bfm.put(mercury, mercury.gravitationalForce(sun));
|
||||
|
||||
assertEquals(0, bfm.get(earth).distanceTo(earth.gravitationalForce(sun)));
|
||||
assertEquals(0, bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))));
|
||||
|
||||
assertEquals(0, bfm.put(earth, new Vector3(0, 0, 0)).distanceTo(earth.gravitationalForce(sun)));
|
||||
assertEquals(0, bfm.get(mercury).distanceTo(mercury.gravitationalForce(sun)));
|
||||
assertEquals(mercury.gravitationalForce(sun), bfm.get(mercury));
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
import codedraw.CodeDraw;
|
||||
|
||||
// This class represents celestial bodies like stars, planets, asteroids, etc..
|
||||
/**
|
||||
* This class represents celestial bodies like stars, planets, asteroids, etc...
|
||||
*/
|
||||
public class Body {
|
||||
private double mass;
|
||||
private final double mass;
|
||||
private Vector3 massCenter; // position of the mass center.
|
||||
private Vector3 currentMovement;
|
||||
|
||||
@ -12,16 +14,26 @@ public class Body {
|
||||
this.currentMovement = currentMovement;
|
||||
}
|
||||
|
||||
// Returns the distance between the mass centers of this body and the specified body 'b'.
|
||||
public Body(Body other) {
|
||||
this.mass = other.mass;
|
||||
this.massCenter = new Vector3(other.massCenter);
|
||||
this.currentMovement = new Vector3(other.currentMovement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the distance between the mass centers of this body and the specified body 'b'.
|
||||
*/
|
||||
public double distanceTo(Body b) {
|
||||
return massCenter.distanceTo(b.massCenter);
|
||||
}
|
||||
|
||||
// Returns a vector representing the gravitational force exerted by 'b' on this body.
|
||||
// The gravitational Force F is calculated by F = G*(m1*m2)/(r*r), with m1 and m2 being the
|
||||
// masses of the objects interacting, r being the distance between the centers of the masses
|
||||
// and G being the gravitational constant.
|
||||
// Hint: see simulation loop in Simulation.java to find out how this is done.
|
||||
/**
|
||||
* Returns a vector representing the gravitational force exerted by 'b' on this body.
|
||||
* The gravitational Force F is calculated by F = G*(m1*m2)/(r*r), with m1 and m2 being the
|
||||
* masses of the objects interacting, r being the distance between the centers of the masses
|
||||
* and G being the gravitational constant.
|
||||
* Hint: see simulation loop in Simulation.java to find out how this is done.
|
||||
*/
|
||||
public Vector3 gravitationalForce(Body b) {
|
||||
Vector3 direction = b.massCenter.minus(massCenter);
|
||||
double distance = direction.length();
|
||||
@ -30,10 +42,12 @@ public class Body {
|
||||
return direction.times(force);
|
||||
}
|
||||
|
||||
// Moves this body to a new position, according to the specified force vector 'force' exerted
|
||||
// on it, and updates the current movement accordingly.
|
||||
// (Movement depends on the mass of this body, its current movement and the exerted force.)
|
||||
// Hint: see simulation loop in Simulation.java to find out how this is done.
|
||||
/**
|
||||
* Moves this body to a new position, according to the specified force vector 'force' exerted
|
||||
* on it, and updates the current movement accordingly.
|
||||
* (Movement depends on the mass of this body, its current movement and the exerted force.)
|
||||
* Hint: see simulation loop in Simulation.java to find out how this is done.
|
||||
*/
|
||||
public void move(Vector3 force) {
|
||||
// F = m*a -> a = F/m
|
||||
Vector3 newPosition = massCenter.plus(force.times(1.0 / mass)).plus(currentMovement);
|
||||
@ -46,43 +60,61 @@ public class Body {
|
||||
currentMovement = newMovement;
|
||||
}
|
||||
|
||||
// Returns the approximate radius of this body.
|
||||
// (It is assumed that the radius r is related to the mass m of the body by r = m ^ 0.5,
|
||||
// where m and r measured in solar units.)
|
||||
/**
|
||||
* Returns the approximate radius of this body.
|
||||
* (It is assumed that the radius r is related to the mass m of the body by r = m ^ 0.5,
|
||||
* where m and r measured in solar units.)
|
||||
*/
|
||||
public double radius() {
|
||||
return SpaceDraw.massToRadius(mass);
|
||||
}
|
||||
|
||||
// Returns a new body that is formed by the collision of this body and 'b'. The impulse
|
||||
// of the returned body is the sum of the impulses of 'this' and 'b'.
|
||||
public Body merge(Body b) {
|
||||
double mass = this.mass + b.mass;
|
||||
/**
|
||||
* Return the mass of the Body.
|
||||
*/
|
||||
public double mass() {
|
||||
return mass;
|
||||
}
|
||||
|
||||
public boolean collidesWith(Body body) {
|
||||
return this.distanceTo(body) < this.radius() + body.radius();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new body that is formed by the collision of this body and 'b'. The impulse
|
||||
* of the returned body is the sum of the impulses of 'this' and 'b'.
|
||||
*/
|
||||
public Body merge(Body body) {
|
||||
double totalMass = this.mass + body.mass;
|
||||
return new Body(
|
||||
mass,
|
||||
massCenter.times(this.mass).plus(b.massCenter.times(b.mass)).times(1.0 / mass),
|
||||
currentMovement.times(this.mass).plus(b.currentMovement.times(b.mass)).times(1.0 / mass)
|
||||
totalMass,
|
||||
this.massCenter.times(this.mass).plus(body.massCenter.times(body.mass)).times(1.0 / totalMass),
|
||||
this.currentMovement.times(this.mass).plus(body.currentMovement.times(body.mass)).times(1.0 / totalMass)
|
||||
);
|
||||
}
|
||||
|
||||
// Draws the body to the specified canvas as a filled circle.
|
||||
// The radius of the circle corresponds to the radius of the body
|
||||
// (use a conversion of the real scale to the scale of the canvas as
|
||||
// in 'Simulation.java').
|
||||
// Hint: call the method 'drawAsFilledCircle' implemented in 'Vector3'.
|
||||
/**
|
||||
* Draws the body to the specified canvas as a filled circle.
|
||||
* The radius of the circle corresponds to the radius of the body
|
||||
* (use a conversion of the real scale to the scale of the canvas as
|
||||
* in 'Simulation.java').
|
||||
* Hint: call the method 'drawAsFilledCircle' implemented in 'Vector3'.
|
||||
*/
|
||||
public void draw(CodeDraw cd) {
|
||||
cd.setColor(SpaceDraw.massToColor(mass));
|
||||
massCenter.drawAsFilledCircle(cd, SpaceDraw.massToRadius(mass));
|
||||
}
|
||||
|
||||
// Returns a string with the information about this body including
|
||||
// mass, position (mass center) and current movement. Example:
|
||||
// "5.972E24 kg, position: [1.48E11,0.0,0.0] m, movement: [0.0,29290.0,0.0] m/s."
|
||||
/**
|
||||
* Returns a string with the information about this body including
|
||||
* mass, position (mass center) and current movement. Example:
|
||||
* "5.972E24 kg, position: [1.48E11,0.0,0.0] m, movement: [0.0,29290.0,0.0] m/s."
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"%f kg, position: %s m, movement: %s m/s.",
|
||||
"%g kg, position: %s m, movement: %s m/s.",
|
||||
mass, massCenter.toString(), currentMovement.toString()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
119
src/BodyForceMap.java
Normal file
119
src/BodyForceMap.java
Normal file
@ -0,0 +1,119 @@
|
||||
/**
|
||||
* A map that associates a body with a force exerted on it. The number of
|
||||
* key-value pairs is not limited.
|
||||
*/
|
||||
public class BodyForceMap {
|
||||
private int size = 0;
|
||||
private int capacity;
|
||||
private Body[] keys;
|
||||
private Vector3[] values;
|
||||
|
||||
public BodyForceMap() {
|
||||
this(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this map with an initial capacity.
|
||||
* Precondition: initialCapacity > 0.
|
||||
*/
|
||||
public BodyForceMap(int initialCapacity) {
|
||||
this.capacity = initialCapacity;
|
||||
this.keys = new Body[this.capacity];
|
||||
this.values = new Vector3[this.capacity];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new key-value association to this map. If the key already exists in this map,
|
||||
* the value is replaced and the old value is returned. Otherwise 'null' is returned.
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 put(Body key, Vector3 force) {
|
||||
if (size == capacity) {
|
||||
doubleCapacity();
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int right = size - 1;
|
||||
while (left <= right) {
|
||||
int middle = left + (right - left) / 2;
|
||||
if (keys[middle] == key) {
|
||||
Vector3 v = values[middle];
|
||||
values[middle] = force;
|
||||
return v;
|
||||
} else if (keys[middle].mass() < key.mass()) {
|
||||
right = middle - 1;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int insert = right + 1;
|
||||
|
||||
for (int i = size; i > insert; i--) {
|
||||
keys[i] = keys[i - 1];
|
||||
values[i] = values[i - 1];
|
||||
}
|
||||
size++;
|
||||
keys[insert] = key;
|
||||
values[insert] = force;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated with the specified key, i.e. the returns the force vector
|
||||
* associated with the specified body. Returns 'null' if the key is not contained in this map.
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 get(Body key) {
|
||||
int left = 0;
|
||||
int right = size - 1;
|
||||
|
||||
while (left <= right) {
|
||||
int middle = left + ((right - left) / 2);
|
||||
int middleLeft = left + (middle - left) / 2;
|
||||
int middleRight = middle + (right - middle) / 2;
|
||||
|
||||
if (keys[middle] == key) {
|
||||
return values[middle];
|
||||
}
|
||||
|
||||
if (keys[middleLeft].mass() == keys[middle].mass()) {
|
||||
for (int i = middleLeft; i < middle; i++) {
|
||||
if (keys[i] == key)
|
||||
return values[i];
|
||||
}
|
||||
}
|
||||
if (keys[middle].mass() == keys[middleRight].mass()) {
|
||||
for (int i = middle; i < middleRight; i++) {
|
||||
if (keys[i] == key)
|
||||
return values[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (keys[middle].mass() < key.mass()) {
|
||||
right = middle - 1;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Doubles the capacity of the map.
|
||||
*/
|
||||
private void doubleCapacity() {
|
||||
capacity *= 2;
|
||||
Body[] tmpKeys = new Body[capacity];
|
||||
Vector3[] tmpValues = new Vector3[capacity];
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
tmpKeys[i] = keys[i];
|
||||
tmpValues[i] = values[i];
|
||||
}
|
||||
|
||||
keys = tmpKeys;
|
||||
values = tmpValues;
|
||||
}
|
||||
}
|
155
src/BodyForceTreeMap.java
Normal file
155
src/BodyForceTreeMap.java
Normal file
@ -0,0 +1,155 @@
|
||||
/**
|
||||
* A map that associates a Body with a Vector3 (typically this is the force exerted on the body).
|
||||
* The number of key-value pairs is not limited.
|
||||
*/
|
||||
public class BodyForceTreeMap {
|
||||
private int size = 0;
|
||||
private BodyForceTreeMapItem root = null;
|
||||
|
||||
/**
|
||||
* Adds a new key-value association to this map. If the key already exists in this map,
|
||||
* the value is replaced and the old value is returned. Otherwise 'null' is returned.
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 put(Body key, Vector3 value) {
|
||||
if (root == null) {
|
||||
root = new BodyForceTreeMapItem(key, value);
|
||||
size++;
|
||||
return null;
|
||||
}
|
||||
|
||||
BodyForceTreeMapItem item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
Vector3 old = item.value();
|
||||
item.setValue(value);
|
||||
return old;
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
if (item.left() != null) {
|
||||
item = item.left();
|
||||
} else {
|
||||
item.setLeft(new BodyForceTreeMapItem(key, value));
|
||||
size++;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (item.right() != null) {
|
||||
item = item.right();
|
||||
} else{
|
||||
item.setRight(new BodyForceTreeMapItem(key, value));
|
||||
size++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated with the specified key, i.e. the method returns the force vector
|
||||
* associated with the specified key. Returns 'null' if the key is not contained in this map.
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 get(Body key) {
|
||||
BodyForceTreeMapItem item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
return item.value();
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
item = item.left();
|
||||
} else {
|
||||
item = item.right();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns 'true' if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(Body key) {
|
||||
BodyForceTreeMapItem item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
return true;
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
item = item.left();
|
||||
} else {
|
||||
item = item.right();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
private String toString(BodyForceTreeMapItem item) {
|
||||
String s = "";
|
||||
if (item == null) {
|
||||
return s;
|
||||
}
|
||||
s += this.toString(item.right());
|
||||
s += String.format("{%s: %s}\n", item.key(), item.value());
|
||||
s += this.toString(item.left());
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a readable representation of this map, in which key-value pairs are ordered
|
||||
* descending according to the mass of the bodies.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(root);
|
||||
}
|
||||
}
|
||||
|
||||
class BodyForceTreeMapItem {
|
||||
private final Body key;
|
||||
private Vector3 value;
|
||||
private BodyForceTreeMapItem parent;
|
||||
private BodyForceTreeMapItem left;
|
||||
private BodyForceTreeMapItem right;
|
||||
|
||||
public BodyForceTreeMapItem(Body key, Vector3 value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Body key() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public void setValue(Vector3 value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Vector3 value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem left() {
|
||||
return this.left;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem right() {
|
||||
return this.right;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem parent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public void setLeft(BodyForceTreeMapItem left) {
|
||||
this.left = left;
|
||||
if (left != null) left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BodyForceTreeMapItem right) {
|
||||
this.right = right;
|
||||
if (right != null) right.parent = this;
|
||||
}
|
||||
}
|
261
src/BodyLinkedList.java
Normal file
261
src/BodyLinkedList.java
Normal file
@ -0,0 +1,261 @@
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A list of bodies implemented as a linked list.
|
||||
* The number of elements of the list is not limited.
|
||||
*/
|
||||
public class BodyLinkedList implements Iterable<Body> {
|
||||
private int size = 0;
|
||||
private BodyLinkedListItem first;
|
||||
private BodyLinkedListItem last;
|
||||
|
||||
/**
|
||||
* Initializes 'this' as an empty list.
|
||||
*/
|
||||
public BodyLinkedList() {
|
||||
first = null;
|
||||
last = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes 'this' as an independent copy of the specified list 'list'.
|
||||
* Calling methods of this list will not affect the specified list 'list'
|
||||
* and vice versa.
|
||||
* Precondition: list != null.
|
||||
*/
|
||||
public BodyLinkedList(BodyLinkedList list) {
|
||||
this.size = 0;
|
||||
for (Body b : list) {
|
||||
this.addLast(b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element 'body' at the beginning of this list.
|
||||
*/
|
||||
public void addFirst(Body body) {
|
||||
if (first == null) {
|
||||
first = new BodyLinkedListItem(body);
|
||||
last = first;
|
||||
} else {
|
||||
first.setPrev(new BodyLinkedListItem(body));
|
||||
first = first.prev();
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the specified element 'body' to the end of this list.
|
||||
*/
|
||||
public void addLast(Body body) {
|
||||
if (last == null) {
|
||||
last = new BodyLinkedListItem(body);
|
||||
first = last;
|
||||
} else {
|
||||
last.setNext(new BodyLinkedListItem(body));
|
||||
last = last.next();
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body getLast() {
|
||||
return (last != null) ? last.body() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body getFirst() {
|
||||
return (first != null) ? first.body() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the first element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body pollFirst() {
|
||||
if (first == null) {
|
||||
return null;
|
||||
}
|
||||
Body b = first.body();
|
||||
first = first.next();
|
||||
if (first != null) first.setPrev(null);
|
||||
size--;
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the last element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body pollLast() {
|
||||
if (last == null) {
|
||||
return null;
|
||||
}
|
||||
Body b = last.body();
|
||||
last = last.prev();
|
||||
if (last != null) last.setNext(null);
|
||||
size--;
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element 'body' at the specified position in this list.
|
||||
* Precondition: i >= 0 && i <= size().
|
||||
*/
|
||||
public void add(int i, Body body) {
|
||||
if (first == null || i == 0) {
|
||||
addFirst(body);
|
||||
return;
|
||||
} else if (i == size) {
|
||||
addLast(body);
|
||||
return;
|
||||
}
|
||||
|
||||
BodyLinkedListItem item = first;
|
||||
for (int j = 0; j < i; j++) {
|
||||
item = item.next();
|
||||
}
|
||||
|
||||
item.prev().setNext(new BodyLinkedListItem(body));
|
||||
item.setPrev(item.prev().next());
|
||||
size++;
|
||||
}
|
||||
|
||||
private Body removeItem(BodyLinkedListItem item) {
|
||||
if (item == first) {
|
||||
first = item.next();
|
||||
if (first != null) first.setPrev(null);
|
||||
} else if (item == last) {
|
||||
last = item.prev();
|
||||
if (last != null) last.setNext(null);
|
||||
} else {
|
||||
item.next().setPrev(item.prev());
|
||||
}
|
||||
size--;
|
||||
return item.body();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
* Precondition: i >= 0 && i < size().
|
||||
*/
|
||||
public Body get(int i) {
|
||||
BodyLinkedListItem item;
|
||||
if (i < size / 2) {
|
||||
item = first;
|
||||
for (int j = 0; j < i; j++) {
|
||||
item = item.next();
|
||||
}
|
||||
} else {
|
||||
item = last;
|
||||
for (int j = size - 1; j > i; j--) {
|
||||
item = item.prev();
|
||||
}
|
||||
}
|
||||
return item.body();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first occurrence of the specified element in this list, or -1 if
|
||||
* this list does not contain the element.
|
||||
*/
|
||||
public int indexOf(Body body) {
|
||||
if (first == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
BodyLinkedListItem item = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (item.body() == body) {
|
||||
return i;
|
||||
}
|
||||
item = item.next();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all bodies of this list, which are colliding with the specified
|
||||
* body. Returns a list with all the removed bodies.
|
||||
*/
|
||||
public BodyLinkedList removeCollidingWith(Body body) {
|
||||
BodyLinkedList removed = new BodyLinkedList();
|
||||
for (BodyLinkedListItem item = first; item != null; item = item.next()) {
|
||||
if (body != item.body() && body.collidesWith(item.body())) {
|
||||
removed.addLast(this.removeItem(item));
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of bodies in this list.
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Body> iterator() {
|
||||
return new Iterator<>() {
|
||||
BodyLinkedListItem ptr = first;
|
||||
boolean yieldedFirst = false;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return ptr != null && (!yieldedFirst || ptr.next() != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Body next() {
|
||||
if (!yieldedFirst) {
|
||||
yieldedFirst = true;
|
||||
return ptr.body();
|
||||
}
|
||||
ptr = ptr.next();
|
||||
return ptr.body();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class BodyLinkedListItem {
|
||||
private final Body body;
|
||||
private BodyLinkedListItem prev;
|
||||
private BodyLinkedListItem next;
|
||||
|
||||
public BodyLinkedListItem(Body body) {
|
||||
this.body = body;
|
||||
this.prev = null;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public Body body() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public BodyLinkedListItem prev() {
|
||||
return prev;
|
||||
}
|
||||
|
||||
public void setPrev(BodyLinkedListItem prev) {
|
||||
this.prev = prev;
|
||||
if (prev != null) prev.next = this;
|
||||
}
|
||||
|
||||
public BodyLinkedListItem next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(BodyLinkedListItem next) {
|
||||
this.next = next;
|
||||
if (next != null) next.prev = this;
|
||||
}
|
||||
}
|
89
src/BodyQueue.java
Normal file
89
src/BodyQueue.java
Normal file
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* A queue of bodies. A collection designed for holding bodies prior to processing.
|
||||
* The bodies of the queue can be accessed in a FIFO (first-in-first-out) manner,
|
||||
* i.e., the body that was first inserted by 'add' is retrieved first by 'poll'.
|
||||
* The number of elements of the queue is not limited.
|
||||
*/
|
||||
public class BodyQueue {
|
||||
private int capacity;
|
||||
private int head = 0;
|
||||
private int tail = 0;
|
||||
private Body[] queue;
|
||||
|
||||
public BodyQueue() {
|
||||
this(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this queue with an initial capacity.
|
||||
* Precondition: initialCapacity > 0.
|
||||
*/
|
||||
public BodyQueue(int initialCapacity) {
|
||||
this.capacity = initialCapacity;
|
||||
this.queue = new Body[this.capacity];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this queue as an independent copy of the specified queue.
|
||||
* Calling methods of this queue will not affect the specified queue
|
||||
* and vice versa.
|
||||
* Precondition: other != null.
|
||||
*/
|
||||
public BodyQueue(BodyQueue other) {
|
||||
this.capacity = other.capacity;
|
||||
this.head = other.size();
|
||||
this.tail = 0;
|
||||
this.queue = new Body[this.capacity];
|
||||
for (int i = 0, j = other.tail; i < this.head; i++, j++) {
|
||||
this.queue[i] = other.queue[j % other.capacity];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified body 'b' to this queue.
|
||||
*/
|
||||
public void add(Body b) {
|
||||
if ((head + 1) % capacity == tail) {
|
||||
doubleCapacity();
|
||||
}
|
||||
queue[head] = b;
|
||||
head = (head + 1) % capacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue, or returns 'null'
|
||||
* if this queue is empty.
|
||||
*/
|
||||
public Body poll() {
|
||||
if (tail == head) {
|
||||
tail = 0;
|
||||
head = 0;
|
||||
return null;
|
||||
}
|
||||
Body b = queue[tail];
|
||||
queue[tail] = null;
|
||||
tail = (tail + 1) % capacity;
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of bodies in this queue.
|
||||
*/
|
||||
public int size() {
|
||||
return (head - tail + capacity) % capacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Double the capacity of the queue.
|
||||
*/
|
||||
private void doubleCapacity() {
|
||||
Body[] tmp = new Body[capacity * 2];
|
||||
head = size();
|
||||
for (int i = 0, j = tail; i < head; i++, j++) {
|
||||
tmp[i] = queue[j % capacity];
|
||||
}
|
||||
tail = 0;
|
||||
capacity *= 2;
|
||||
queue = tmp;
|
||||
}
|
||||
}
|
@ -12,7 +12,9 @@ import java.util.Random;
|
||||
// 3. Beim Methodenaufruf steht links vom `.` entweder das Objekt, oder die Klasse. In Java beginnen Variablennamen
|
||||
// üblicherweise mit Kleinbuchstaben und Klassennamen üblicherweise mit einem Großbuchstaben.
|
||||
|
||||
// Simulates the formation of a massive solar system.
|
||||
/**
|
||||
* Simulates the formation of a massive solar system.
|
||||
*/
|
||||
public class Simulation {
|
||||
|
||||
// gravitational constant
|
||||
@ -37,18 +39,20 @@ public class Simulation {
|
||||
|
||||
// all quantities are based on units of kilogram respectively second and meter.
|
||||
|
||||
// The main simulation method using instances of other classes.
|
||||
/**
|
||||
* The main simulation method using instances of other classes.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// simulation
|
||||
CodeDraw cd = new CodeDraw();
|
||||
Body[] bodies = new Body[NUMBER_OF_BODIES];
|
||||
Vector3[] forceOnBody = new Vector3[bodies.length];
|
||||
BodyQueue bodies = new BodyQueue();
|
||||
BodyForceMap forceOnBody = new BodyForceMap();
|
||||
|
||||
Random random = new Random(2022);
|
||||
|
||||
for (int i = 0; i < bodies.length; i++) {
|
||||
bodies[i] = new Body(
|
||||
Math.abs(random.nextGaussian()) * OVERALL_SYSTEM_MASS / bodies.length, // kg
|
||||
for (int i = 0; i < NUMBER_OF_BODIES; i++) {
|
||||
bodies.add(new Body(
|
||||
Math.abs(random.nextGaussian()) * OVERALL_SYSTEM_MASS / NUMBER_OF_BODIES, // kg
|
||||
new Vector3(
|
||||
0.2 * random.nextGaussian() * AU,
|
||||
0.2 * random.nextGaussian() * AU,
|
||||
@ -59,19 +63,25 @@ public class Simulation {
|
||||
0 + random.nextGaussian() * 5e3,
|
||||
0 + random.nextGaussian() * 5e3
|
||||
)
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
double seconds = 0;
|
||||
|
||||
// simulation loop
|
||||
while (true) {
|
||||
BodyQueue newBodies = new BodyQueue(bodies);
|
||||
Body[] tmp = new Body[bodies.size()];
|
||||
for (int i = 0; bodies.size() > 0; i++) {
|
||||
tmp[i] = bodies.poll();
|
||||
}
|
||||
seconds++; // each iteration computes the movement of the celestial bodies within one second.
|
||||
|
||||
/*
|
||||
// merge bodies that have collided
|
||||
for (int i = 0; i < bodies.length; i++) {
|
||||
for (int j = i + 1; j < bodies.length; j++) {
|
||||
if (bodies[j].distanceTo(bodies[i]) < bodies[j].radius() + bodies[i].radius()) {
|
||||
if (bodies[j].collidesWith(bodies[i])) {
|
||||
bodies[i] = bodies[i].merge(bodies[j]);
|
||||
Body[] bodiesOneRemoved = new Body[bodies.length - 1];
|
||||
for (int k = 0; k < bodiesOneRemoved.length; k++) {
|
||||
@ -86,22 +96,23 @@ public class Simulation {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// for each body (with index i): compute the total force exerted on it.
|
||||
for (int i = 0; i < bodies.length; i++) {
|
||||
forceOnBody[i] = new Vector3(); // begin with zero
|
||||
for (int j = 0; j < bodies.length; j++) {
|
||||
if (i != j) {
|
||||
Vector3 forceToAdd = bodies[i].gravitationalForce(bodies[j]);
|
||||
forceOnBody[i] = forceOnBody[i].plus(forceToAdd);
|
||||
for (Body b1 : tmp) {
|
||||
Vector3 force = new Vector3(); // begin with zero
|
||||
for (Body b2 : tmp) {
|
||||
if (b1 != b2) {
|
||||
force = force.plus(b1.gravitationalForce(b2));
|
||||
}
|
||||
}
|
||||
forceOnBody.put(b1, force);
|
||||
}
|
||||
// now forceOnBody[i] holds the force vector exerted on body with index i.
|
||||
|
||||
// for each body (with index i): move it according to the total force exerted on it.
|
||||
for (int i = 0; i < bodies.length; i++) {
|
||||
bodies[i].move(forceOnBody[i]);
|
||||
for (Body body : tmp) {
|
||||
body.move(forceOnBody.get(body));
|
||||
}
|
||||
|
||||
// show all movements in the canvas only every hour (to speed up the simulation)
|
||||
@ -110,7 +121,7 @@ public class Simulation {
|
||||
cd.clear(Color.BLACK);
|
||||
|
||||
// draw new positions
|
||||
for (Body body : bodies) {
|
||||
for (Body body : tmp) {
|
||||
body.draw(cd);
|
||||
}
|
||||
|
||||
@ -118,7 +129,7 @@ public class Simulation {
|
||||
cd.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bodies = newBodies;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
74
src/Simulation3.java
Normal file
74
src/Simulation3.java
Normal file
@ -0,0 +1,74 @@
|
||||
import codedraw.CodeDraw;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Simulates the formation of a massive solar system.
|
||||
*/
|
||||
public class Simulation3 {
|
||||
|
||||
/**
|
||||
* The main simulation method using instances of other classes.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CodeDraw cd = new CodeDraw();
|
||||
BodyLinkedList bodies = new BodyLinkedList();
|
||||
BodyForceTreeMap forceOnBody = new BodyForceTreeMap();
|
||||
|
||||
Random random = new Random(2022);
|
||||
|
||||
for (int i = 0; i < Simulation.NUMBER_OF_BODIES; i++) {
|
||||
bodies.addLast(new Body(
|
||||
Math.abs(random.nextGaussian()) * Simulation.OVERALL_SYSTEM_MASS / Simulation.NUMBER_OF_BODIES,
|
||||
new Vector3(
|
||||
0.2 * random.nextGaussian() * Simulation.AU,
|
||||
0.2 * random.nextGaussian() * Simulation.AU,
|
||||
0.2 * random.nextGaussian() * Simulation.AU
|
||||
),
|
||||
new Vector3(
|
||||
0 + random.nextGaussian() * 5e3,
|
||||
0 + random.nextGaussian() * 5e3,
|
||||
0 + random.nextGaussian() * 5e3
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
long seconds = 0;
|
||||
while (true) {
|
||||
seconds++;
|
||||
|
||||
BodyLinkedList mergedBodies = new BodyLinkedList();
|
||||
for (Body b1 : bodies) {
|
||||
BodyLinkedList colliding = bodies.removeCollidingWith(b1);
|
||||
for (Body b2 : colliding) {
|
||||
b1 = b1.merge(b2);
|
||||
}
|
||||
mergedBodies.addLast(b1);
|
||||
}
|
||||
bodies = mergedBodies;
|
||||
|
||||
for (Body b1 : bodies) {
|
||||
Vector3 force = new Vector3();
|
||||
for (Body b2 : bodies) {
|
||||
if (b1 != b2) {
|
||||
force = force.plus(b1.gravitationalForce(b2));
|
||||
}
|
||||
}
|
||||
forceOnBody.put(b1, force);
|
||||
}
|
||||
|
||||
for (Body body : bodies) {
|
||||
body.move(forceOnBody.get(body));
|
||||
}
|
||||
|
||||
if ((seconds % 3600) == 0) {
|
||||
cd.clear(Color.BLACK);
|
||||
for (Body body : bodies) {
|
||||
body.draw(cd);
|
||||
}
|
||||
cd.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
src/SolSystem.java
Normal file
7
src/SolSystem.java
Normal file
@ -0,0 +1,7 @@
|
||||
public class SolSystem {
|
||||
public static final Body SUN = new Body(1.989e30, new Vector3(0, 0, 0), new Vector3(0, 0, 0));
|
||||
public static final Body EARTH = new Body(5.972e24, new Vector3(-1.394555e11, 5.103346e10, 0), new Vector3(-10308.53, -28169.38, 0));
|
||||
public static final Body MERCURY = new Body(3.301e23, new Vector3(-5.439054e10, 9.394878e9, 0), new Vector3(-17117.83, -46297.48, -1925.57));
|
||||
public static final Body VENUS = new Body(4.86747e24, new Vector3(-1.707667e10, 1.066132e11, 2.450232e9), new Vector3(-34446.02, -5567.47, 2181.10));
|
||||
public static final Body MARS = new Body(6.41712e23, new Vector3(-1.010178e11, -2.043939e11, -1.591727E9), new Vector3(20651.98, -10186.67, -2302.79));
|
||||
}
|
@ -2,17 +2,20 @@ import java.awt.*;
|
||||
|
||||
public class SpaceDraw {
|
||||
|
||||
// Returns the approximate radius of a celestial body with the specified mass.
|
||||
// (It is assumed that the radius r is related to the mass m of the body by r = m ^ 0.5,
|
||||
// where m and r measured in solar units.)
|
||||
/**
|
||||
* Returns the approximate radius of a celestial body with the specified mass.
|
||||
* (It is assumed that the radius r is related to the mass m of the body by r = m ^ 0.5,
|
||||
* where m and r measured in solar units.)
|
||||
*/
|
||||
public static double massToRadius(double mass) {
|
||||
|
||||
return Simulation.SUN_RADIUS * (Math.pow(mass / Simulation.SUN_MASS, 0.5));
|
||||
}
|
||||
|
||||
// Returns the approximate color of a celestial body with the specified mass. The color of
|
||||
// the body corresponds to the temperature of the body, assuming the relation of mass and
|
||||
// temperature of a main sequence star.
|
||||
/**
|
||||
* Returns the approximate color of a celestial body with the specified mass. The color of
|
||||
* the body corresponds to the temperature of the body, assuming the relation of mass and
|
||||
* temperature of a main sequence star.
|
||||
*/
|
||||
public static Color massToColor(double mass) {
|
||||
Color color;
|
||||
if (mass < Simulation.SUN_MASS / 10) {
|
||||
@ -26,9 +29,10 @@ public class SpaceDraw {
|
||||
return color;
|
||||
}
|
||||
|
||||
// Returns the approximate color of temperature 'kelvin'.
|
||||
/**
|
||||
* Returns the approximate color of temperature 'kelvin'.
|
||||
*/
|
||||
private static Color kelvinToColor(int kelvin) {
|
||||
|
||||
double k = kelvin / 100D;
|
||||
double red = k <= 66 ? 255 : 329.698727446 * Math.pow(k - 60, -0.1332047592);
|
||||
double green = k <= 66 ? 99.4708025861 * Math.log(k) - 161.1195681661 : 288.1221695283 * Math.pow(k - 60, -0.0755148492);
|
||||
@ -41,14 +45,19 @@ public class SpaceDraw {
|
||||
);
|
||||
}
|
||||
|
||||
// A transformation used in the method 'kelvinToColor'.
|
||||
/**
|
||||
* A transformation used in the method 'kelvinToColor'.
|
||||
*/
|
||||
private static int limitAndDarken(double color, int kelvin) {
|
||||
|
||||
int kelvinNorm = kelvin - 373;
|
||||
|
||||
if (color < 0 || kelvinNorm < 0) return 0;
|
||||
else if (color > 255) return 255;
|
||||
else if (kelvinNorm < 500) return (int) ((color / 256D) * (kelvinNorm / 500D) * 256);
|
||||
else return (int) color;
|
||||
if (color < 0 || kelvinNorm < 0) {
|
||||
return 0;
|
||||
} else if (color > 255) {
|
||||
return 255;
|
||||
} else if (kelvinNorm < 500) {
|
||||
return (int) ((color / 256D) * (kelvinNorm / 500D) * 256);
|
||||
} else {
|
||||
return (int) color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
import codedraw.CodeDraw;
|
||||
|
||||
// This class represents vectors in a 3D vector space.
|
||||
/**
|
||||
* This class represents vectors in a 3D vector space.
|
||||
*/
|
||||
public class Vector3 {
|
||||
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
@ -21,35 +22,35 @@ public class Vector3 {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
// Returns the sum of this vector and vector 'v'.
|
||||
public Vector3(Vector3 other) {
|
||||
this(other.x, other.y, other.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this vector and vector 'v'.
|
||||
*/
|
||||
public Vector3 plus(Vector3 v) {
|
||||
Vector3 result = new Vector3();
|
||||
result.x = x + v.x;
|
||||
result.y = y + v.y;
|
||||
result.z = z + v.z;
|
||||
return result;
|
||||
return new Vector3(x + v.x, y + v.y, z + v.z);
|
||||
}
|
||||
|
||||
// Returns the product of this vector and 'd'.
|
||||
/**
|
||||
* Returns the product of this vector and 'd'.
|
||||
*/
|
||||
public Vector3 times(double d) {
|
||||
Vector3 result = new Vector3();
|
||||
result.x = x * d;
|
||||
result.y = y * d;
|
||||
result.z = z * d;
|
||||
return result;
|
||||
return new Vector3(x * d, y * d, z * d);
|
||||
}
|
||||
|
||||
// Returns the sum of this vector and -1*v.
|
||||
/**
|
||||
* Returns the sum of this vector and -1*v.
|
||||
*/
|
||||
public Vector3 minus(Vector3 v) {
|
||||
Vector3 result = new Vector3();
|
||||
result.x = x - v.x;
|
||||
result.y = y - v.y;
|
||||
result.z = z - v.z;
|
||||
return result;
|
||||
return new Vector3(x - v.x, y - v.y, z - v.z);
|
||||
}
|
||||
|
||||
// Returns the Euclidean distance of this vector
|
||||
// to the specified vector 'v'.
|
||||
/**
|
||||
* Returns the Euclidean distance of this vector
|
||||
* to the specified vector 'v'.
|
||||
*/
|
||||
public double distanceTo(Vector3 v) {
|
||||
double dX = x - v.x;
|
||||
double dY = y - v.y;
|
||||
@ -57,13 +58,17 @@ public class Vector3 {
|
||||
return Math.sqrt(dX * dX + dY * dY + dZ * dZ);
|
||||
}
|
||||
|
||||
// Returns the length (norm) of this vector.
|
||||
/**
|
||||
* Returns the length (norm) of this vector.
|
||||
*/
|
||||
public double length() {
|
||||
return distanceTo(new Vector3());
|
||||
}
|
||||
|
||||
// Normalizes this vector: changes the length of this vector such that it becomes 1.
|
||||
// The direction and orientation of the vector is not affected.
|
||||
/**
|
||||
* Normalizes this vector: changes the length of this vector such that it becomes 1.
|
||||
* The direction and orientation of the vector is not affected.
|
||||
*/
|
||||
public void normalize() {
|
||||
double length = length();
|
||||
x /= length;
|
||||
@ -71,20 +76,38 @@ public class Vector3 {
|
||||
z /= length;
|
||||
}
|
||||
|
||||
// Draws a filled circle with a specified radius centered at the (x,y) coordinates of this vector
|
||||
// in the canvas associated with 'cd'. The z-coordinate is not used.
|
||||
public double getScreenX(CodeDraw cd) {
|
||||
return cd.getWidth() * (this.x + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
}
|
||||
|
||||
public double getScreenY(CodeDraw cd) {
|
||||
return cd.getWidth() * (this.y + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a filled circle with a specified radius centered at the (x,y) coordinates of this vector
|
||||
* in the canvas associated with 'cd'. The z-coordinate is not used.
|
||||
*/
|
||||
public void drawAsFilledCircle(CodeDraw cd, double radius) {
|
||||
double x = cd.getWidth() * (this.x + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
double y = cd.getWidth() * (this.y + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
radius = cd.getWidth() * radius / Simulation.SECTION_SIZE;
|
||||
cd.fillCircle(x, y, Math.max(radius, 1.5));
|
||||
cd.fillCircle(getScreenX(cd), getScreenY(cd), Math.max(radius, 1.5));
|
||||
}
|
||||
|
||||
// Returns the coordinates of this vector in brackets as a string
|
||||
// in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]".
|
||||
/**
|
||||
* Returns the coordinates of this vector in brackets as a string
|
||||
* in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]".
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("[%f,%f,%f]", x, y, z);
|
||||
return String.format("[%g,%g,%g]", x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other.getClass() != Vector3.class) {
|
||||
return false;
|
||||
}
|
||||
Vector3 v = (Vector3) other;
|
||||
return this.x == v.x && this.y == v.y && this.z == v.z;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user