Refactor comments
This commit is contained in:
@ -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:
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
## 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.
|
||||
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 linearer und einer assoziativen Datenstruktur (siehe Skriptum Seiten 50-59).
|
||||
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 linearer Datenstruktur zur Verwaltung
|
||||
- `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.
|
||||
@ -20,8 +20,8 @@ sollten. Bei einer fehlerfreien Implementierung sollten bei der Ausführung dies
|
||||
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.
|
||||
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
|
||||
|
@ -1,9 +1,7 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Aufgabe1Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//test classes Body and Vector3
|
||||
|
||||
// create two bodies
|
||||
|
@ -1,6 +1,8 @@
|
||||
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 Vector3 massCenter; // position of the mass center.
|
||||
@ -12,16 +14,20 @@ public class Body {
|
||||
this.currentMovement = currentMovement;
|
||||
}
|
||||
|
||||
// Returns the distance between the mass centers of this body and the specified body 'b'.
|
||||
/**
|
||||
* 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 +36,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,19 +54,26 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mass of the Body.
|
||||
*/
|
||||
public double mass() {
|
||||
return 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'.
|
||||
/**
|
||||
* 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 new Body(
|
||||
@ -68,25 +83,27 @@ public class Body {
|
||||
);
|
||||
}
|
||||
|
||||
// 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."
|
||||
*/
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"%f kg, position: %s m, movement: %s m/s.",
|
||||
mass, massCenter.toString(), currentMovement.toString()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
// A map that associates a body with a force exerted on it. The number of
|
||||
// key-value pairs is not limited.
|
||||
//
|
||||
/**
|
||||
* 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;
|
||||
@ -12,17 +12,21 @@ public class BodyForceMap {
|
||||
this(4);
|
||||
}
|
||||
|
||||
// Initializes this map with an initial capacity.
|
||||
// Precondition: initialCapacity > 0.
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* 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();
|
||||
@ -55,9 +59,11 @@ public class BodyForceMap {
|
||||
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.
|
||||
/**
|
||||
* 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;
|
||||
@ -94,6 +100,9 @@ public class BodyForceMap {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Doubles the capacity of the map.
|
||||
*/
|
||||
private void doubleCapacity() {
|
||||
capacity *= 2;
|
||||
Body[] tmpKeys = new Body[capacity];
|
||||
|
@ -1,10 +1,10 @@
|
||||
// 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.
|
||||
//
|
||||
/**
|
||||
* 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;
|
||||
@ -14,17 +14,21 @@ public class BodyQueue {
|
||||
this(4);
|
||||
}
|
||||
|
||||
// Initializes this queue with an initial capacity.
|
||||
// Precondition: initialCapacity > 0.
|
||||
/**
|
||||
* 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: q != null.
|
||||
/**
|
||||
* 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: q != null.
|
||||
*/
|
||||
public BodyQueue(BodyQueue q) {
|
||||
this.capacity = q.capacity;
|
||||
this.head = q.size();
|
||||
@ -35,7 +39,9 @@ public class BodyQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// Adds the specified body 'b' to this queue.
|
||||
/**
|
||||
* Adds the specified body 'b' to this queue.
|
||||
*/
|
||||
public void add(Body b) {
|
||||
queue[head] = b;
|
||||
head = (head + 1) % capacity;
|
||||
@ -45,8 +51,10 @@ public class BodyQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieves and removes the head of this queue, or returns 'null'
|
||||
// if this queue is empty.
|
||||
/**
|
||||
* Retrieves and removes the head of this queue, or returns 'null'
|
||||
* if this queue is empty.
|
||||
*/
|
||||
public Body poll() {
|
||||
if (tail == head) {
|
||||
return null;
|
||||
@ -57,11 +65,16 @@ public class BodyQueue {
|
||||
return b;
|
||||
}
|
||||
|
||||
// Returns the number of bodies in this queue.
|
||||
/**
|
||||
* 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];
|
||||
for (int i = head, j = 0; i < tail + capacity; i++, j++) {
|
||||
|
@ -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,7 +39,9 @@ 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();
|
||||
@ -127,6 +131,5 @@ public class Simulation {
|
||||
|
||||
bodies = newBodies;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,21 @@ 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,7 +30,9 @@ 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;
|
||||
@ -41,7 +47,9 @@ 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;
|
||||
|
@ -1,6 +1,8 @@
|
||||
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;
|
||||
@ -21,7 +23,9 @@ public class Vector3 {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
// Returns the sum of this vector and vector 'v'.
|
||||
/**
|
||||
* Returns the sum of this vector and vector 'v'.
|
||||
*/
|
||||
public Vector3 plus(Vector3 v) {
|
||||
Vector3 result = new Vector3();
|
||||
result.x = x + v.x;
|
||||
@ -30,7 +34,9 @@ public class Vector3 {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -39,7 +45,9 @@ public class Vector3 {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -48,8 +56,10 @@ public class Vector3 {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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 +67,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,8 +85,10 @@ 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.
|
||||
/**
|
||||
* 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;
|
||||
@ -80,11 +96,11 @@ public class Vector3 {
|
||||
cd.fillCircle(x, y, 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]".
|
||||
*/
|
||||
public String toString() {
|
||||
return String.format("[%f,%f,%f]", x, y, z);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user