Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement equals() and hasCode() methods in Direction subclasses and …

…add rotate(float)
  • Loading branch information...
commit b27e57b9dcfd02f42737d1601953a81bb71a91ab 1 parent 57809c6
aumgn authored
7 core/src/main/java/fr/aumgn/bukkitutils/geom/Direction.java
View
@@ -10,6 +10,8 @@
Vector getVector();
+ Direction rotate(float angle);
+
public static final Direction NONE = new Direction() {
@Override
public Vector2D getVector2D() {
@@ -30,5 +32,10 @@ public float getYaw() {
public float getPitch() {
return Float.NaN;
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return NONE;
+ }
};
}
75 core/src/main/java/fr/aumgn/bukkitutils/geom/Face.java
View
@@ -1,30 +1,32 @@
package fr.aumgn.bukkitutils.geom;
-public enum Face implements Direction {
+import fr.aumgn.bukkitutils.geom.direction.LocationDirection;
- UP ( 0.0f, 180.0f, 0, 1, 0),
- DOWN ( 0.0f, -180.0f, 0, -1, 0),
+public final class Face implements Direction {
- NORTH ( 90.0f, 0.0f, -1, 0, 0),
- EAST (180.0f, 0.0f, 0, 0, -1),
- SOUTH (270.0f, 0.0f, 1, 0, 0),
- WEST ( 0.0f, 0.0f, 0, 0, 1),
+ public static final Face UP = new Face( 0.0f, 180.0f, 0, 1, 0);
+ public static final Face DOWN = new Face( 0.0f, -180.0f, 0, -1, 0);
- UP_NORTH ( 90.0f, 180.0f, -1, 1, 0),
- UP_EAST (180.0f, 180.0f, 0, 1, -1),
- UP_SOUTH (270.0f, 180.0f, 1, 1, 0),
- UP_WEST ( 0.0f, 180.0f, 0, 1, 1),
+ public static final Face NORTH = new Face( 90.0f, 0.0f, -1, 0, 0);
+ public static final Face EAST = new Face(180.0f, 0.0f, 0, 0, -1);
+ public static final Face SOUTH = new Face(270.0f, 0.0f, 1, 0, 0);
+ public static final Face WEST = new Face( 0.0f, 0.0f, 0, 0, 1);
- DOWN_NORTH ( 90.0f, -180.0f, -1, -1, 0),
- DOWN_EAST (180.0f, -180.0f, 0, -1, -1),
- DOWN_SOUTH (270.0f, -180.0f, 1, -1, 0),
- DOWN_WEST ( 0.0f, -180.0f, 0, -1, 1),
+ public static final Face UP_NORTH = new Face( 90.0f, 180.0f, -1, 1, 0);
+ public static final Face UP_EAST = new Face(180.0f, 180.0f, 0, 1, -1);
+ public static final Face UP_SOUTH = new Face(270.0f, 180.0f, 1, 1, 0);
+ public static final Face UP_WEST = new Face( 0.0f, 180.0f, 0, 1, 1);
- NORTH_EAST (135.0f, 0.0f, -1, 0, -1),
- NORTH_WEST ( 45.0f, 0.0f, -1, 0, 1),
+ public static final Face DOWN_NORTH = new Face( 90.0f, -180.0f, -1, -1, 0);
+ public static final Face DOWN_EAST = new Face(180.0f, -180.0f, 0, -1, -1);
+ public static final Face DOWN_SOUTH = new Face(270.0f, -180.0f, 1, -1, 0);
+ public static final Face DOWN_WEST = new Face( 0.0f, -180.0f, 0, -1, 1);
- SOUTH_EAST (225.0f, 0.0f, 1, 0, -1),
- SOUTH_WEST (315.0f, 0.0f, 1, 0, 1);
+ public static final Face NORTH_EAST = new Face(135.0f, 0.0f, -1, 0, -1);
+ public static final Face NORTH_WEST = new Face( 45.0f, 0.0f, -1, 0, 1);
+
+ public static final Face SOUTH_EAST = new Face(225.0f, 0.0f, 1, 0, -1);
+ public static final Face SOUTH_WEST = new Face(315.0f, 0.0f, 1, 0, 1);
private final float yaw;
private final float pitch;
@@ -59,4 +61,39 @@ public Vector2D getVector2D() {
public Vector getVector() {
return new Vector(x, y, z);
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return new LocationDirection(yaw, pitch).rotate(angle);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(yaw);
+ result = prime * result + Float.floatToIntBits(pitch);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Direction)) {
+ return false;
+ }
+
+ Direction other = (Direction) obj;
+ if (yaw != other.getYaw()) {
+ return false;
+ }
+ if (pitch != other.getPitch()) {
+ return false;
+ }
+
+ return true;
+ }
}
40 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/HorizontalDirection.java
View
@@ -12,6 +12,11 @@ public static Vector2D calculateVector2D(float yaw) {
Math.sin(radians), Math.cos(radians));
}
+ public static Direction calculateHorizontalRotation(Direction dir, float angle) {
+ float oppositeYaw = (dir.getYaw() + 180f) % 360f;
+ return new HorizontalDirection(oppositeYaw);
+ }
+
protected final float yaw;
private Vector2D vector;
@@ -41,4 +46,39 @@ public Vector2D getVector2D() {
public Vector getVector() {
return new Vector();
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return calculateHorizontalRotation(this, angle);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(yaw);
+ result = prime * result;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Direction)) {
+ return false;
+ }
+
+ Direction other = (Direction) obj;
+ if (yaw != other.getYaw()) {
+ return false;
+ }
+ if (0f != other.getPitch()) {
+ return false;
+ }
+
+ return true;
+ }
}
42 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/LocationDirection.java
View
@@ -2,6 +2,7 @@
import org.bukkit.Location;
+import fr.aumgn.bukkitutils.geom.Direction;
import fr.aumgn.bukkitutils.geom.Vector;
public class LocationDirection extends HorizontalDirection {
@@ -10,6 +11,12 @@ public static double calculateVectorY(float yaw, float pitch) {
return 0.0;
}
+ public static Direction calculateRotation(Direction dir, float angle) {
+ float oppositeYaw = (dir.getYaw() + 180f) % 360f;
+ float oppositePitch = (dir.getPitch() + 180f + angle) % 360f - 180f;
+ return new LocationDirection(oppositeYaw, oppositePitch);
+ }
+
private final float pitch;
private double y;
@@ -37,4 +44,39 @@ public double getVectorY() {
public Vector getVector() {
return getVector2D().to3D(getVectorY());
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return calculateRotation(this, angle);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(yaw);
+ result = prime * result + Float.floatToIntBits(pitch);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Direction)) {
+ return false;
+ }
+
+ Direction other = (Direction) obj;
+ if (yaw != other.getYaw()) {
+ return false;
+ }
+ if (pitch != other.getPitch()) {
+ return false;
+ }
+
+ return true;
+ }
}
28 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/Vector2DDirection.java
View
@@ -52,4 +52,32 @@ public Vector2D getVector2D() {
public Vector getVector() {
return vector.to3D();
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return HorizontalDirection.calculateHorizontalRotation(this, angle);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(getYaw());
+ result = prime * result + Float.floatToIntBits(getPitch());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Direction)) {
+ return false;
+ }
+
+ Direction other = (Direction) obj;
+ return vector.to3D().equals(other.getVector());
+ }
}
28 core/src/main/java/fr/aumgn/bukkitutils/geom/direction/VectorDirection.java
View
@@ -54,4 +54,32 @@ public Vector2D getVector2D() {
public Vector getVector() {
return vector;
}
+
+ @Override
+ public Direction rotate(float angle) {
+ return LocationDirection.calculateRotation(this, angle);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(getYaw());
+ result = prime * result + Float.floatToIntBits(getPitch());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof Direction)) {
+ return false;
+ }
+
+ Direction other = (Direction) obj;
+ return vector.equals(other.getVector());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.