Skip to content

Commit

Permalink
Update new Method Names on Point based on Discussion with Brett.
Browse files Browse the repository at this point in the history
Changed Torus Wrapping Method to be Simpler -> getClosestMappedPoint
  • Loading branch information
hawkerm committed May 5, 2016
1 parent 3f05d97 commit 0a4865e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 72 deletions.
2 changes: 1 addition & 1 deletion SBA_Serv/buildnum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1112
1113
8 changes: 4 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ v1.2 : Planned - May 2016 [Season 5]
* Movement direction can still be a double
* Added Test Cases for Point Class
* Fixed **getAngleTo** method to use built-in atan2
* Added **closeTo** box test method
* Added **getPointFromAngleAndDistance** for point projections
* Added **inEllipse** check method
* Added **getPointsOnTorus** for world bounds assistance
* Added **isCloseTo** box test method
* Added **getPointAt** for point projections based on angles and distance
* Added **isInEllipse** check method
* Added **getClosestMappedPoint** for world bounds assistance
* Added option for Torpedoes to be effected by gravity
* Split 'explodable' from 'gravitable' for Entities, two separate object flags now.
* Separated option for 'showip' in Application settings to decouple from showing statistics, no longer always show IP in Debug mode.
Expand Down
67 changes: 37 additions & 30 deletions java_client_src/src/ihs/apcs/spacebattle/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ public int getAngleTo(Point other) {
*
* @since 1.2
*/
public Point getPointFromAngleAndDistance(double angle, double distance)
{
public Point getPointAt(double angle, double distance) {
return new Point(this.getX() + distance * Math.cos(Math.toRadians(angle)),
this.getY() - distance * Math.sin(Math.toRadians(angle)));
}

/**
* Checks if this point is in an Ellipse with the given center point, major/minor axis lengths and orientation.
* Checks if this point is in an Ellipse with the given center point, major/minor axis lengths at the given orientation.
*
* @param center of the ellipse
* @param major axis length
Expand All @@ -103,7 +102,7 @@ public Point getPointFromAngleAndDistance(double angle, double distance)
*
* @since 1.2
*/
public boolean inEllipse(Point center, int major, int minor, int orientation)
public boolean isInEllipse(Point center, int major, int minor, int orientation)
{
double xDiff = this.getX() - center.getX();
double yDiff = -1 * (this.getY() - center.getY()); // Our coordinates are flipped compared to regular math functions.
Expand All @@ -116,42 +115,51 @@ public boolean inEllipse(Point center, int major, int minor, int orientation)
}

/**
* Wraps this point on a torus of size width, height if the point is within the given tolerance of the edge of the Torus.
* Maps the other point across world boundaries to return the closest form of the given point.
*
* Note: This point may map outside the bounds of the world, it should only be used for orientation purposes and distance only.
*
* @param other point to map from this one
* @param width of the world
* @param height of the world
* @return form of the point that is closest to this one
*
* @since 1.2
*/
public Point getClosestMappedPoint(Point other, int width, int height)
{
Point closest = null;
for (Point p : other.getPointsOnTorus(width, height)) {
if (closest == null || this.getDistanceTo(p) < this.getDistanceTo(closest)) {
closest = p;
}
}

return closest == null ? other : closest;
}

/**
* Wraps this point on a torus of size width, height of the edge of the Torus.
*
* Returns a List of points (including this one) which represent the coordinates of this point projected beyond the bounds of the 'world' on a Torus.
* This can be used to determine if a point is actually close to a position when it exists beyond the bounds of the 'world'.
*
* @param width of torus
* @param height of torus
* @param tolerance to wrap coordinates
* @return set of points projected beyond bounds of torus
*
* @since 1.2
*/
public List<Point> getPointsOnTorus(int width, int height, int tolerance)
{
private List<Point> getPointsOnTorus(int width, int height) {
List<Point> points = new ArrayList<Point>();
points.add(new Point(this.getX(), this.getY()));

if (this.getX() < tolerance) {
if (this.getY() < tolerance) {
points.add(new Point(this.getX() + width, this.getY() + height));
}

points.add(new Point(this.getX() + width, this.getY()));
} else if (this.getX() > width - tolerance) {
if (this.getY() > height - tolerance) {
points.add(new Point(this.getX() - width, this.getY() - height));
}

points.add(new Point(this.getX() - width, this.getY()));
}

if (this.getY() < tolerance) {
points.add(new Point(this.getX(), this.getY() + height));
} else if (this.getY() > height - tolerance) {
points.add(new Point(this.getX(), this.getY() - height));
}
points.add(new Point(this.getX(), this.getY()));
points.add(new Point(this.getX() + width, this.getY() + height));
points.add(new Point(this.getX() + width, this.getY()));
points.add(new Point(this.getX(), this.getY() + height));
points.add(new Point(this.getX() - width, this.getY() - height));
points.add(new Point(this.getX() - width, this.getY()));
points.add(new Point(this.getX(), this.getY() - height));

return points;
}
Expand All @@ -166,8 +174,7 @@ public List<Point> getPointsOnTorus(int width, int height, int tolerance)
*
* @since 1.2
*/
public boolean closeTo(Point other, double tolerance)
{
public boolean isCloseTo(Point other, double tolerance) {
return (Math.abs(this.getX() - other.getX()) <= tolerance &&
Math.abs(this.getY() - other.getY()) <= tolerance);
}
Expand Down
76 changes: 39 additions & 37 deletions java_client_src/test/ihs/apcs/spacebattle/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,61 +38,63 @@ public void testGetAngleTo() {

@Test
public void testGetPointFromAngleAndDistance() {
assertTrue((new Point(0, 0)).getPointFromAngleAndDistance(45, 10).closeTo(new Point(7.07, -7.07), 0.1));
assertTrue((new Point(0, 0)).getPointFromAngleAndDistance(135, 10).closeTo(new Point(-7.07, -7.07), 0.1));
assertTrue((new Point(0, 0)).getPointFromAngleAndDistance(-45, 10).closeTo(new Point(7.07, 7.07), 0.1));
assertTrue((new Point(0, 0)).getPointAt(45, 10).isCloseTo(new Point(7.07, -7.07), 0.1));
assertTrue((new Point(0, 0)).getPointAt(135, 10).isCloseTo(new Point(-7.07, -7.07), 0.1));
assertTrue((new Point(0, 0)).getPointAt(-45, 10).isCloseTo(new Point(7.07, 7.07), 0.1));

for (int i = 0; i < 360; i++)
{
Point zero = new Point(0, 0);
Point loc = zero.getPointFromAngleAndDistance(i, 1);
assertTrue(loc.closeTo(new Point(Math.cos(Math.toRadians(i)), -Math.sin(Math.toRadians(i))), 0.05));
Point loc = zero.getPointAt(i, 1);
assertTrue(loc.isCloseTo(new Point(Math.cos(Math.toRadians(i)), -Math.sin(Math.toRadians(i))), 0.05));
}
}

@Test
public void testInEllipse() {
public void testIsInEllipse() {
// Test Basic Points Edges in Standard Ellipse
assertTrue((new Point(0, 1)).inEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(0, -1)).inEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(0, -1.01)).inEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(0, 0)).inEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(4, 1)).inEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(4, 0)).inEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(-4, 0)).inEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(-4.01, 0)).inEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(0, 1)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(0, -1)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(0, -1.01)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(0, 0)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(4, 1)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(4, 0)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertTrue((new Point(-4, 0)).isInEllipse(new Point(0, 0), 4, 1, 0));
assertFalse((new Point(-4.01, 0)).isInEllipse(new Point(0, 0), 4, 1, 0));

// Test Rotation
assertFalse((new Point(-1, -1)).inEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(1, -1)).inEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(-1, 1)).inEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(1, 1)).inEllipse(new Point(0, 0), 2, 1, 0));
assertTrue((new Point(1, 1)).inEllipse(new Point(0, 0), 2, 1, -45));
assertFalse((new Point(1, -1)).inEllipse(new Point(0, 0), 2, 1, -45));
assertTrue((new Point(-1, -1)).inEllipse(new Point(0, 0), 2, 1, -45));
assertFalse((new Point(1, 1)).inEllipse(new Point(0, 0), 2, 1, 45));
assertFalse((new Point(-1, -1)).isInEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(1, -1)).isInEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(-1, 1)).isInEllipse(new Point(0, 0), 2, 1, 0));
assertFalse((new Point(1, 1)).isInEllipse(new Point(0, 0), 2, 1, 0));
assertTrue((new Point(1, 1)).isInEllipse(new Point(0, 0), 2, 1, -45));
assertFalse((new Point(1, -1)).isInEllipse(new Point(0, 0), 2, 1, -45));
assertTrue((new Point(-1, -1)).isInEllipse(new Point(0, 0), 2, 1, -45));
assertFalse((new Point(1, 1)).isInEllipse(new Point(0, 0), 2, 1, 45));
}

