diff --git a/docs/samenvatting.md b/docs/samenvatting.md index 76e61a4..47581da 100644 --- a/docs/samenvatting.md +++ b/docs/samenvatting.md @@ -35,7 +35,7 @@ defensief ### orientation - direction hoek theta (radialen) + orientation hoek theta (radialen) rechts: 0 boven: pi/2 nominaal 0..2pi diff --git a/src/Worm.java b/src/Worm.java index 7ad8be8..8b3c858 100644 --- a/src/Worm.java +++ b/src/Worm.java @@ -1,12 +1,15 @@ -import be.kuleuven.cs.som.annotate.*; - public class Worm { + + // region properties + //=================================================================================== + + // Location private double[] location; // Orientation - private double direction; + private double orientation; // shape private double radius; @@ -21,19 +24,26 @@ public class Worm { // 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) { throw new IllegalArgumentException(); } this.location = location; - this.direction = direction; + this.orientation = orientation; this.radius = radius; this.minRadius = minRadius; @@ -43,9 +53,123 @@ public class Worm { } 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 ... * @return ... */ @@ -66,83 +190,22 @@ public class Worm { return -1; } - public double[] getLocation() { - return this.location; - } + //=================================================================================== + // endregion - /** - * @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); - } + // region move + //=================================================================================== /** * @param numberSteps ... * the number of steps the worm should take * * @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 * @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 * * @throws IllegalArgumentException @@ -153,32 +216,35 @@ public class Worm { if (numberSteps < 0) throw new IllegalArgumentException(); - double distanceX = this.radius * Math.cos(this.direction); - double distanceY = this.radius * Math.sin(this.direction); - this.location = new double[] {this.location[0] + numberSteps * distanceX, this.location[1] + numberSteps * distanceY}; + double distanceX = this.radius * Math.cos(this.orientation); + double distanceY = this.radius * Math.sin(this.orientation); + this.location = new double[] { + this.location[0] + numberSteps * distanceX, + this.location[1] + numberSteps * distanceY + }; } /** * @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) * |0 <= angleToAdd < 2pi * - * @post the new direction is the old direction plus the given angle - * |new.direction = direction + angleToAdd - * @post the resulting angle (= the new direction) must be between 0 and 2pi (including 0) - * |0 <= new.direction < 2pi + * @post the new orientation is the old orientation plus the given angle + * |new.orientation = orientation + angleToAdd + * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) + * |0 <= new.orientation < 2pi */ public void turn(double angleToAdd) { assert 0 <= angleToAdd && angleToAdd < (2 * Math.PI); - if (0 <= (this.direction + angleToAdd) && (this.direction + angleToAdd) < (2 * Math.PI)) - setdirection(this.direction + angleToAdd); - setdirection((this.direction + angleToAdd)%(2 * Math.PI)); + if (0 <= (this.orientation + angleToAdd) && (this.orientation + angleToAdd) < (2 * Math.PI)) + setOrientation(this.orientation + angleToAdd); + setOrientation((this.orientation + angleToAdd)%(2 * Math.PI)); } - private void updatePoints (double value) { + //=================================================================================== + // endregion + - this.points -= (int) Math.round(Math.toDegrees(value) / 2); - } }