AB3: BodyLinkedList working
This commit is contained in:
@ -78,7 +78,7 @@ public class Aufgabe3Test {
|
|||||||
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)), 0);
|
testValue(bfm.get(earth).distanceTo(earth.gravitationalForce(sun)), 0);
|
||||||
testValue(bfm.get(sun).distanceTo(sun.gravitationalForce(earth).plus(sun.gravitationalForce(venus))), 0);
|
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(mercury).distanceTo(mercury.gravitationalForce(sun)), 0);
|
||||||
testValue(bfm.get(earth).distanceTo(new Vector3(0, 0, 0)), 0);
|
testValue(bfm.get(earth).distanceTo(new Vector3(0, 0, 0)), 0);
|
||||||
testValue(bfm.get(mercury), mercury.gravitationalForce(sun));
|
testValue(bfm.get(mercury), mercury.gravitationalForce(sun));
|
||||||
|
|
||||||
|
@ -70,16 +70,20 @@ public class Body {
|
|||||||
return 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
|
* 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'.
|
* of the returned body is the sum of the impulses of 'this' and 'b'.
|
||||||
*/
|
*/
|
||||||
public Body merge(Body b) {
|
public Body merge(Body body) {
|
||||||
double mass = this.mass + b.mass;
|
double totalMass = this.mass + body.mass;
|
||||||
return new Body(
|
return new Body(
|
||||||
mass,
|
totalMass,
|
||||||
massCenter.times(this.mass).plus(b.massCenter.times(b.mass)).times(1.0 / mass),
|
this.massCenter.times(this.mass).plus(body.massCenter.times(body.mass)).times(1.0 / totalMass),
|
||||||
currentMovement.times(this.mass).plus(b.currentMovement.times(b.mass)).times(1.0 / mass)
|
this.currentMovement.times(this.mass).plus(body.currentMovement.times(body.mass)).times(1.0 / totalMass)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of bodies implemented as a linked list.
|
* A list of bodies implemented as a linked list.
|
||||||
* The number of elements of the list is not limited.
|
* The number of elements of the list is not limited.
|
||||||
*/
|
*/
|
||||||
public class BodyLinkedList {
|
public class BodyLinkedList implements Iterable<Body> {
|
||||||
|
private int size = 0;
|
||||||
//TODO: declare variables.
|
private BodyLinkedListItem first;
|
||||||
|
private BodyLinkedListItem last;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes 'this' as an empty list.
|
* Initializes 'this' as an empty list.
|
||||||
*/
|
*/
|
||||||
public BodyLinkedList() {
|
public BodyLinkedList() {
|
||||||
//TODO: define constructor.
|
first = null;
|
||||||
|
last = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,21 +24,38 @@ public class BodyLinkedList {
|
|||||||
* Precondition: list != null.
|
* Precondition: list != null.
|
||||||
*/
|
*/
|
||||||
public BodyLinkedList(BodyLinkedList list) {
|
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) {
|
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) {
|
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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,8 +63,7 @@ public class BodyLinkedList {
|
|||||||
* Returns 'null' if the list is empty.
|
* Returns 'null' if the list is empty.
|
||||||
*/
|
*/
|
||||||
public Body getLast() {
|
public Body getLast() {
|
||||||
//TODO: implement method.
|
return (last != null) ? last.body() : null;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,8 +71,7 @@ public class BodyLinkedList {
|
|||||||
* Returns 'null' if the list is empty.
|
* Returns 'null' if the list is empty.
|
||||||
*/
|
*/
|
||||||
public Body getFirst() {
|
public Body getFirst() {
|
||||||
//TODO: implement method.
|
return (first != null) ? first.body() : null;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,8 +79,14 @@ public class BodyLinkedList {
|
|||||||
* Returns 'null' if the list is empty.
|
* Returns 'null' if the list is empty.
|
||||||
*/
|
*/
|
||||||
public Body pollFirst() {
|
public Body pollFirst() {
|
||||||
//TODO: implement method.
|
if (first == null) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
Body b = first.body();
|
||||||
|
first = first.next();
|
||||||
|
if (first != null) first.setPrev(null);
|
||||||
|
size--;
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,8 +94,14 @@ public class BodyLinkedList {
|
|||||||
* Returns 'null' if the list is empty.
|
* Returns 'null' if the list is empty.
|
||||||
*/
|
*/
|
||||||
public Body pollLast() {
|
public Body pollLast() {
|
||||||
//TODO: implement method.
|
if (last == null) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
Body b = last.body();
|
||||||
|
last = last.prev();
|
||||||
|
if (last != null) last.setNext(null);
|
||||||
|
size--;
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,7 +109,36 @@ public class BodyLinkedList {
|
|||||||
* Precondition: i >= 0 && i <= size().
|
* Precondition: i >= 0 && i <= size().
|
||||||
*/
|
*/
|
||||||
public void add(int i, Body body) {
|
public void add(int i, Body body) {
|
||||||
//TODO: implement method.
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,8 +146,19 @@ public class BodyLinkedList {
|
|||||||
* Precondition: i >= 0 && i < size().
|
* Precondition: i >= 0 && i < size().
|
||||||
*/
|
*/
|
||||||
public Body get(int i) {
|
public Body get(int i) {
|
||||||
//TODO: implement method.
|
BodyLinkedListItem item;
|
||||||
return null;
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,8 +166,19 @@ public class BodyLinkedList {
|
|||||||
* this list does not contain the element.
|
* this list does not contain the element.
|
||||||
*/
|
*/
|
||||||
public int indexOf(Body body) {
|
public int indexOf(Body body) {
|
||||||
//TODO: implement method.
|
if (first == null) {
|
||||||
return -2;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
BodyLinkedListItem item = first;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (item.body() == body) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
item = item.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,15 +186,42 @@ public class BodyLinkedList {
|
|||||||
* body. Returns a list with all the removed bodies.
|
* body. Returns a list with all the removed bodies.
|
||||||
*/
|
*/
|
||||||
public BodyLinkedList removeCollidingWith(Body body) {
|
public BodyLinkedList removeCollidingWith(Body body) {
|
||||||
//TODO: implement method.
|
BodyLinkedList removed = new BodyLinkedList();
|
||||||
return null;
|
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.
|
* Returns the number of bodies in this list.
|
||||||
*/
|
*/
|
||||||
public int size() {
|
public int size() {
|
||||||
//TODO: implement method.
|
return size;
|
||||||
return -1;
|
}
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
src/BodyLinkedListItem.java
Normal file
33
src/BodyLinkedListItem.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
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
|
// merge bodies that have collided
|
||||||
for (int i = 0; i < bodies.length; i++) {
|
for (int i = 0; i < bodies.length; i++) {
|
||||||
for (int j = i + 1; j < bodies.length; j++) {
|
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]);
|
bodies[i] = bodies[i].merge(bodies[j]);
|
||||||
Body[] bodiesOneRemoved = new Body[bodies.length - 1];
|
Body[] bodiesOneRemoved = new Body[bodies.length - 1];
|
||||||
for (int k = 0; k < bodiesOneRemoved.length; k++) {
|
for (int k = 0; k < bodiesOneRemoved.length; k++) {
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
import codedraw.CodeDraw;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simulates the formation of a massive solar system.
|
* Simulates the formation of a massive solar system.
|
||||||
*/
|
*/
|
||||||
@ -7,6 +12,63 @@ 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) {
|
public static void main(String[] args) {
|
||||||
//TODO: change implementation of this method according to 'Aufgabenblatt3.md'.
|
CodeDraw cd = new CodeDraw();
|
||||||
|
BodyLinkedList bodies = new BodyLinkedList();
|
||||||
|
BodyForceMap forceOnBody = new BodyForceMap();
|
||||||
|
|
||||||
|
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.
|
* This class represents vectors in a 3D vector space.
|
||||||
*/
|
*/
|
||||||
public class Vector3 {
|
public class Vector3 {
|
||||||
|
|
||||||
private double x;
|
private double x;
|
||||||
private double y;
|
private double y;
|
||||||
private double z;
|
private double z;
|
||||||
@ -27,33 +26,21 @@ public class Vector3 {
|
|||||||
* Returns the sum of this vector and vector 'v'.
|
* Returns the sum of this vector and vector 'v'.
|
||||||
*/
|
*/
|
||||||
public Vector3 plus(Vector3 v) {
|
public Vector3 plus(Vector3 v) {
|
||||||
Vector3 result = new Vector3();
|
return new Vector3(x + v.x, y + v.y, z + v.z);
|
||||||
result.x = x + v.x;
|
|
||||||
result.y = y + v.y;
|
|
||||||
result.z = z + v.z;
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the product of this vector and 'd'.
|
* Returns the product of this vector and 'd'.
|
||||||
*/
|
*/
|
||||||
public Vector3 times(double d) {
|
public Vector3 times(double d) {
|
||||||
Vector3 result = new Vector3();
|
return new Vector3(x * d, y * d, z * d);
|
||||||
result.x = x * d;
|
|
||||||
result.y = y * d;
|
|
||||||
result.z = z * d;
|
|
||||||
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) {
|
public Vector3 minus(Vector3 v) {
|
||||||
Vector3 result = new Vector3();
|
return new Vector3(x - v.x, y - v.y, z - v.z);
|
||||||
result.x = x - v.x;
|
|
||||||
result.y = y - v.y;
|
|
||||||
result.z = z - v.z;
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,15 +72,21 @@ public class Vector3 {
|
|||||||
z /= length;
|
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
|
* 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.
|
* in the canvas associated with 'cd'. The z-coordinate is not used.
|
||||||
*/
|
*/
|
||||||
public void drawAsFilledCircle(CodeDraw cd, double radius) {
|
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;
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user