@Test
public void testGetPointsOnTorus() {
// TODO: deal with checking actual results
List<Point> points = (new Point(5.0, 5.0)).getPointsOnTorus(200, 100, 10);
public void testGetClosestMappedPoint() {
Point me = new Point(5, 5);

assertEquals(4, points.size());
assertTrue((new Point(-5, 5)).isCloseTo(me.getClosestMappedPoint(new Point(95, 5), 100, 100), 0.1));
assertEquals(10, me.getDistanceTo(me.getClosestMappedPoint(new Point(95, 5), 100, 100)), 0.1);

System.out.println(points);
assertTrue((new Point(-5, -5)).isCloseTo(me.getClosestMappedPoint(new Point(95, 95), 100, 100), 0.1));

points = (new Point(15.0, 5.0)).getPointsOnTorus(200, 100, 10);
assertTrue((new Point(95, 5)).isCloseTo(me.getClosestMappedPoint(new Point(95, 5), 200, 100), 0.1));

assertEquals(2, points.size());
me = new Point(50, 10);

System.out.println(points);
assertTrue((new Point(95, 5)).isCloseTo(me.getClosestMappedPoint(new Point(95, 5), 100, 100), 0.1));

points = (new Point(15.0, 15.0)).getPointsOnTorus(200, 100, 10);

assertEquals(1, points.size());
assertTrue((new Point(50, -35)).isCloseTo(me.getClosestMappedPoint(new Point(50, 65), 100, 100), 0.1));
assertTrue((new Point(50, 55)).isCloseTo(me.getClosestMappedPoint(new Point(50, 55), 100, 100), 0.1));

me = new Point(95, 90);

System.out.println(points);
assertTrue((new Point(90, 75)).isCloseTo(me.getClosestMappedPoint(new Point(90, 75), 100, 100), 0.1));
assertTrue((new Point(110, 120)).isCloseTo(me.getClosestMappedPoint(new Point(10, 20), 100, 100), 0.1));
}

@Test
Expand All @@ -107,12 +109,12 @@ public void testEqualsPoint() {
}

@Test
public void testCloseTo()
public void testIsCloseTo()
{
Point one = new Point(25.1, 49.9);
Point two = new Point(24.9, 50.1);

assertTrue(one.closeTo(two, 0.20001));
assertFalse(one.closeTo(two, 0.1));
assertTrue(one.isCloseTo(two, 0.20001));
assertFalse(one.isCloseTo(two, 0.1));
}
}

0 comments on commit 0a4865e

Please sign in to comment.