Added Mock.Angle, Mock.Azimith, Mock.Bearing. These encapsulate the relevant angle type, to allow for better type safety (e.g., to avoid passing a bearing into an azimuth function).
Added more comments to Mock.Boat. Moved calculateAzimuth to GPSCoordinate, from Boat.main
parent
60458f0f7a
commit
cc54c92d5b
@ -0,0 +1,65 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents an angle.
|
||||||
|
* Has functions to return angle as either degrees or radians.
|
||||||
|
*/
|
||||||
|
public class Angle {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The angle stored in this object.
|
||||||
|
* Degrees.
|
||||||
|
*/
|
||||||
|
private double degrees;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctor.
|
||||||
|
* Don't use this.
|
||||||
|
* This is protected because you need to use the static helper functions {@link #fromDegrees(double)} and {@link #fromRadians(double)} to construct an Angle object.
|
||||||
|
*
|
||||||
|
* @param degrees The value, in degrees, to initialize this Angle object with.
|
||||||
|
*/
|
||||||
|
protected Angle(double degrees) {
|
||||||
|
this.degrees = degrees;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an Angle object from an angle value in degrees.
|
||||||
|
* @param degrees Angle value in degrees.
|
||||||
|
* @return Angle object.
|
||||||
|
*/
|
||||||
|
public static Angle fromDegrees(double degrees) {
|
||||||
|
Angle angle = new Angle(degrees);
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an Angle object from an angle value in radians.
|
||||||
|
* @param radians Angle value in radians.
|
||||||
|
* @return Angle object.
|
||||||
|
*/
|
||||||
|
public static Angle fromRadians(double radians) {
|
||||||
|
return Angle.fromDegrees(Math.toDegrees(radians));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of this Angle object, in degrees.
|
||||||
|
* @return The value of this Angle object, in degrees.
|
||||||
|
*/
|
||||||
|
public double degrees() {
|
||||||
|
return this.degrees;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of this Angle object, in radians.
|
||||||
|
* @return The value of this Angle object, in radians.
|
||||||
|
*/
|
||||||
|
public double radians() {
|
||||||
|
return Math.toRadians(this.degrees);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an azimuth.
|
||||||
|
* This is the angle between north and a target point.
|
||||||
|
* It has the interval [-180, 180), and clockwise from north is positive.
|
||||||
|
*/
|
||||||
|
public class Azimuth extends Angle{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctor.
|
||||||
|
* This is protected because you need to use the static helper functions {@link #fromDegrees(double)} and {@link #fromRadians(double)} to construct an Azimuth object.
|
||||||
|
*
|
||||||
|
* @param degrees The value, in degrees, to initialize this Azimuth object with.
|
||||||
|
*/
|
||||||
|
protected Azimuth(double degrees) {
|
||||||
|
super(degrees);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an angle in degrees into an angle in degrees in the correct interval for an azimuth - [-180, 180).
|
||||||
|
* E.g., converts -183 to 177, or converts 250 to -110, or converts 180 to -180.
|
||||||
|
* @param degrees Degree value to convert.
|
||||||
|
* @return Degree value in interval [-180, 180).
|
||||||
|
*/
|
||||||
|
public static double toAzimuthInterval(double degrees) {
|
||||||
|
|
||||||
|
if (degrees >= 180) {
|
||||||
|
//Too large.
|
||||||
|
degrees -= 360;
|
||||||
|
} else if (degrees < -180) {
|
||||||
|
//Too small.
|
||||||
|
degrees += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
return degrees;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an Azimuth object from an angle value in degrees.
|
||||||
|
* @param degrees Azimuth value in degrees.
|
||||||
|
* @return Azimuth object.
|
||||||
|
*/
|
||||||
|
public static Azimuth fromDegrees(double degrees) {
|
||||||
|
//Ensure the angle is in the correct interval.
|
||||||
|
double degreesInInterval = Azimuth.toAzimuthInterval(degrees);
|
||||||
|
|
||||||
|
Azimuth azimuth = new Azimuth(degreesInInterval);
|
||||||
|
return azimuth;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an Azimuth object from an angle value in radians.
|
||||||
|
* @param radians Azimuth value in radians.
|
||||||
|
* @return Azimuth object.
|
||||||
|
*/
|
||||||
|
public static Azimuth fromRadians(double radians) {
|
||||||
|
return Azimuth.fromDegrees(Math.toDegrees(radians));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a bearing. Also known as a heading.
|
||||||
|
* This is the angle between north and a target point.
|
||||||
|
* Has the interval [0, 360), and clockwise from north is positive.
|
||||||
|
*/
|
||||||
|
public class Bearing extends Angle {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctor.
|
||||||
|
* This is protected because you need to use the static helper functions {@link #fromDegrees(double)} and {@link #fromRadians(double)} to construct a Bearing object.
|
||||||
|
*
|
||||||
|
* @param degrees The value, in degrees, to initialize this Bearing object with.
|
||||||
|
*/
|
||||||
|
protected Bearing(double degrees) {
|
||||||
|
super(degrees);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an angle in degrees into an angle in degrees in the correct interval for a bearing - [0, 360).
|
||||||
|
* E.g., converts -183 to 177, or converts 425 to 65.
|
||||||
|
* @param degrees Degree value to convert.
|
||||||
|
* @return Degree value in interval [0, 360).
|
||||||
|
*/
|
||||||
|
public static double toBearingInterval(double degrees) {
|
||||||
|
|
||||||
|
if (degrees >= 360) {
|
||||||
|
//Too large.
|
||||||
|
degrees -= 360;
|
||||||
|
} else if (degrees < 0) {
|
||||||
|
//Too small.
|
||||||
|
degrees += 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
return degrees;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a Bearing object from an angle value in degrees.
|
||||||
|
* @param degrees Bearing value in degrees.
|
||||||
|
* @return Bearing object.
|
||||||
|
*/
|
||||||
|
public static Bearing fromDegrees(double degrees) {
|
||||||
|
//Ensure the angle is in the correct interval.
|
||||||
|
double degreesInInterval = Bearing.toBearingInterval(degrees);
|
||||||
|
|
||||||
|
Bearing bearing = new Bearing(degreesInInterval);
|
||||||
|
return bearing;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a Bearing object from an angle value in radians.
|
||||||
|
* @param radians Bearing value in radians.
|
||||||
|
* @return Bearing object.
|
||||||
|
*/
|
||||||
|
public static Bearing fromRadians(double radians) {
|
||||||
|
return Bearing.fromDegrees(Math.toDegrees(radians));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue