Compare commits
7 Commits
Uebungstes
...
Uebungstes
Author | SHA1 | Date | |
---|---|---|---|
cda144aa2a
|
|||
4bb1d6a36c
|
|||
01b2fb8989
|
|||
9925835a1e
|
|||
f24ad9bcaf
|
|||
cf4c1ad9d0
|
|||
8c67e157d5
|
@ -1,7 +1,6 @@
|
||||
public class Aufgabe2Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//test classes BodyQueue and BodyForceMap
|
||||
|
||||
// create three bodies
|
||||
@ -49,7 +48,6 @@ public class Aufgabe2Test {
|
||||
bfm.put(earth, new Vector3(0,0,0));
|
||||
testValue(bfm.get(earth).distanceTo(new Vector3(0,0,0)), 0);
|
||||
testValue(bfm.get(mercury),null);
|
||||
|
||||
}
|
||||
|
||||
public static void testComparison(Object first, Object second, boolean expected) {
|
||||
|
@ -3,7 +3,6 @@ import java.util.Objects;
|
||||
public class Aufgabe3Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//test classes BodyLinkedList and BodyForceTreeMap
|
||||
|
||||
// create five bodies
|
||||
@ -79,9 +78,8 @@ public class Aufgabe3Test {
|
||||
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))), 0);
|
||||
|
||||
testValue(bfm.put(earth, new Vector3(0, 0, 0)).distanceTo(earth.gravitationalForce(sun)), 0);
|
||||
testValue(bfm.get(earth).distanceTo(new Vector3(0, 0, 0)), 0);
|
||||
testValue(bfm.get(mercury).distanceTo(mercury.gravitationalForce(sun)), 0);
|
||||
testValue(bfm.get(mercury), mercury.gravitationalForce(sun));
|
||||
|
||||
}
|
||||
|
||||
public static void testComparison(Object first, Object second, boolean expected) {
|
||||
@ -95,7 +93,7 @@ public class Aufgabe3Test {
|
||||
}
|
||||
|
||||
public static void testValue(Object given, Object expected) {
|
||||
if (given == expected) {
|
||||
if (Objects.equals(given, expected)) {
|
||||
System.out.println("Successful test");
|
||||
} else {
|
||||
System.out.println("Test NOT successful! Expected value: " + expected + " / Given value: " + given);
|
||||
|
@ -70,16 +70,20 @@ public class Body {
|
||||
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 b) {
|
||||
double mass = this.mass + b.mass;
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
@ -102,7 +106,7 @@ public class Body {
|
||||
*/
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
@ -1,40 +1,154 @@
|
||||
// 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.
|
||||
/**
|
||||
* 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;
|
||||
|
||||
//TODO: declare variables.
|
||||
|
||||
// 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: implement method.
|
||||
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.
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
//TODO: implement method.
|
||||
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.
|
||||
/**
|
||||
* Returns 'true' if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(Body key) {
|
||||
|
||||
//TODO: implement method.
|
||||
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;
|
||||
}
|
||||
|
||||
// Returns a readable representation of this map, in which key-value pairs are ordered
|
||||
// descending according to the mass of the bodies.
|
||||
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.
|
||||
*/
|
||||
public String toString() {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +1,261 @@
|
||||
// A list of bodies implemented as a linked list.
|
||||
// The number of elements of the list is not limited.
|
||||
public class BodyLinkedList {
|
||||
import java.util.Iterator;
|
||||
|
||||
//TODO: declare variables.
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* Initializes 'this' as an empty list.
|
||||
*/
|
||||
public BodyLinkedList() {
|
||||
|
||||
//TODO: define constructor.
|
||||
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.
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
//TODO: define constructor.
|
||||
this.size = 0;
|
||||
for (Body b : list) {
|
||||
this.addLast(b);
|
||||
}
|
||||
}
|
||||
|
||||
// Inserts the specified element 'body' at the beginning of this list.
|
||||
/**
|
||||
* Inserts the specified element 'body' at the beginning of this list.
|
||||
*/
|
||||
public void addFirst(Body body) {
|
||||
|
||||
//TODO: implement method.
|
||||
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.
|
||||
/**
|
||||
* Appends the specified element 'body' to the end of this list.
|
||||
*/
|
||||
public void addLast(Body body) {
|
||||
|
||||
//TODO: implement method.
|
||||
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.
|
||||
/**
|
||||
* Returns the last element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body getLast() {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
return (last != null) ? last.body() : null;
|
||||
}
|
||||
|
||||
// Returns the first element in this list.
|
||||
// Returns 'null' if the list is empty.
|
||||
/**
|
||||
* Returns the first element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body getFirst() {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
return (first != null) ? first.body() : null;
|
||||
}
|
||||
|
||||
// Retrieves and removes the first element in this list.
|
||||
// Returns 'null' if the list is empty.
|
||||
/**
|
||||
* Retrieves and removes the first element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body pollFirst() {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
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.
|
||||
/**
|
||||
* Retrieves and removes the last element in this list.
|
||||
* Returns 'null' if the list is empty.
|
||||
*/
|
||||
public Body pollLast() {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
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().
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
//TODO: implement method.
|
||||
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++;
|
||||
}
|
||||
|
||||
// Returns the element at the specified position in this list.
|
||||
// Precondition: i >= 0 && i < 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) {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
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.
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
//TODO: implement method.
|
||||
return -2;
|
||||
}
|
||||
BodyLinkedListItem item = first;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (item.body() == body) {
|
||||
return i;
|
||||
}
|
||||
item = item.next();
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
//TODO: implement method.
|
||||
return null;
|
||||
}
|
||||
|
||||
// Returns the number of bodies in this list.
|
||||
public int size() {
|
||||
|
||||
//TODO: implement method.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ public class Simulation {
|
||||
// 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++) {
|
||||
|
@ -1,11 +1,74 @@
|
||||
// Simulates the formation of a massive solar system.
|
||||
//
|
||||
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.
|
||||
/**
|
||||
* 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();
|
||||
|
||||
//TODO: change implementation of this method according to 'Aufgabenblatt3.md'.
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import codedraw.CodeDraw;
|
||||
* This class represents vectors in a 3D vector space.
|
||||
*/
|
||||
public class Vector3 {
|
||||
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
@ -27,33 +26,21 @@ public class Vector3 {
|
||||
* 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'.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,15 +72,21 @@ public class Vector3 {
|
||||
z /= length;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,6 +94,15 @@ public class Vector3 {
|
||||
* 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);
|
||||
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