This commit is contained in:
2018-03-07 01:02:24 +01:00
parent f41d6c8ede
commit 39d628f9d4
2 changed files with 155 additions and 89 deletions

View File

@@ -35,7 +35,7 @@
defensief defensief
### orientation ### orientation
direction hoek theta (radialen) orientation hoek theta (radialen)
rechts: 0 boven: pi/2 rechts: 0 boven: pi/2
nominaal nominaal
0..2pi 0..2pi

View File

@@ -1,12 +1,15 @@
import be.kuleuven.cs.som.annotate.*;
public class Worm { public class Worm {
// region properties
//===================================================================================
// Location // Location
private double[] location; private double[] location;
// Orientation // Orientation
private double direction; private double orientation;
// shape // shape
private double radius; private double radius;
@@ -21,19 +24,26 @@ public class Worm {
// name // name
private final String name; private final String name;
public Worm(double[] location, double direction, String name, double radius) throws IllegalArgumentException { //===================================================================================
// endregion
this(location, direction, name, radius, 0.25);
// region constructor
//===================================================================================
public Worm(double[] location, double orientation, String name, double radius) throws IllegalArgumentException {
this(location, orientation, name, radius, 0.25);
} }
public Worm(double[] location, double direction, String name, double radius, double minRadius) throws IllegalArgumentException { public Worm(double[] location, double orientation, String name, double radius, double minRadius)
throws IllegalArgumentException {
if ( location.length != 2) { if ( location.length != 2) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
this.location = location; this.location = location;
this.direction = direction; this.orientation = orientation;
this.radius = radius; this.radius = radius;
this.minRadius = minRadius; this.minRadius = minRadius;
@@ -43,9 +53,123 @@ public class Worm {
} }
this.name = name; this.name = name;
} }
//===================================================================================
// endregion
// region location
//===================================================================================
public double[] getLocation() {
return this.location;
}
private void setLocation(double[] orientation) {
// TODO checking
this.location = orientation;
}
//===================================================================================
// endregion
// region Orientation
//===================================================================================
/** /**
* @param orientation
* | ...
* *
*/
public void setOrientation(double orientation) {
this.orientation = orientation;
}
public double getOrientation() {
return orientation;
}
//===================================================================================
// endregion
// region Shape mass/radius
//===================================================================================
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) throws IllegalArgumentException {
if (radius < this.minRadius) {
throw new IllegalArgumentException("Radius is smaller than " + this.minRadius);
}
this.radius = radius;
setMass(radius);
}
public double getMinRadius() {
return this.minRadius;
}
public double getMass() {
return this.mass;
}
private void setMass(double radius) {
final int rho = 1062;
double mass = rho * (4 / 3 * Math.PI * Math.pow(radius, 3));
this.mass = mass;
setMaxPoints(mass);
}
//===================================================================================
// endregion
// region Points
//===================================================================================
public int getPoints() {
return this.points;
}
public void setPoints(int points) {
if (points > this.maxPoints) {
points = this.maxPoints;
}
else if (points < 0)
points = 0;
this.points = points;
}
private void setMaxPoints(double maxPoints) {
this.maxPoints = (int) Math.round(maxPoints);
setPoints(this.points);
}
private void updatePoints (double value) {
this.points -= (int) Math.round(Math.toDegrees(value) / 2);
}
//===================================================================================
// endregion
// region name
//===================================================================================
public String getName() {
return this.name;
}
/**
* @param name ... * @param name ...
* @return ... * @return ...
*/ */
@@ -66,83 +190,22 @@ public class Worm {
return -1; return -1;
} }
public double[] getLocation() { //===================================================================================
return this.location; // endregion
}
/** // region move
* @param direction //===================================================================================
* | ...
*
*/
public void setdirection(double direction) {
this.direction = direction;
}
private void setLocation(double[] direction) {
this.location = direction;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) throws IllegalArgumentException {
if (radius < this.minRadius) {
throw new IllegalArgumentException("Radius is smaller than " + this.minRadius);
}
this.radius = radius;
setMass(radius);
}
public double getMinRadius() {
return this.minRadius;
}
public double getMass() {
return this.mass;
}
private void setMass(double radius) {
final int rho = 1062;
double mass = rho * (4 / 3 * Math.PI * Math.pow(radius, 3));
this.mass = mass;
setMaxPoints(mass);
}
public int getPoints() {
return this.points;
}
public void setPoints(int points) {
if (points > this.maxPoints) {
points = this.maxPoints;
}
else if (points < 0)
points = 0;
this.points = points;
}
public String getName() {
return this.name;
}
private void setMaxPoints(double maxPoints) {
this.maxPoints = (int) Math.round(maxPoints);
setPoints(this.points);
}
/** /**
* @param numberSteps ... * @param numberSteps ...
* the number of steps the worm should take * the number of steps the worm should take
* *
* @post the new x-coordinate schould be the old one plus the number of steps multiplied * @post the new x-coordinate schould be the old one plus the number of steps multiplied
* with the distance of a step in the x-direction * with the distance of a step in the x-orientation
* |new.CoordX = CoordX + NumberSteps * distanceX * |new.CoordX = CoordX + NumberSteps * distanceX
* @post the new y-coordinate schould be the old one plus the number of steps multiplied * @post the new y-coordinate schould be the old one plus the number of steps multiplied
* with the distance of a step in the y-direction * with the distance of a step in the y-orientation
* |new.CoordY = CoordY + NumberSteps * distanceY * |new.CoordY = CoordY + NumberSteps * distanceY
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
@@ -153,32 +216,35 @@ public class Worm {
if (numberSteps < 0) if (numberSteps < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
double distanceX = this.radius * Math.cos(this.direction); double distanceX = this.radius * Math.cos(this.orientation);
double distanceY = this.radius * Math.sin(this.direction); double distanceY = this.radius * Math.sin(this.orientation);
this.location = new double[] {this.location[0] + numberSteps * distanceX, this.location[1] + numberSteps * distanceY}; this.location = new double[] {
this.location[0] + numberSteps * distanceX,
this.location[1] + numberSteps * distanceY
};
} }
/** /**
* @param angleToAdd * @param angleToAdd
* the angle that must be added to the direction * the angle that must be added to the orientation
* *
* @pre the angle to add must be between 0 and 2pi (including 0) * @pre the angle to add must be between 0 and 2pi (including 0)
* |0 <= angleToAdd < 2pi * |0 <= angleToAdd < 2pi
* *
* @post the new direction is the old direction plus the given angle * @post the new orientation is the old orientation plus the given angle
* |new.direction = direction + angleToAdd * |new.orientation = orientation + angleToAdd
* @post the resulting angle (= the new direction) must be between 0 and 2pi (including 0) * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0)
* |0 <= new.direction < 2pi * |0 <= new.orientation < 2pi
*/ */
public void turn(double angleToAdd) { public void turn(double angleToAdd) {
assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI); assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI);
if (0 <= (this.direction + angleToAdd) && (this.direction + angleToAdd) < (2 * Math.PI)) if (0 <= (this.orientation + angleToAdd) && (this.orientation + angleToAdd) < (2 * Math.PI))
setdirection(this.direction + angleToAdd); setOrientation(this.orientation + angleToAdd);
setdirection((this.direction + angleToAdd)%(2 * Math.PI)); setOrientation((this.orientation + angleToAdd)%(2 * Math.PI));
} }
private void updatePoints (double value) { //===================================================================================
// endregion
this.points -= (int) Math.round(Math.toDegrees(value) / 2);
}
} }