Initial revision
[old-projects.git] / ekit / com / hexidec / util / Cartesian.java
diff --git a/ekit/com/hexidec/util/Cartesian.java b/ekit/com/hexidec/util/Cartesian.java
new file mode 100644 (file)
index 0000000..3a5444d
--- /dev/null
@@ -0,0 +1,137 @@
+package com.hexidec.util;
+
+public class Cartesian
+{
+       private int x;
+       private int y;
+
+       public Cartesian(int x, int y)
+       {
+               this.x = x;
+               this.y = y;
+       }
+
+       public Cartesian(Cartesian cartesian)
+       {
+               this.x = cartesian.getX();
+               this.y = cartesian.getY();
+       }
+
+       public Cartesian()
+       {
+               this(0, 0);
+       }
+
+       public int getX() { return x; }
+       public int getY() { return y; }
+
+       public void setX(int i) { x = i; }
+       public void setY(int i) { y = i; }
+
+       public void mirror(Cartesian cpSource)
+       {
+               this.setX(cpSource.getX());
+               this.setY(cpSource.getY());
+       }
+
+       public void add(Cartesian cpAdd)
+       {
+               this.setX(this.getX() + cpAdd.getX());
+               this.setY(this.getY() + cpAdd.getY());
+       }
+
+       public void subtract(Cartesian cpSub)
+       {
+               this.setX(this.getX() - cpSub.getX());
+               this.setY(this.getY() - cpSub.getY());
+       }
+
+       public void scale(int magnitude)
+       {
+               this.setX(this.getX() * magnitude);
+               this.setY(this.getY() * magnitude);
+       }
+
+       public Cartesian getScaledInstance(int magnitude)
+       {
+               Cartesian cpNew = this.dupe();
+               cpNew.scale(magnitude);
+               return cpNew;
+       }
+
+       public Cartesian getScaledInstance(Cartesian cpMagnitude)
+       {
+               Cartesian cpNew = this.dupe();
+               cpNew.setX(cpNew.getX() * cpMagnitude.getX());
+               cpNew.setY(cpNew.getY() * cpMagnitude.getY());
+               return cpNew;
+       }
+
+       public Cartesian addToLimit(Cartesian cpBase, Cartesian cpMin, Cartesian cpMax)
+       {
+               Cartesian cpNew = this.dupe();
+               cpNew.add(cpBase);
+               if(cpNew.getX() < cpMin.getX()) { cpNew.setX(cpMin.getX()); }
+               if(cpNew.getY() < cpMin.getY()) { cpNew.setY(cpMin.getY()); }
+               if(cpNew.getX() > cpMax.getX()) { cpNew.setX(cpMax.getX()); }
+               if(cpNew.getY() > cpMax.getY()) { cpNew.setY(cpMax.getY()); }
+               return cpNew;
+       }
+
+       public Cartesian translate(Cartesian cpOffset, boolean unknown)
+       {
+               Cartesian cpNew = this.dupe();
+               cpNew.add(cpOffset);
+               return cpNew;
+       }
+
+       public Cartesian translate(Cartesian cpOffset)
+       {
+               return translate(cpOffset, false);
+       }
+
+       public double distance(Cartesian cpTarget)
+       {
+               // Formula -> SQRT(SQR(x2 - x1) + SQR(y2 - y1))
+               int xDiff = cpTarget.getX() - this.getX();
+               int yDiff = cpTarget.getY() - this.getY();
+               return Math.sqrt((xDiff * xDiff) + (yDiff * yDiff));
+       }
+
+       public Cartesian midpoint(Cartesian cpTarget)
+       {
+               // Formula -> (x1 + x2) / 2, (y1 + y2) / 2
+               int xMid = (this.getX() + cpTarget.getX()) / 2;
+               int yMid = (this.getY() + cpTarget.getY()) / 2;
+               return new Cartesian(xMid, yMid);
+       }
+
+       public double slope(Cartesian cpTarget)
+       {
+               // Formula -> (y1 - y2) / (x1 - x2)
+               int xDiff = this.getX() - cpTarget.getX();
+               int yDiff = this.getY() - cpTarget.getY();
+               return yDiff / xDiff;
+       }
+
+       public Cartesian dupe()
+       {
+               return new Cartesian(this);
+       }
+
+       public boolean equals(Cartesian cpCompare)
+       {
+               return ((this.getX() == cpCompare.getX()) && (this.getY() == cpCompare.getY()));
+       }
+
+       public String toString()
+       {
+               return "(" + this.getX() + "," + this.getY() + ")";
+       }
+
+       public String toKey()
+       {
+               return "X" + this.getX() + "Y" + this.getY();
+       }
+
+}
\ No newline at end of file