Cleaned up Le Abstract Jungle in "Constraints Framework"

v5stable
ondra 11 years ago
parent 3320678f36
commit 2690e6c5f3
  1. 1
      src/mightypork/utils/math/Calc.java
  2. 37
      src/mightypork/utils/math/Polar.java
  3. 62
      src/mightypork/utils/math/constraints/ConstraintFactory.java
  4. 4
      src/mightypork/utils/math/constraints/RectBound.java
  5. 6
      src/mightypork/utils/math/constraints/RectCache.java
  6. 4
      src/mightypork/utils/math/constraints/VectBound.java
  7. 37
      src/mightypork/utils/math/num/AbstractNum.java
  8. 760
      src/mightypork/utils/math/num/Num.java
  9. 2
      src/mightypork/utils/math/num/NumAdapter.java
  10. 251
      src/mightypork/utils/math/num/NumConst.java
  11. 168
      src/mightypork/utils/math/num/NumMath.java
  12. 153
      src/mightypork/utils/math/num/NumMathBase.java
  13. 613
      src/mightypork/utils/math/num/NumMathDynamic.java
  14. 271
      src/mightypork/utils/math/num/NumMathStatic.java
  15. 69
      src/mightypork/utils/math/num/NumMutable.java
  16. 20
      src/mightypork/utils/math/num/NumProxy.java
  17. 96
      src/mightypork/utils/math/num/NumVal.java
  18. 6
      src/mightypork/utils/math/num/NumVar.java
  19. 16
      src/mightypork/utils/math/num/NumView.java
  20. 45
      src/mightypork/utils/math/rect/AbstractRect.java
  21. 562
      src/mightypork/utils/math/rect/Rect.java
  22. 10
      src/mightypork/utils/math/rect/RectAdapter.java
  23. 239
      src/mightypork/utils/math/rect/RectConst.java
  24. 139
      src/mightypork/utils/math/rect/RectMath.java
  25. 359
      src/mightypork/utils/math/rect/RectMathDynamic.java
  26. 189
      src/mightypork/utils/math/rect/RectMathStatic.java
  27. 117
      src/mightypork/utils/math/rect/RectMutable.java
  28. 37
      src/mightypork/utils/math/rect/RectProxy.java
  29. 224
      src/mightypork/utils/math/rect/RectVal.java
  30. 15
      src/mightypork/utils/math/rect/RectVar.java
  31. 37
      src/mightypork/utils/math/rect/RectVectAdapter.java
  32. 90
      src/mightypork/utils/math/rect/RectView.java
  33. 38
      src/mightypork/utils/math/rect/VectViewRect.java
  34. 83
      src/mightypork/utils/math/vect/AbstractVect.java
  35. 1059
      src/mightypork/utils/math/vect/Vect.java
  36. 2
      src/mightypork/utils/math/vect/VectAdapter.java
  37. 85
      src/mightypork/utils/math/vect/VectAnimated.java
  38. 228
      src/mightypork/utils/math/vect/VectConst.java
  39. 422
      src/mightypork/utils/math/vect/VectMath.java
  40. 665
      src/mightypork/utils/math/vect/VectMathDynamic.java
  41. 280
      src/mightypork/utils/math/vect/VectMathStatic.java
  42. 71
      src/mightypork/utils/math/vect/VectMutable.java
  43. 6
      src/mightypork/utils/math/vect/VectNumAdapter.java
  44. 35
      src/mightypork/utils/math/vect/VectProxy.java
  45. 145
      src/mightypork/utils/math/vect/VectVal.java
  46. 4
      src/mightypork/utils/math/vect/VectVar.java
  47. 78
      src/mightypork/utils/math/vect/VectView.java
  48. 16
      src/mightypork/utils/objects/Convert.java

@ -685,6 +685,7 @@ public class Calc {
public static String toString(double d) public static String toString(double d)
{ {
String s = Double.toString(d); String s = Double.toString(d);
s = s.replace(',', '.');
s = s.replaceAll("([0-9]+\\.[0-9]+)00+[0-9]+", "$1"); s = s.replaceAll("([0-9]+\\.[0-9]+)00+[0-9]+", "$1");
s = s.replaceAll("0+$", ""); s = s.replaceAll("0+$", "");
s = s.replaceAll("\\.$", ""); s = s.replaceAll("\\.$", "");

@ -4,7 +4,6 @@ package mightypork.utils.math;
import mightypork.utils.math.Calc.Deg; import mightypork.utils.math.Calc.Deg;
import mightypork.utils.math.Calc.Rad; import mightypork.utils.math.Calc.Rad;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectView;
/** /**
@ -20,7 +19,7 @@ public class Polar {
/** distance in units */ /** distance in units */
private double radius = 0; private double radius = 0;
private VectView coord = null; private Vect coord = null;
/** /**
@ -135,26 +134,24 @@ public class Polar {
* *
* @return coord * @return coord
*/ */
public VectView toCoord() public Vect toCoord()
{ {
// lazy init // lazy init
if (coord == null) { if (coord == null) coord = new Vect() {
coord = new VectView() {
@Override
@Override public double x()
public double x() {
{ return radius * Math.cos(angle);
return radius * Math.cos(angle); }
}
@Override
@Override public double y()
public double y() {
{ return radius * Math.sin(angle);
return radius * Math.sin(angle); }
} };
};
}
return coord; return coord;
} }

@ -3,7 +3,7 @@
// //
//import mightypork.gamecore.control.timing.Poller; //import mightypork.gamecore.control.timing.Poller;
//import mightypork.utils.math.num.Num; //import mightypork.utils.math.num.Num;
//import mightypork.utils.math.num.NumView; //import mightypork.utils.math.num.Num;
//import mightypork.utils.math.rect.Rect; //import mightypork.utils.math.rect.Rect;
//import mightypork.utils.math.rect.RectVal; //import mightypork.utils.math.rect.RectVal;
//import mightypork.utils.math.rect.RectView; //import mightypork.utils.math.rect.RectView;
@ -38,7 +38,7 @@
// { // {
// if (o instanceof NumBound) return (NumBound) o; // if (o instanceof NumBound) return (NumBound) o;
// //
// if (o instanceof Number) return new NumView() { // if (o instanceof Number) return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -87,9 +87,9 @@
// } // }
// //
// //
// public static NumView min(final Object a, final Object b) // public static Num min(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -100,9 +100,9 @@
// } // }
// //
// //
// public static NumView max(final Object a, final Object b) // public static Num max(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -113,9 +113,9 @@
// } // }
// //
// //
// public static NumView abs(final NumBound a) // public static Num abs(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -126,9 +126,9 @@
// } // }
// //
// //
// public static NumView half(final NumBound a) // public static Num half(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -139,9 +139,9 @@
// } // }
// //
// //
// public static NumView round(final NumBound a) // public static Num round(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -165,9 +165,9 @@
// } // }
// //
// //
// public static NumView ceil(final NumBound a) // public static Num ceil(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -178,9 +178,9 @@
// } // }
// //
// //
// public static NumView floor(final NumBound a) // public static Num floor(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -191,9 +191,9 @@
// } // }
// //
// //
// public static NumView neg(final NumBound a) // public static Num neg(final NumBound a)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -204,9 +204,9 @@
// } // }
// //
// //
// public static NumView add(final Object a, final Object b) // public static Num add(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -217,9 +217,9 @@
// } // }
// //
// //
// public static NumView sub(final Object a, final Object b) // public static Num sub(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -230,9 +230,9 @@
// } // }
// //
// //
// public static NumView mul(final Object a, final Object b) // public static Num mul(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -243,15 +243,15 @@
// } // }
// //
// //
// public static NumView half(final Object a) // public static Num half(final Object a)
// { // {
// return mul(a, 0.5); // return mul(a, 0.5);
// } // }
// //
// //
// public static NumView div(final Object a, final Object b) // public static Num div(final Object a, final Object b)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -262,9 +262,9 @@
// } // }
// //
// //
// public static NumView perc(final Object whole, final Object percent) // public static Num perc(final Object whole, final Object percent)
// { // {
// return new NumView() { // return new Num() {
// //
// @Override // @Override
// public double value() // public double value()
@ -867,13 +867,13 @@
// } // }
// //
// //
// public static NumView height(final RectBound r) // public static Num height(final RectBound r)
// { // {
// return size(r).yC(); // return size(r).yC();
// } // }
// //
// //
// public static NumView width(final RectBound r) // public static Num width(final RectBound r)
// { // {
// return size(r).xC(); // return size(r).xC();
// } // }

@ -1,7 +1,7 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.rect.RectView; import mightypork.utils.math.rect.Rect;
/** /**
@ -14,5 +14,5 @@ public interface RectBound {
/** /**
* @return rect region * @return rect region
*/ */
RectView getRect(); Rect getRect();
} }

@ -3,8 +3,8 @@ package mightypork.utils.math.constraints;
import mightypork.gamecore.control.timing.Pollable; import mightypork.gamecore.control.timing.Pollable;
import mightypork.gamecore.control.timing.Poller; import mightypork.gamecore.control.timing.Poller;
import mightypork.utils.math.rect.Rect;
import mightypork.utils.math.rect.RectMutable; import mightypork.utils.math.rect.RectMutable;
import mightypork.utils.math.rect.RectView;
/** /**
@ -17,7 +17,7 @@ import mightypork.utils.math.rect.RectView;
public class RectCache implements RectBound, Pollable { public class RectCache implements RectBound, Pollable {
private final RectBound observed; private final RectBound observed;
private final RectMutable cached = RectMutable.zero(); private final RectMutable cached = Rect.makeVar();
/** /**
@ -42,7 +42,7 @@ public class RectCache implements RectBound, Pollable {
@Override @Override
public RectView getRect() public Rect getRect()
{ {
return cached; return cached;
} }

@ -1,7 +1,7 @@
package mightypork.utils.math.constraints; package mightypork.utils.math.constraints;
import mightypork.utils.math.vect.VectView; import mightypork.utils.math.vect.Vect;
/** /**
@ -14,5 +14,5 @@ public interface VectBound {
/** /**
* @return the current vector. * @return the current vector.
*/ */
public VectView getVect(); public Vect getVect();
} }

@ -1,37 +0,0 @@
package mightypork.utils.math.num;
abstract class AbstractNum implements Num {
private NumView proxy;
@Override
public Num getNum()
{
return this;
}
@Override
public NumView view()
{
if (proxy == null) proxy = new NumProxy(this);
return proxy;
}
@Override
public NumVal copy()
{
return new NumVal(this);
}
@Override
public String toString()
{
return String.format("#{%.1f}",value());
}
}

@ -1,20 +1,768 @@
package mightypork.utils.math.num; package mightypork.utils.math.num;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.Calc;
import mightypork.utils.math.constraints.NumBound; import mightypork.utils.math.constraints.NumBound;
public interface Num extends NumBound { public abstract class Num implements NumBound {
Num ZERO = NumVal.make(0); static final double CMP_EPSILON = 0.0000001;
Num ONE = NumVal.make(1);
public static final NumConst ZERO = Num.make(0);
public static final NumConst ONE = Num.make(1);
double value();
@FactoryMethod
public static NumConst make(double value)
{
return new NumConst(value);
}
NumView view();
@FactoryMethod
public static NumVar makeVar()
{
return makeVar(0);
}
NumVal copy();
@FactoryMethod
public static NumVar makeVar(double value)
{
return new NumVar(value);
}
@FactoryMethod
public static NumVar makeVar(Num copied)
{
return new NumVar(eval(copied));
}
private Num p_ceil;
private Num p_floor;
private Num p_sgn;
private Num p_round;
private Num p_atan;
private Num p_acos;
private Num p_asin;
private Num p_tan;
private Num p_cos;
private Num p_sin;
private Num p_cbrt;
private Num p_sqrt;
private Num p_cube;
private Num p_square;
private Num p_neg;
private Num p_abs;
/**
* Convert to double, turning null into zero.
*
* @param a num
* @return double
*/
protected static double eval(final NumBound a)
{
return toNum(a).value();
}
/**
* Convert {@link NumBound} to {@link Num}, turning null to Num.ZERO.
*
* @param a numeric bound
* @return num
*/
protected static Num toNum(final NumBound a)
{
return (a == null) ? Num.ZERO : (a.getNum() == null ? Num.ZERO : a.getNum());
}
public NumConst freeze()
{
return new NumConst(value());
}
@Override
public Num getNum()
{
return this;
}
/**
* @return the number
*/
public abstract double value();
public Num abs()
{
if (p_abs == null) p_abs = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.abs(t.value());
}
};
return p_abs;
}
public Num acos()
{
if (p_acos == null) p_acos = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.acos(t.value());
}
};
return p_acos;
}
public Num add(final double addend)
{
return new Num() {
private final Num t = Num.this;
@Override
public double value()
{
return t.value() + addend;
}
};
}
public Num add(final Num addend)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return t.value() + eval(addend);
}
};
}
public Num asin()
{
if (p_asin == null) p_asin = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.asin(t.value());
}
};
return p_asin;
}
public Num atan()
{
if (p_atan == null) p_atan = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.atan(t.value());
}
};
return p_atan;
}
public Num average(final double other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return (t.value() + other) / 2;
}
};
}
public Num average(final Num other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return (t.value() + eval(other)) / 2;
}
};
}
public Num cbrt()
{
if (p_cbrt == null) p_cbrt = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.cbrt(t.value());
}
};
return p_cbrt;
}
public Num ceil()
{
if (p_ceil == null) p_ceil = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.round(t.value());
}
};
return p_ceil;
}
public Num cos()
{
if (p_cos == null) p_cos = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.cos(t.value());
}
};
return p_cos;
}
public Num cube()
{
if (p_cube == null) p_cube = new Num() {
final Num t = Num.this;
@Override
public double value()
{
final double v = t.value();
return v * v * v;
}
};
return p_cube;
}
public Num div(final double factor)
{
return mul(1 / factor);
}
public Num div(final Num factor)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return t.value() / eval(factor);
}
};
}
public boolean eq(double other)
{
return value() == other;
}
public boolean eq(final Num a)
{
return eq(eval(a));
}
public Num floor()
{
if (p_floor == null) p_floor = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.floor(t.value());
}
};
return p_floor;
}
public boolean gt(double other)
{
return Math.signum(value() - other) >= 0;
}
public boolean gt(final Num other)
{
return gt(eval(other));
}
public boolean gte(double other)
{
return Math.signum(value() - other) >= 0;
}
public boolean gte(final Num other)
{
return gte(eval(other));
}
public Num half()
{
return mul(0.5);
}
public boolean isNegative()
{
return value() < 0;
}
public boolean isPositive()
{
return value() > 0;
}
public boolean isZero()
{
return value() == 0;
}
public boolean lt(double other)
{
return !gte(other);
}
public boolean lt(final Num other)
{
return !gte(other);
}
public boolean lte(double other)
{
return !gt(other);
}
public boolean lte(final Num other)
{
return !gt(other);
}
public Num max(final double other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.max(t.value(), other);
}
};
}
public Num max(final Num other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.max(t.value(), eval(other));
}
};
}
public Num min(final double other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.min(t.value(), other);
}
};
}
public Num min(final Num other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.min(t.value(), eval(other));
}
};
}
public Num mul(final double factor)
{
return new Num() {
private final Num t = Num.this;
@Override
public double value()
{
return t.value() * factor;
}
};
}
public Num mul(final Num factor)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return t.value() * eval(factor);
}
};
}
public Num neg()
{
if (p_neg == null) p_neg = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return -1 * t.value();
}
};
return p_neg;
}
public Num perc(final double percent)
{
return mul(percent / 100);
}
public Num perc(final Num percent)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return t.value() * (eval(percent) / 100);
}
};
}
public Num pow(final double other)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.pow(t.value(), other);
}
};
}
public Num pow(final Num power)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.pow(t.value(), eval(power));
}
};
}
public Num round()
{
if (p_round == null) p_round = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.round(t.value());
}
};
return p_round;
}
public Num signum()
{
if (p_sgn == null) p_sgn = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.signum(t.value());
}
};
return p_sgn;
}
public Num sin()
{
if (p_sin == null) p_sin = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.sin(t.value());
}
};
return p_sin;
}
public Num sqrt()
{
if (p_sqrt == null) p_sqrt = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.sqrt(t.value());
}
};
return p_sqrt;
}
public Num square()
{
if (p_square == null) p_square = new Num() {
final Num t = Num.this;
@Override
public double value()
{
final double v = t.value();
return v * v;
}
};
return p_square;
}
public Num sub(final double subtrahend)
{
return add(-subtrahend);
}
public Num sub(final Num subtrahend)
{
return new Num() {
final Num t = Num.this;
@Override
public double value()
{
return t.value() - eval(subtrahend);
}
};
}
public Num tan()
{
if (p_tan == null) p_tan = new Num() {
final Num t = Num.this;
@Override
public double value()
{
return Math.tan(t.value());
}
};
return p_tan;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(value());
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Num)) return false;
final Num other = (Num) obj;
return eq(other);
}
@Override
public String toString()
{
return Calc.toString(value());
}
} }

@ -1,7 +1,7 @@
package mightypork.utils.math.num; package mightypork.utils.math.num;
public abstract class NumAdapter extends NumView { public abstract class NumAdapter extends Num {
protected abstract Num getSource(); protected abstract Num getSource();

@ -0,0 +1,251 @@
package mightypork.utils.math.num;
import mightypork.utils.math.constraints.NumBound;
/**
* Constant number {@link NumBound}
*
* @author MightyPork
*/
public class NumConst extends Num {
private final double value;
NumConst(Num copied) {
this.value = copied.value();
}
NumConst(double value) {
this.value = value;
}
@Override
public double value()
{
return value;
}
/**
* No good to copy a constant.
*/
@Override
@Deprecated
public NumConst freeze()
{
return this;
}
@Override
public NumConst add(double addend)
{
return Num.make(value() + addend);
}
@Override
public NumConst sub(double subtrahend)
{
return add(-subtrahend);
}
@Override
public NumConst mul(double factor)
{
return Num.make(value() * factor);
}
@Override
public NumConst div(double factor)
{
return mul(1 / factor);
}
@Override
public NumConst perc(double percents)
{
return mul(percents / 100);
}
@Override
public NumConst neg()
{
return mul(-1);
}
@Override
public NumConst abs()
{
return Num.make(Math.abs(value()));
}
@Override
public NumConst max(double other)
{
return Num.make(Math.max(value(), other));
}
@Override
public NumConst min(double other)
{
return Num.make(Math.min(value(), other));
}
@Override
public NumConst pow(double power)
{
return Num.make(Math.pow(value(), power));
}
@Override
public NumConst square()
{
final double v = value();
return Num.make(v * v);
}
@Override
public NumConst cube()
{
final double v = value();
return Num.make(v * v * v);
}
@Override
public NumConst sqrt()
{
return Num.make(Math.sqrt(value()));
}
@Override
public NumConst cbrt()
{
return Num.make(Math.cbrt(value()));
}
@Override
public NumConst sin()
{
return Num.make(Math.sin(value()));
}
@Override
public NumConst cos()
{
return Num.make(Math.cos(value()));
}
@Override
public NumConst tan()
{
return Num.make(Math.tan(value()));
}
@Override
public NumConst asin()
{
return Num.make(Math.asin(value()));
}
@Override
public NumConst acos()
{
return Num.make(Math.acos(value()));
}
@Override
public NumConst atan()
{
return Num.make(Math.atan(value()));
}
@Override
public NumConst signum()
{
return Num.make(Math.signum(value()));
}
@Override
public NumConst average(double other)
{
return Num.make((value() + other) / 2);
}
@Override
public NumConst round()
{
return Num.make(Math.round(value()));
}
@Override
public NumConst ceil()
{
return Num.make(Math.ceil(value()));
}
@Override
public NumConst floor()
{
return Num.make(Math.floor(value()));
}
@Override
public NumConst half()
{
return mul(0.5);
}
public NumConst add(NumConst addend)
{
return make(value + addend.value);
}
public NumConst sub(NumConst addend)
{
return make(value - addend.value);
}
public NumConst mul(NumConst addend)
{
return make(value * addend.value);
}
public NumConst div(NumConst addend)
{
return make(value / addend.value);
}
}

@ -1,168 +0,0 @@
package mightypork.utils.math.num;
import mightypork.utils.math.rect.Rect;
/**
* Math operations for numbers
*
* @author MightyPork
* @param <N> functions return type
*/
interface NumMath<N extends NumMath<N>> extends Num {
double CMP_EPSILON = 0.0000001;
N add(Num addend);
N sub(Num subtrahend);
N mul(Num factor);
N div(Num factor);
N perc(Num percent);
N max(Num other);
N min(Num other);
N pow(Num other);
N average(Num other);
N add(double addend);
N sub(double subtrahend);
N mul(double factor);
N div(double factor);
N perc(double percent);
N neg();
N abs();
N max(double other);
N min(double other);
N pow(double other);
N square();
N cube();
N sqrt();
N cbrt();
N sin();
N cos();
N tan();
N asin();
N acos();
N atan();
N round();
N floor();
N ceil();
N signum();
N half();
N average(double other);
boolean lt(Num other);
boolean lte(Num other);
boolean gt(Num other);
boolean gte(Num other);
boolean eq(Num other);
boolean lt(double other);
boolean lte(double other);
boolean gt(double other);
boolean gte(double other);
boolean eq(double other);
boolean isNegative();
boolean isPositive();
boolean isZero();
/**
* Make a square rect with this side, positioned at 0,0
*
* @return new rect
*/
Rect box();
}

@ -1,153 +0,0 @@
package mightypork.utils.math.num;
import mightypork.utils.math.constraints.NumBound;
abstract class NumMathBase<N extends NumMath<N>> extends AbstractNum implements NumMath<N> {
/**
* Convert to double, turning null into zero.
*
* @param a num
* @return double
*/
protected static double eval(final NumBound a)
{
return toNum(a).value();
}
/**
* Convert {@link NumBound} to {@link Num}, turning null to Num.ZERO.
*
* @param a numeric bound
* @return num
*/
protected static Num toNum(final NumBound a)
{
return (a == null) ? Num.ZERO : (a.getNum() == null ? Num.ZERO : a.getNum());
}
@Override
public Num getNum()
{
return this;
}
@Override
public boolean lt(double other)
{
return !gte(other);
}
@Override
public boolean lt(final Num other)
{
return !gte(other);
}
@Override
public boolean lte(double other)
{
return !gt(other);
}
@Override
public boolean lte(final Num other)
{
return !gt(other);
}
@Override
public boolean gt(double other)
{
return Math.signum(value() - other) >= 0;
}
@Override
public boolean gt(final Num other)
{
return gt(eval(other));
}
@Override
public boolean gte(double other)
{
return Math.signum(value() - other) >= 0;
}
@Override
public boolean gte(final Num other)
{
return gte(eval(other));
}
@Override
public boolean eq(double other)
{
return Math.abs(value() - other) <= CMP_EPSILON;
}
@Override
public boolean eq(final Num a)
{
return eq(eval(a));
}
@Override
public boolean isNegative()
{
return Math.signum(value()) < 0;
}
@Override
public boolean isPositive()
{
return Math.signum(value()) > 0;
}
@Override
public boolean isZero()
{
return Math.abs(value()) <= CMP_EPSILON;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(value());
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof NumMathBase)) return false;
final NumMathBase<?> other = (NumMathBase<?>) obj;
return eq(other);
}
}

@ -1,613 +0,0 @@
package mightypork.utils.math.num;
import mightypork.utils.math.rect.RectView;
abstract class NumMathDynamic extends NumMathBase<NumView> {
private NumView ceil;
private NumView floor;
private NumView sgn;
private NumView round;
private NumView atan;
private NumView acos;
private NumView asin;
private NumView tan;
private NumView cos;
private NumView sin;
private NumView cbrt;
private NumView sqrt;
private NumView cube;
private NumView square;
private NumView neg;
private NumView abs;
@Override
public NumView add(final double addend)
{
return new NumView() {
private final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() + addend;
}
};
}
@Override
public NumView sub(final double subtrahend)
{
return add(-subtrahend);
}
@Override
public NumView mul(final double factor)
{
return new NumView() {
private final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() + factor;
}
};
}
@Override
public NumView div(final double factor)
{
return mul(1 / factor);
}
@Override
public NumView perc(final double percent)
{
return mul(percent / 100);
}
@Override
public NumView neg()
{
if (neg == null) neg = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return -1 * t.value();
}
};
return neg;
}
@Override
public NumView abs()
{
if (abs == null) abs = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.abs(t.value());
}
};
return abs;
}
@Override
public NumView max(final double other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.max(t.value(), other);
}
};
}
@Override
public NumView min(final double other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.min(t.value(), other);
}
};
}
@Override
public NumView pow(final double other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.pow(t.value(), other);
}
};
}
@Override
public NumView square()
{
if (square == null) square = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
final double v = t.value();
return v * v;
}
};
return square;
}
@Override
public NumView cube()
{
if (cube == null) cube = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
final double v = t.value();
return v * v * v;
}
};
return cube;
}
@Override
public NumView sqrt()
{
if (sqrt == null) sqrt = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.sqrt(t.value());
}
};
return sqrt;
}
@Override
public NumView cbrt()
{
if (cbrt == null) cbrt = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.cbrt(t.value());
}
};
return cbrt;
}
@Override
public NumView sin()
{
if (sin == null) sin = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.sin(t.value());
}
};
return sin;
}
@Override
public NumView cos()
{
if (cos == null) cos = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.cos(t.value());
}
};
return cos;
}
@Override
public NumView tan()
{
if (tan == null) tan = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.tan(t.value());
}
};
return tan;
}
@Override
public NumView asin()
{
if (asin == null) asin = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.asin(t.value());
}
};
return asin;
}
@Override
public NumView acos()
{
if (acos == null) acos = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.acos(t.value());
}
};
return acos;
}
@Override
public NumView atan()
{
if (atan == null) atan = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.atan(t.value());
}
};
return atan;
}
@Override
public NumView round()
{
if (round == null) round = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.round(t.value());
}
};
return round;
}
@Override
public NumView floor()
{
if (floor == null) floor = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.floor(t.value());
}
};
return floor;
}
@Override
public NumView ceil()
{
if (ceil == null) ceil = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.round(t.value());
}
};
return ceil;
}
@Override
public NumView signum()
{
if (sgn == null) sgn = new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.signum(t.value());
}
};
return sgn;
}
@Override
public NumView average(final double other)
{
return null;
}
@Override
public NumView half()
{
return mul(0.5);
}
@Override
public NumView add(final Num addend)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() + eval(addend);
}
};
}
@Override
public NumView sub(final Num subtrahend)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() - eval(subtrahend);
}
};
}
@Override
public NumView mul(final Num factor)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() * eval(factor);
}
};
}
@Override
public NumView div(final Num factor)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() / eval(factor);
}
};
}
@Override
public NumView perc(final Num percent)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return t.value() * (eval(percent) / 100);
}
};
}
@Override
public NumView max(final Num other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.max(t.value(), eval(other));
}
};
}
@Override
public NumView min(final Num other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.min(t.value(), eval(other));
}
};
}
@Override
public NumView pow(final Num power)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return Math.pow(t.value(), eval(power));
}
};
}
@Override
public NumView average(final Num other)
{
return new NumView() {
final Num t = NumMathDynamic.this;
@Override
public double value()
{
return (t.value() + eval(other)) / 2;
}
};
}
@Override
public RectView box()
{
return RectView.make(this, this);
}
}

@ -1,271 +0,0 @@
package mightypork.utils.math.num;
import mightypork.utils.math.rect.RectVal;
abstract class NumMathStatic<N extends NumMathStatic<N>> extends NumMathBase<N> {
protected abstract N result(double a);
@Override
public N add(double addend)
{
return result(value() + addend);
}
@Override
public N sub(double subtrahend)
{
return add(-subtrahend);
}
@Override
public N mul(double factor)
{
return result(value() * factor);
}
@Override
public N div(double factor)
{
return mul(1 / factor);
}
@Override
public N perc(double percents)
{
return mul(percents / 100);
}
@Override
public N neg()
{
return mul(-1);
}
@Override
public N abs()
{
return result(Math.abs(value()));
}
@Override
public N max(double other)
{
return result(Math.max(value(), other));
}
@Override
public N min(double other)
{
return result(Math.min(value(), other));
}
@Override
public N pow(double power)
{
return result(Math.pow(value(), power));
}
@Override
public N square()
{
final double v = value();
return result(v * v);
}
@Override
public N cube()
{
final double v = value();
return result(v * v * v);
}
@Override
public N sqrt()
{
return result(Math.sqrt(value()));
}
@Override
public N cbrt()
{
return result(Math.cbrt(value()));
}
@Override
public N sin()
{
return result(Math.sin(value()));
}
@Override
public N cos()
{
return result(Math.cos(value()));
}
@Override
public N tan()
{
return result(Math.tan(value()));
}
@Override
public N asin()
{
return result(Math.asin(value()));
}
@Override
public N acos()
{
return result(Math.acos(value()));
}
@Override
public N atan()
{
return result(Math.atan(value()));
}
@Override
public N signum()
{
return result(Math.signum(value()));
}
@Override
public N average(double other)
{
return result((value() + other) / 2);
}
@Override
public N round()
{
return result(Math.round(value()));
}
@Override
public N ceil()
{
return result(Math.ceil(value()));
}
@Override
public N floor()
{
return result(Math.floor(value()));
}
@Override
public N half()
{
return mul(0.5);
}
@Override
public N add(final Num addend)
{
return add(eval(addend));
}
@Override
public N sub(final Num subtrahend)
{
return sub(eval(subtrahend));
}
@Override
public N mul(final Num factor)
{
return mul(eval(factor));
}
@Override
public N div(final Num factor)
{
return div(eval(factor));
}
@Override
public N perc(final Num percent)
{
return perc(eval(percent));
}
@Override
public N max(final Num other)
{
return min(eval(other));
}
@Override
public N min(final Num other)
{
return min(eval(other));
}
@Override
public N pow(final Num power)
{
return pow(eval(power));
}
@Override
public N average(final Num other)
{
return average(eval(other));
}
@Override
public RectVal box()
{
return RectVal.make(this, this);
}
@Override
public String toString()
{
return String.format("{%.1f}", value());
}
}

@ -1,79 +1,12 @@
package mightypork.utils.math.num; package mightypork.utils.math.num;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.constraints.NumBound;
/** /**
* Mutable numeric variable * Mutable numeric variable
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class NumMutable extends NumView { public abstract class NumMutable extends Num {
/**
* Make a new mutable number initialized as zero (0)
*
* @return new mutable number
*/
@FactoryMethod
public static NumMutable zero()
{
return make(0);
}
/**
* Make a new mutable number initialized as one (1)
*
* @return new mutable number
*/
@FactoryMethod
public static NumMutable one()
{
return make(1);
}
/**
* Make as copy of another
*
* @param value copied number
* @return new mutable number with the same value
*/
@FactoryMethod
public static NumMutable make(double value)
{
return new NumMutableImpl(value);
}
/**
* Make as copy of another
*
* @param copied copied number
* @return new mutable number with the same value
*/
@FactoryMethod
public static NumMutable make(Num copied)
{
return new NumMutableImpl(eval(copied));
}
/**
* Make as copy of another
*
* @param copied copied number
* @return new mutable number with the same value
*/
@FactoryMethod
public static NumMutable make(NumBound copied)
{
return new NumMutableImpl(eval(copied));
}
/** /**
* Assign a value * Assign a value

@ -1,20 +0,0 @@
package mightypork.utils.math.num;
class NumProxy extends NumAdapter {
private final Num observed;
public NumProxy(Num observed) {
this.observed = observed;
}
@Override
protected Num getSource()
{
return observed;
}
}

@ -1,96 +0,0 @@
package mightypork.utils.math.num;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.constraints.NumBound;
/**
* Constant number {@link NumBound}
*
* @author MightyPork
*/
public class NumVal extends NumMathStatic<NumVal> {
@SuppressWarnings("hiding")
public static final NumVal ZERO = NumVal.make(0);
@SuppressWarnings("hiding")
public static final NumVal ONE = NumVal.make(1);
/**
* Make a new constant
*
* @param value constant value
* @return new constant with the value
*/
@FactoryMethod
public static NumVal make(double value)
{
return new NumVal(value);
}
/**
* Make a new constant
*
* @param copied number whose value to use
* @return new constant with the value
*/
@FactoryMethod
public static NumVal make(Num copied)
{
return (copied == null ? ZERO : copied.copy());
}
/**
* Make a new constant
*
* @param copied number whose value to use
* @return new constant with the value
*/
@FactoryMethod
public static NumVal make(NumBound copied)
{
return new NumVal(eval(copied));
}
private final double value;
NumVal(Num copied) {
this.value = copied.value();
}
NumVal(double value) {
this.value = value;
}
@Override
public double value()
{
return value;
}
@Override
protected NumVal result(double a)
{
return new NumVal(a);
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public NumVal copy()
{
return super.copy();
}
}

@ -6,17 +6,17 @@ package mightypork.utils.math.num;
* *
* @author MightyPork * @author MightyPork
*/ */
class NumMutableImpl extends NumMutable { public class NumVar extends NumMutable {
private double value; private double value;
public NumMutableImpl(Num value) { public NumVar(Num value) {
this.value = eval(value); this.value = eval(value);
} }
public NumMutableImpl(double value) { public NumVar(double value) {
this.value = value; this.value = value;
} }

@ -1,16 +0,0 @@
package mightypork.utils.math.num;
public abstract class NumView extends NumMathDynamic {
/**
* @deprecated No point in taking view of a view.
*/
@Override
@Deprecated
public NumView view()
{
return this; // no work here
}
}

@ -1,45 +0,0 @@
package mightypork.utils.math.rect;
/**
* Abstract {@link Rect}, implementing all but the data getters
*
* @author MightyPork
*/
abstract class AbstractRect implements Rect {
private RectProxy proxy;
@Override
public RectView getRect()
{
return this.view();
}
@Override
public RectView view()
{
// must NOT call RectView.make, it'd cause infinite recursion.
if (proxy == null) proxy = new RectProxy(this);
return proxy;
}
@Override
public RectVal copy()
{
// must NOT call RectVal.make, it'd cause infinite recursion.
return new RectVal(this);
}
@Override
public String toString()
{
return String.format("Rect { %s - %s }", origin(), origin().copy().add(size()));
}
}

@ -1,9 +1,12 @@
package mightypork.utils.math.rect; package mightypork.utils.math.rect;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.constraints.RectBound; import mightypork.utils.math.constraints.RectBound;
import mightypork.utils.math.num.Num; import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumConst;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectConst;
/** /**
@ -11,10 +14,113 @@ import mightypork.utils.math.vect.Vect;
* *
* @author MightyPork * @author MightyPork
*/ */
public interface Rect extends RectBound { public abstract class Rect implements RectBound {
Rect ZERO = new RectVal(0, 0, 0, 0); public static final RectConst ZERO = new RectConst(0, 0, 0, 0);
Rect ONE = new RectVal(0, 0, 1, 1); public static final RectConst ONE = new RectConst(0, 0, 1, 1);
@FactoryMethod
public static Rect make(Num width, Num height)
{
final Vect origin = Vect.ZERO;
final Vect size = Vect.make(width, height);
return Rect.make(origin, size);
}
@FactoryMethod
public static Rect make(Num x, Num y, Num width, Num height)
{
final Vect origin = Vect.make(x, y);
final Vect size = Vect.make(width, height);
return Rect.make(origin, size);
}
@FactoryMethod
public static Rect make(final Vect origin, final Vect size)
{
return new RectVectAdapter(origin, size);
}
@FactoryMethod
public static RectConst make(NumConst width, NumConst height)
{
final VectConst origin = Vect.ZERO;
final VectConst size = Vect.make(width, height);
return Rect.make(origin, size);
}
@FactoryMethod
public static RectConst make(NumConst x, NumConst y, NumConst width, NumConst height)
{
final VectConst origin = Vect.make(x, y);
final VectConst size = Vect.make(width, height);
return Rect.make(origin, size);
}
@FactoryMethod
public static RectConst make(final VectConst origin, final VectConst size)
{
return new RectConst(origin, size);
}
@FactoryMethod
public static RectConst make(double width, double height)
{
return Rect.make(0, 0, width, height);
}
@FactoryMethod
public static RectConst make(double x, double y, double width, double height)
{
return new RectConst(x, y, width, height);
}
@FactoryMethod
public static RectVar makeVar(double x, double y)
{
return Rect.makeVar(0, 0, x, y);
}
@FactoryMethod
public static RectVar makeVar(Rect copied)
{
return Rect.makeVar(copied.origin(), copied.size());
}
@FactoryMethod
public static RectVar makeVar(Vect origin, Vect size)
{
return Rect.makeVar(origin.x(), origin.y(), size.x(), size.y());
}
@FactoryMethod
public static RectVar makeVar(double x, double y, double width, double height)
{
return new RectVar(x, y, width, height);
}
@FactoryMethod
public static RectVar makeVar()
{
return Rect.makeVar(Rect.ZERO);
}
/** /**
@ -22,15 +128,25 @@ public interface Rect extends RectBound {
* *
* @return copy * @return copy
*/ */
RectVal copy(); public RectConst freeze()
{
// must NOT call RectVal.make, it'd cause infinite recursion.
return new RectConst(this);
}
/** @Override
* Get a proxying view public Rect getRect()
* {
* @return proxy return this;
*/ }
RectView view();
@Override
public String toString()
{
return String.format("Rect { %s - %s }", origin(), origin().freeze().add(size()));
}
/** /**
@ -38,7 +154,7 @@ public interface Rect extends RectBound {
* *
* @return origin (top left) * @return origin (top left)
*/ */
Vect origin(); public abstract Vect origin();
/** /**
@ -46,107 +162,437 @@ public interface Rect extends RectBound {
* *
* @return size vector * @return size vector
*/ */
Vect size(); public abstract Vect size();
/** /**
* @return current width * Add vector to origin
*
* @param move offset vector
* @return result
*/ */
public abstract Num width(); public Rect move(final Vect move)
{
return new Rect() {
private final Rect t = Rect.this;
/**
* @return current height
*/
public abstract Num height();
@Override
public Vect size()
{
return t.size();
}
/**
* @return origin X
*/
public abstract Num x();
@Override
public Vect origin()
{
return t.origin().add(move);
}
/** };
* @return origin Y }
*/
public abstract Num y();
/** /**
* @return left X (low) * Add X and Y to origin
*
* @param x x to add
* @param y y to add
* @return result
*/ */
public abstract Num left(); public Rect move(final double x, final double y)
{
return new Rect() {
private final Rect t = Rect.this;
/**
* @return right X (high)
*/
public abstract Num right();
@Override
public Vect size()
{
return t.size();
}
/**
* @return top Y (low)
*/
public abstract Num top();
@Override
public Vect origin()
{
return t.origin().add(x, y);
}
/** };
* @return bottom Y (high) }
*/
public abstract Num bottom();
public Rect move(final Num x, final Num y)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size();
}
@Override
public Vect origin()
{
return t.origin().add(x, y);
}
};
}
/** /**
* @return top left corner position * Shrink to sides
*
* @param shrink shrink size (horisontal and vertical)
* @return result
*/ */
public abstract Vect topLeft();
public Rect shrink(Vect shrink)
{
return shrink(shrink.x(), shrink.y());
}
/** /**
* @return top center position * Shrink to sides at sides
*
* @param x horizontal shrink
* @param y vertical shrink
* @return result
*/ */
public abstract Vect topCenter(); public Rect shrink(double x, double y)
{
return shrink(x, x, y, y);
}
/** /**
* @return top right corner position * Shrink the rect
*
* @param left shrink
* @param right shrink
* @param top shrink
* @param bottom shrink
* @return result
*/ */
public abstract Vect topRight(); public Rect shrink(final double left, final double right, final double top, final double bottom)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().sub(left + right, top + bottom);
}
@Override
public Vect origin()
{
return t.origin().add(left, top);
}
};
}
/** /**
* @return left center position * Grow to sides
*
* @param grow grow size (added to each side)
* @return grown copy
*/ */
public abstract Vect centerLeft(); public final Rect grow(Vect grow)
{
return grow(grow.x(), grow.y());
}
/** /**
* @return center position * Grow to sides
*
* @param x horizontal grow
* @param y vertical grow
* @return result
*/ */
public abstract Vect center(); public final Rect grow(double x, double y)
{
return grow(x, x, y, y);
}
/** /**
* @return right center position * Grow the rect
*
* @param left growth
* @param right growth
* @param top growth
* @param bottom growth
* @return result
*/ */
public abstract Vect centerRight(); public Rect grow(final double left, final double right, final double top, final double bottom)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().add(left + right, top + bottom);
}
@Override
public Vect origin()
{
return t.origin().sub(left, top);
}
};
}
public Rect shrink(final Num left, final Num right, final Num top, final Num bottom)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().sub(left.add(right), top.add(bottom));
}
@Override
public Vect origin()
{
return t.origin().add(left, top);
}
};
}
public Rect grow(final Num left, final Num right, final Num top, final Num bottom)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().add(left.add(right), top.add(bottom));
}
@Override
public Vect origin()
{
return t.origin().sub(left, top);
}
};
}
/** /**
* @return bottom left corner position * Round coords
*
* @return result
*/ */
public abstract Vect bottomLeft(); public Rect round()
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().round();
}
@Override
public Vect origin()
{
return t.origin().round();
}
};
}
public Num x()
{
return origin().xn();
}
public Num y()
{
return origin().yn();
}
public Num width()
{
return size().xn();
}
public Num height()
{
return size().yn();
}
public Num left()
{
return origin().yn();
}
public Num right()
{
return origin().xn().add(size().xn());
}
public Num top()
{
return origin().yn();
}
public Num bottom()
{
return origin().yn().add(size().yn());
}
public Vect topLeft()
{
return origin();
}
public Vect topCenter()
{
return origin().add(size().xn().half(), Num.ZERO);
}
public Vect topRight()
{
return origin().add(size().xn(), Num.ZERO);
}
public Vect centerLeft()
{
return origin().add(Num.ZERO, size().yn().half());
}
public Vect center()
{
return origin().add(size().half());
}
public Vect centerRight()
{
return origin().add(size().xn(), size().yn().half());
}
public Vect bottomLeft()
{
return origin().add(Num.ZERO, size().yn());
}
public Vect bottomCenter()
{
return origin().add(size().xn().half(), size().yn());
}
public Vect bottomRight()
{
return origin().add(size().xn(), size().yn());
}
/** /**
* @return bottom center position * Center to given point
*
* @param point new center
* @return centered
*/ */
public abstract Vect bottomCenter(); public Rect centerTo(final Vect point)
{
return new Rect() {
Rect t = Rect.this;
@Override
public Vect size()
{
return t.size();
}
@Override
public Vect origin()
{
return point.sub(t.size().half());
}
};
}
/** /**
* @return bottom right corner position * Check if point is inside this rectangle
*
* @param point point to test
* @return is inside
*/ */
public abstract Vect bottomRight(); public boolean contains(Vect point)
{
final double x = point.x();
final double y = point.y();
final double x1 = origin().x();
final double y1 = origin().y();
final double x2 = x1 + size().x();
final double y2 = y1 + size().y();
return x >= x1 && y >= y1 && x <= x2 && y <= y2;
}
} }

@ -3,7 +3,6 @@ package mightypork.utils.math.rect;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectAdapter; import mightypork.utils.math.vect.VectAdapter;
import mightypork.utils.math.vect.VectView;
/** /**
@ -12,7 +11,10 @@ import mightypork.utils.math.vect.VectView;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class RectAdapter extends RectView { public abstract class RectAdapter extends Rect {
// adapters are needed in case the vect returned from source changes
// (is replaced). This way, references to origin and rect will stay intack.
private final VectAdapter originAdapter = new VectAdapter() { private final VectAdapter originAdapter = new VectAdapter() {
@ -40,14 +42,14 @@ public abstract class RectAdapter extends RectView {
@Override @Override
public VectView origin() public Vect origin()
{ {
return originAdapter; return originAdapter;
} }
@Override @Override
public VectView size() public Vect size()
{ {
return sizeAdapter; return sizeAdapter;
} }

@ -0,0 +1,239 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.num.NumConst;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectConst;
/**
* Rectangle with constant bounds, that can never change.
*
* @author MightyPork
*/
public class RectConst extends Rect {
private final VectConst pos;
private final VectConst size;
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
*/
RectConst(double x, double y, double width, double height) {
this.pos = Vect.make(x, y);
this.size = Vect.make(width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
*/
RectConst(Vect origin, Vect size) {
this.pos = origin.freeze();
this.size = size.freeze();
}
/**
* Create at given origin, with given size.
*
* @param another other coord
*/
RectConst(Rect another) {
this.pos = another.origin().freeze();
this.size = another.size().freeze();
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public RectConst freeze()
{
return this; // already constant
}
@Override
public VectConst origin()
{
return pos;
}
@Override
public VectConst size()
{
return size;
}
@Override
public RectConst move(Vect move)
{
return move(move.x(), move.y());
}
@Override
public RectConst move(double x, double y)
{
return Rect.make(origin().add(x, y), size()).freeze();
}
@Override
public RectConst shrink(double left, double right, double top, double bottom)
{
return Rect.make(origin().add(left, top), size().sub(left + right, top + bottom)).freeze();
}
@Override
public RectConst grow(double left, double right, double top, double bottom)
{
return Rect.make(origin().sub(left, top), size().add(left + right, top + bottom)).freeze();
}
@Override
public RectConst round()
{
final VectConst s = size();
final VectConst o = origin();
return Rect.make(o.round(), s.round()).freeze();
}
@Override
public NumConst x()
{
return origin().xn();
}
@Override
public NumConst y()
{
return origin().yn();
}
@Override
public NumConst width()
{
return size().xn();
}
@Override
public NumConst height()
{
return size().yn();
}
@Override
public NumConst left()
{
return origin().xn();
}
@Override
public NumConst right()
{
return origin().xn().add(size().xn());
}
@Override
public NumConst top()
{
return origin().yn();
}
@Override
public NumConst bottom()
{
return origin().yn().add(size().yn());
}
@Override
public VectConst topLeft()
{
return origin();
}
@Override
public VectConst topCenter()
{
return origin().add(size().x() / 2, 0);
}
@Override
public VectConst topRight()
{
return origin().add(size().x(), 0);
}
@Override
public VectConst centerLeft()
{
return origin().add(0, size().y() / 2);
}
@Override
public VectConst center()
{
return origin().add(size().half()).freeze();
}
@Override
public VectConst centerRight()
{
return origin().add(size().x(), size().y() / 2);
}
@Override
public VectConst bottomLeft()
{
return origin().add(0, size().y());
}
@Override
public VectConst bottomCenter()
{
return origin().add(size().x() / 2, size().y());
}
@Override
public VectConst bottomRight()
{
return origin().add(size()).freeze();
}
}

@ -1,139 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.vect.Vect;
abstract class RectMath<R extends Rect> extends AbstractRect {
/**
* Add vector to origin
*
* @param move offset vector
* @return result
*/
public abstract R move(Vect move);
/**
* Add X and Y to origin
*
* @param x x to add
* @param y y to add
* @return result
*/
public abstract R move(double x, double y);
/**
* Shrink to sides
*
* @param shrink shrink size (horisontal and vertical)
* @return result
*/
public R shrink(Vect shrink)
{
return shrink(shrink.x(), shrink.y());
}
/**
* Shrink to sides at sides
*
* @param x horizontal shrink
* @param y vertical shrink
* @return result
*/
public R shrink(double x, double y)
{
return shrink(x, x, y, y);
}
/**
* Shrink the rect
*
* @param left shrink
* @param right shrink
* @param top shrink
* @param bottom shrink
* @return result
*/
public abstract R shrink(double left, double right, double top, double bottom);
/**
* Grow to sides
*
* @param grow grow size (added to each side)
* @return grown copy
*/
public final R grow(Vect grow)
{
return grow(grow.x(), grow.y());
}
/**
* Grow to sides
*
* @param x horizontal grow
* @param y vertical grow
* @return result
*/
public final R grow(double x, double y)
{
return grow(x, x, y, y);
}
/**
* Grow the rect
*
* @param left growth
* @param right growth
* @param top growth
* @param bottom growth
* @return result
*/
public abstract R grow(double left, double right, double top, double bottom);
/**
* Round coords
*
* @return result
*/
public abstract R round();
/**
* Center to given point
*
* @param point new center
* @return centered
*/
public abstract R centerTo(final Vect point);
/**
* Check if point is inside this rectangle
*
* @param point point to test
* @return is inside
*/
public boolean contains(Vect point)
{
final double x = point.x();
final double y = point.y();
final double x1 = origin().x();
final double y1 = origin().y();
final double x2 = x1 + size().x();
final double y2 = y1 + size().y();
return x >= x1 && y >= y1 && x <= x2 && y <= y2;
}
}

@ -1,359 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectView;
abstract class RectMathDynamic extends RectMath<RectView> {
@Override
public abstract VectView origin();
@Override
public abstract VectView size();
@Override
public RectView move(final Vect move)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size();
}
@Override
public VectView origin()
{
return t.origin().add(move);
}
};
}
@Override
public RectView move(final double x, final double y)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size();
}
@Override
public VectView origin()
{
return t.origin().add(x, y);
}
};
}
public RectView move(final Num x, final Num y)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size();
}
@Override
public VectView origin()
{
return t.origin().add(x, y);
}
};
}
@Override
public RectView shrink(final double left, final double right, final double top, final double bottom)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size().sub(left + right, top + bottom);
}
@Override
public VectView origin()
{
return t.origin().add(left, top);
}
};
}
@Override
public RectView grow(final double left, final double right, final double top, final double bottom)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size().add(left + right, top + bottom);
}
@Override
public VectView origin()
{
return t.origin().sub(left, top);
}
};
}
public RectView shrink(final Num left, final Num right, final Num top, final Num bottom)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size().sub(left.view().add(right), top.view().add(bottom));
}
@Override
public VectView origin()
{
return t.origin().add(left, top);
}
};
}
public RectView grow(final Num left, final Num right, final Num top, final Num bottom)
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size().add(left.view().add(right), top.view().add(bottom));
}
@Override
public VectView origin()
{
return t.origin().sub(left, top);
}
};
}
@Override
public RectView round()
{
return new RectView() {
private final RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size().round();
}
@Override
public VectView origin()
{
return t.origin().round();
}
};
}
@Override
public Num x()
{
return origin().xn();
}
@Override
public Num y()
{
return origin().yn();
}
@Override
public Num width()
{
return size().xn();
}
@Override
public Num height()
{
return size().yn();
}
@Override
public Num left()
{
return origin().yn();
}
@Override
public Num right()
{
return origin().xn().add(size().xn());
}
@Override
public Num top()
{
return origin().yn();
}
@Override
public Num bottom()
{
return origin().yn().add(size().yn());
}
@Override
public VectView topLeft()
{
return origin();
}
@Override
public VectView topCenter()
{
return origin().add(size().xn().half(), Num.ZERO);
}
@Override
public VectView topRight()
{
return origin().add(size().xn(), Num.ZERO);
}
@Override
public VectView centerLeft()
{
return origin().add(Num.ZERO, size().yn().half());
}
@Override
public VectView center()
{
return origin().add(size().half());
}
@Override
public VectView centerRight()
{
return origin().add(size().xn(), size().yn().half());
}
@Override
public VectView bottomLeft()
{
return origin().add(Num.ZERO, size().yn());
}
@Override
public VectView bottomCenter()
{
return origin().add(size().xn().half(), size().yn());
}
@Override
public VectView bottomRight()
{
return origin().add(size().xn(), size().yn());
}
@Override
public RectView centerTo(final Vect point)
{
return new RectView() {
RectMathDynamic t = RectMathDynamic.this;
@Override
public VectView size()
{
return t.size();
}
@Override
public VectView origin()
{
return point.view().sub(t.size().half());
}
};
}
}

@ -1,189 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.num.NumVal;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectVal;
import mightypork.utils.math.vect.VectView;
abstract class RectMathStatic<R extends RectMathStatic<R>> extends RectMath<R> {
@Override
public abstract VectVal origin();
@Override
public abstract VectVal size();
@Override
public R move(Vect move)
{
return move(move.x(), move.y());
}
@Override
public R move(double x, double y)
{
return result(origin().add(x, y), size());
}
@Override
public R shrink(double left, double right, double top, double bottom)
{
return result(origin().add(left, top), size().sub(left + right, top + bottom));
}
@Override
public R grow(double left, double right, double top, double bottom)
{
return result(origin().sub(left, top), size().add(left + right, top + bottom));
}
@Override
public R centerTo(final Vect point)
{
final VectView s = size().view();
final VectView o = origin().view();
return result(o.sub(s.half()), s);
}
@Override
public R round()
{
final VectView s = size().view();
final VectView o = origin().view();
return result(o.round(), s.round());
}
protected abstract R result(Vect newOrigin, Vect newSize);
@Override
public NumVal x()
{
return origin().xn();
}
@Override
public NumVal y()
{
return origin().yn();
}
@Override
public NumVal width()
{
return size().xn();
}
@Override
public NumVal height()
{
return size().yn();
}
@Override
public NumVal left()
{
return origin().xn();
}
@Override
public NumVal right()
{
return origin().xn().add(size().xn());
}
@Override
public NumVal top()
{
return origin().yn();
}
@Override
public NumVal bottom()
{
return origin().yn().add(size().yn());
}
@Override
public VectVal topLeft()
{
return origin();
}
@Override
public VectVal topCenter()
{
return origin().add(size().x() / 2, 0);
}
@Override
public VectVal topRight()
{
return origin().add(size().x(), 0);
}
@Override
public VectVal centerLeft()
{
return origin().add(0, size().y() / 2);
}
@Override
public VectVal center()
{
return origin().add(size().view().half());
}
@Override
public VectVal centerRight()
{
return origin().add(size().x(), size().y() / 2);
}
@Override
public VectVal bottomLeft()
{
return origin().add(0, size().y());
}
@Override
public VectVal bottomCenter()
{
return origin().add(size().x() / 2, size().y());
}
@Override
public VectVal bottomRight()
{
return origin().add(size().view());
}
}

@ -1,9 +1,7 @@
package mightypork.utils.math.rect; package mightypork.utils.math.rect;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectVal;
/** /**
@ -11,115 +9,7 @@ import mightypork.utils.math.vect.VectVal;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class RectMutable extends RectView { public abstract class RectMutable extends Rect {
/**
* Create at 0,0 with zero size
*
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable zero()
{
return make(0, 0, 0, 0);
}
/**
* Create at 1,1 with zero size
*
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable one()
{
return make(0, 0, 1, 1);
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
* @return new mutable rect
*/
public static RectMutable make(double width, double height)
{
return make(0, 0, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable make(Vect origin, double width, double height)
{
return make(origin, VectVal.make(width, height));
}
/**
* Create at 0,0 with given size.
*
* @param size
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable make(Vect size)
{
return make(Vect.ZERO, size);
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable make(double x, double y, double width, double height)
{
return make(x, y, width, height);
}
/**
* Create as copy of another
*
* @param other copied
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable make(Rect other)
{
return make(other.origin(), other.size());
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
* @return new mutable rect
*/
@FactoryMethod
public static RectMutable make(Vect origin, Vect size)
{
return make(origin.x(), origin.y(), size.x(), size.y());
}
/** /**
* Set to other rect's coordinates * Set to other rect's coordinates
@ -141,7 +31,7 @@ public abstract class RectMutable extends RectView {
*/ */
public void setTo(Vect origin, double width, double height) public void setTo(Vect origin, double width, double height)
{ {
setTo(origin, VectVal.make(width, height)); setTo(origin, Vect.make(width, height));
} }
@ -155,7 +45,7 @@ public abstract class RectMutable extends RectView {
*/ */
public void setTo(double x, double y, double width, double height) public void setTo(double x, double y, double width, double height)
{ {
setTo(VectVal.make(x, y), VectVal.make(width, height)); setTo(Vect.make(x, y), Vect.make(width, height));
} }
@ -180,6 +70,7 @@ public abstract class RectMutable extends RectView {
setTo(Vect.ZERO, Vect.ZERO); setTo(Vect.ZERO, Vect.ZERO);
} }
/** /**
* Set new origin * Set new origin
* *

@ -1,37 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.vect.VectView;
/**
* Immutable rect accessor
*
* @author MightyPork
*/
class RectProxy extends RectView {
private final Rect observed;
public RectProxy(Rect observed) {
assert (!(observed instanceof RectView));
this.observed = observed;
}
@Override
public VectView origin()
{
return observed.origin().view();
}
@Override
public VectView size()
{
return observed.size().view();
}
}

@ -1,224 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectVal;
/**
* Rectangle with constant bounds, that can never change.
*
* @author MightyPork
*/
public class RectVal extends RectMathStatic<RectVal> {
@SuppressWarnings("hiding")
public static final RectVal ZERO = Rect.ZERO.copy();
@SuppressWarnings("hiding")
public static final RectVal ONE = Rect.ONE.copy();
/**
* Get a proxy at given rect
*
* @param observed observed rect
* @return view
*/
@FactoryMethod
public static RectVal make(Rect observed)
{
return observed.copy(); // let the rect handle it
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Num width, Num height)
{
return make(0, 0, width.value(), height.value());
}
/**
* Create at 0,0 with given size
*
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(double width, double height)
{
return make(0, 0, width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Vect origin, double width, double height)
{
return make(origin, VectVal.make(width, height));
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Vect origin, Num width, Num height)
{
return make(origin, VectVal.make(width, height));
}
/**
* Create at 0,0 with given size.
*
* @param size
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Vect size)
{
return make(Vect.ZERO, size);
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(double x, double y, double width, double height)
{
return new RectVal(x, y, width, height);
}
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Num x, Num y, Num width, Num height)
{
return new RectVal(x.value(), y.value(), width.value(), height.value());
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
* @return new mutable rect
*/
@FactoryMethod
public static RectVal make(Vect origin, Vect size)
{
return make(origin.x(), origin.y(), size.x(), size.y());
}
private final VectVal pos;
private final VectVal size;
/**
* Create at given origin, with given size.
*
* @param x
* @param y
* @param width
* @param height
*/
RectVal(double x, double y, double width, double height) {
this.pos = VectVal.make(x, y);
this.size = VectVal.make(width, height);
}
/**
* Create at given origin, with given size.
*
* @param origin
* @param size
*/
RectVal(Vect origin, Vect size) {
this.pos = origin.copy();
this.size = size.copy();
}
/**
* Create at given origin, with given size.
*
* @param another other coord
*/
RectVal(Rect another) {
this.pos = another.origin().copy();
this.size = another.size().copy();
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public RectVal copy()
{
return this; // already constant
}
@Override
public VectVal origin()
{
return pos;
}
@Override
public VectVal size()
{
return size;
}
@Override
protected RectVal result(Vect newOrigin, Vect newSize)
{
return make(newOrigin, newSize);
}
}

@ -2,14 +2,13 @@ package mightypork.utils.math.rect;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectMutable; import mightypork.utils.math.vect.VectVar;
import mightypork.utils.math.vect.VectView;
class RectMutableImpl extends RectMutable { public class RectVar extends RectMutable {
final VectMutable pos = VectMutable.zero(); final VectVar pos = Vect.makeVar();
final VectMutable size = VectMutable.zero(); final VectVar size = Vect.makeVar();
/** /**
@ -20,21 +19,21 @@ class RectMutableImpl extends RectMutable {
* @param width * @param width
* @param height * @param height
*/ */
public RectMutableImpl(double x, double y, double width, double height) { public RectVar(double x, double y, double width, double height) {
this.pos.setTo(x, y); this.pos.setTo(x, y);
this.size.setTo(width, height); this.size.setTo(width, height);
} }
@Override @Override
public VectView origin() public Vect origin()
{ {
return pos; return pos;
} }
@Override @Override
public VectView size() public Vect size()
{ {
return size; return size;
} }

@ -0,0 +1,37 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.vect.Vect;
/**
* Rect made of two {@link VectView}s
*
* @author MightyPork
*/
class RectVectAdapter extends Rect {
private final Vect origin;
private final Vect size;
public RectVectAdapter(Vect origin, Vect size) {
this.origin = origin;
this.size = size;
}
@Override
public Vect origin()
{
return origin;
}
@Override
public Vect size()
{
return size;
}
}

@ -1,90 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectView;
/**
* Immutable rect
*
* @author MightyPork
*/
public abstract class RectView extends RectMathDynamic {
/**
* Get a proxy at given rect
*
* @param observed observed rect
* @return view
*/
@FactoryMethod
public static RectView make(Rect observed)
{
return observed.view(); // let the rect handle it
}
/**
* Get a rect made of numeric constraints
*
* @param width width
* @param height height
* @return view rect
*/
@FactoryMethod
public static RectView make(Num width, Num height)
{
final Vect origin = Vect.ZERO;
final Vect size = VectView.make(width, height);
return new VectViewRect(origin, size);
}
/**
* Get a rect made of numeric constraints
*
* @param x x coord
* @param y y coord
* @param width width
* @param height height
* @return view rect
*/
@FactoryMethod
public static RectView make(Num x, Num y, Num width, Num height)
{
final Vect origin = VectView.make(x, y);
final Vect size = VectView.make(width, height);
return new VectViewRect(origin, size);
}
/**
* Get a rect made of two vect views
*
* @param origin origin view
* @param size size view
* @return view rect
*/
@FactoryMethod
public static RectView make(final Vect origin, final Vect size)
{
return new VectViewRect(origin, size);
}
/**
* @deprecated No point in taking view of a view
*/
@Override
@Deprecated
public RectView view()
{
return this; // wont change
}
}

@ -1,38 +0,0 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectView;
/**
* Rect made of two {@link VectView}s
*
* @author MightyPork
*/
class VectViewRect extends RectView {
private final VectView origin;
private final VectView size;
public VectViewRect(Vect origin, Vect size) {
this.origin = origin.view();
this.size = size.view();
}
@Override
public VectView origin()
{
return origin;
}
@Override
public VectView size()
{
return size;
}
}

@ -1,83 +0,0 @@
package mightypork.utils.math.vect;
abstract class AbstractVect implements Vect {
private VectView proxy;
@Override
public final int xi()
{
return (int) Math.round(x());
}
@Override
public final int yi()
{
return (int) Math.round(y());
}
@Override
public final int zi()
{
return (int) Math.round(z());
}
@Override
public final VectView getVect()
{
return view();
}
@Override
public VectVal copy()
{
// must NOT call VectVal.make, it'd cause infinite recursion.
return new VectVal(this);
}
@Override
public VectView view()
{
if (proxy == null) proxy = new VectProxy(this);
return proxy;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + Double.valueOf(x()).hashCode();
result = prime * result + Double.valueOf(y()).hashCode();
result = prime * result + Double.valueOf(z()).hashCode();
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Vect)) return false;
final Vect other = (Vect) obj;
return x() == other.x() && y() == other.y() && z() == other.z();
}
@Override
public String toString()
{
return String.format("(%.1f|%.1f|%.1f)", x(), y(), z());
}
}

File diff suppressed because it is too large Load Diff

@ -7,7 +7,7 @@ package mightypork.utils.math.vect;
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class VectAdapter extends VectView { public abstract class VectAdapter extends Vect {
/** /**
* @return the proxied coord * @return the proxied coord

@ -15,48 +15,6 @@ import mightypork.utils.math.animation.Easing;
*/ */
public class VectAnimated extends VectMutable implements Pauseable, Updateable { public class VectAnimated extends VectMutable implements Pauseable, Updateable {
/**
* Create an animated vector; This way different easing / settings can be
* specified for each coordinate.
*
* @param x x animator
* @param y y animator
* @param z z animator
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated make(AnimDouble x, AnimDouble y, AnimDouble z)
{
return new VectAnimated(x, y, z);
}
/**
* Create an animated vector
*
* @param start initial positioon
* @param easing animation easing
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated make(Vect start, Easing easing)
{
return new VectAnimated(start, easing);
}
/**
* Create an animated vector, initialized at 0,0,0
*
* @param easing animation easing
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated make(Easing easing)
{
return new VectAnimated(Vect.ZERO, easing);
}
private final AnimDouble x, y, z; private final AnimDouble x, y, z;
private double defaultDuration = 0; private double defaultDuration = 0;
@ -286,4 +244,47 @@ public class VectAnimated extends VectMutable implements Pauseable, Updateable {
z.setEasing(easing); z.setEasing(easing);
} }
/**
* Create an animated vector; This way different easing / settings can be
* specified for each coordinate.
*
* @param x x animator
* @param y y animator
* @param z z animator
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated makeVar(AnimDouble x, AnimDouble y, AnimDouble z)
{
return new VectAnimated(x, y, z);
}
/**
* Create an animated vector
*
* @param start initial positioon
* @param easing animation easing
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated makeVar(Vect start, Easing easing)
{
return new VectAnimated(start, easing);
}
/**
* Create an animated vector, initialized at 0,0,0
*
* @param easing animation easing
* @return animated mutable vector
*/
@FactoryMethod
public static VectAnimated makeVar(Easing easing)
{
return new VectAnimated(Vect.ZERO, easing);
}
} }

@ -0,0 +1,228 @@
package mightypork.utils.math.vect;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumConst;
/**
* Coordinate with immutable numeric values.<br>
* This coordinate is guaranteed to never change, as opposed to view.
*
* @author MightyPork
*/
public final class VectConst extends Vect {
private final double x, y, z;
// non-parametric operations are cached using lazy load.
private NumConst v_size;
private VectConst v_neg;
private VectConst v_ceil;
private VectConst v_floor;
private VectConst v_round;
private VectConst v_half;
private VectConst v_abs;
private NumConst v_xc;
private NumConst v_yc;
private NumConst v_zc;
VectConst(Vect other) {
this(other.x(), other.y(), other.z());
}
VectConst(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public double x()
{
return x;
}
@Override
public double y()
{
return y;
}
@Override
public double z()
{
return z;
}
/**
* @return X constraint
*/
@Override
public final NumConst xn()
{
if (v_xc == null) v_xc = Num.make(this.x);
return v_xc;
}
/**
* @return Y constraint
*/
@Override
public final NumConst yn()
{
if (v_yc == null) v_yc = Num.make(this.y);
return v_yc;
}
/**
* @return Z constraint
*/
@Override
public final NumConst zn()
{
if (v_zc == null) v_zc = Num.make(this.z);
return v_zc;
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public VectConst freeze()
{
return this; // it's constant already
}
@Override
public VectConst abs()
{
if (v_abs != null) return v_abs;
return v_abs = Vect.make(Math.abs(x()), Math.abs(y()), Math.abs(z()));
}
@Override
public VectConst add(double x, double y)
{
return add(x, y, 0);
}
@Override
public VectConst add(double x, double y, double z)
{
return Vect.make(x() + x, y() + y, z() + z);
}
@Override
public VectConst half()
{
if (v_half != null) return v_half;
return v_half = mul(0.5);
}
@Override
public VectConst mul(double d)
{
return mul(d, d, d);
}
@Override
public VectConst mul(double x, double y)
{
return mul(x, y, 1);
}
@Override
public VectConst mul(double x, double y, double z)
{
return Vect.make(x() * x, y() * y, z() * z);
}
@Override
public VectConst round()
{
if (v_round != null) return v_round;
return v_round = Vect.make(Math.round(x()), Math.round(y()), Math.round(z()));
}
@Override
public VectConst floor()
{
if (v_floor != null) return v_floor;
return v_floor = Vect.make(Math.floor(x()), Math.floor(y()), Math.floor(z()));
}
@Override
public VectConst ceil()
{
if (v_ceil != null) return v_ceil;
return v_ceil = Vect.make(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
}
@Override
public VectConst sub(double x, double y)
{
return sub(x, y, 0);
}
@Override
public VectConst sub(double x, double y, double z)
{
return Vect.make(x() - x, y() - y, z() - z);
}
@Override
public VectConst neg()
{
if (v_neg != null) return v_neg;
return v_neg = Vect.make(-x(), -y(), -z());
}
@Override
public VectConst norm(double size)
{
if (isZero()) return this; // can't norm zero vector
final double k = size().mul(1 / size).value();
return mul(k);
}
@Override
public NumConst size()
{
if (v_size != null) return v_size;
final double x = x(), y = y(), z = z();
return v_size = Num.make(Math.sqrt(x * x + y * y + z * z));
}
}

@ -1,422 +0,0 @@
package mightypork.utils.math.vect;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumVal;
/**
* Vec operations
*
* @author MightyPork
* @param <V> return type of vector functions
* @param <N> return type of numeric functions
* @param <B> return type of boolean functions
*/
abstract class VectMath<V extends Vect, N> extends AbstractVect {
/**
* @return X constraint
*/
public abstract Num xn();
/**
* @return Y constraint
*/
public abstract Num yn();
/**
* @return Z constraint
*/
public abstract Num zn();
/**
* Set X coordinate (if immutable, in a copy).
*
* @param x x coordinate
* @return result
*/
public VectView withX(double x)
{
return withX(NumVal.make(x));
}
/**
* Set Y coordinate (if immutable, in a copy).
*
* @param y y coordinate
* @return result
*/
public VectView withY(double y)
{
return withY(NumVal.make(y));
}
/**
* Set Z coordinate (if immutable, in a copy).
*
* @param z z coordinate
* @return result
*/
public VectView withZ(double z)
{
return withZ(NumVal.make(z));
}
public VectView withX(final Num x)
{
return new VectView() {
final Vect t = VectMath.this;
@Override
public double x()
{
return x.value();
}
@Override
public double y()
{
return t.z();
}
@Override
public double z()
{
return t.z();
}
};
}
public VectView withY(final Num y)
{
return new VectView() {
final Vect t = VectMath.this;
@Override
public double x()
{
return t.x();
}
@Override
public double y()
{
return y.value();
}
@Override
public double z()
{
return t.z();
}
};
}
public VectView withZ(final Num z)
{
return new VectView() {
final Vect t = VectMath.this;
@Override
public double x()
{
return t.x();
}
@Override
public double y()
{
return t.y();
}
@Override
public double z()
{
return z.value();
}
};
}
/**
* Get absolute value (positive)
*
* @return result
*/
public abstract V abs();
/**
* Add a vector.
*
* @param vec offset
* @return result
*/
public abstract V add(Vect vec);
/**
* Add to each component.<br>
* Z is unchanged.
*
* @param x x offset
* @param y y offset
* @return result
*/
public abstract V add(double x, double y);
/**
* Add to each component.
*
* @param x x offset
* @param y y offset
* @param z z offset
* @return result
*/
public abstract V add(double x, double y, double z);
public abstract V add(Num x, Num y);
public abstract V add(final Num x, final Num y, final Num z);
/**
* Get copy divided by two
*
* @return result
*/
public abstract V half();
/**
* Multiply each component.
*
* @param d multiplier
* @return result
*/
public abstract V mul(double d);
/**
* Multiply each component.
*
* @param vec vector of multipliers
* @return result
*/
public abstract V mul(Vect vec);
/**
* Multiply each component.<br>
* Z is unchanged.
*
* @param x x multiplier
* @param y y multiplier
* @return result
*/
public abstract V mul(double x, double y);
/**
* Multiply each component.
*
* @param x x multiplier
* @param y y multiplier
* @param z z multiplier
* @return result
*/
public abstract V mul(double x, double y, double z);
/**
* Multiply each component.
*
* @param d multiplier
* @return result
*/
public abstract V mul(final Num d);
/**
* Multiply each component.
*
* @param x x multiplier
* @param y y multiplier
* @return result
*/
public abstract V mul(final Num x, final Num y);
/**
* Multiply each component.
*
* @param x x multiplier
* @param y y multiplier
* @param z z multiplier
* @return result
*/
public abstract V mul(final Num x, final Num y, final Num z);
/**
* Round coordinates.
*
* @return result
*/
public abstract V round();
/**
* Round coordinates down.
*
* @return result
*/
public abstract V floor();
/**
* Round coordinates up.
*
* @return result
*/
public abstract V ceil();
/**
* Subtract vector.
*
* @param vec offset
* @return result
*/
public abstract V sub(Vect vec);
/**
* Subtract a 2D vector.<br>
* Z is unchanged.
*
* @param x x offset
* @param y y offset
* @return result
*/
public abstract V sub(double x, double y);
/**
* Subtract a 3D vector.
*
* @param x x offset
* @param y y offset
* @param z z offset
* @return result
*/
public abstract V sub(double x, double y, double z);
public abstract V sub(Num x, Num y);
public abstract V sub(final Num x, final Num y, final Num z);
/**
* Negate all coordinates (* -1)
*
* @return result
*/
public abstract V neg();
/**
* Scale vector to given size.
*
* @param size size we need
* @return result
*/
public abstract V norm(double size);
/**
* Get distance to other point
*
* @param point other point
* @return distance
*/
public abstract N distTo(Vect point);
/**
* Get middle of line to other point
*
* @param point other point
* @return result
*/
public abstract V midTo(Vect point);
/**
* Create vector from this point to other point
*
* @param point second point
* @return result
*/
public abstract V vectTo(Vect point);
/**
* Get cross product (vector multiplication)
*
* @param vec other vector
* @return result
*/
public abstract V cross(Vect vec);
/**
* Get dot product (scalar multiplication)
*
* @param vec other vector
* @return dot product
*/
public abstract N dot(Vect vec);
/**
* Get vector size
*
* @return size
*/
public abstract N size();
@Override
public boolean isZero()
{
return x() == 0 && y() == 0 && z() == 0;
}
}

@ -1,665 +0,0 @@
package mightypork.utils.math.vect;
import mightypork.utils.math.constraints.NumBound;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumVal;
import mightypork.utils.math.num.NumView;
/**
* Dynamic vector math functions, to be used with a view.
*
* @author MightyPork
*/
abstract class VectMathDynamic extends VectMath<VectView, NumBound> {
private NumView size;
private VectView neg;
private VectView half;
private VectView abs;
private NumView xc;
private NumView yc;
private NumView zc;
/**
* @return X constraint
*/
@Override
public final NumView xn()
{
if (xc == null) xc = new NumView() {
@Override
public double value()
{
return x();
}
};
return xc;
}
/**
* @return Y constraint
*/
@Override
public final NumView yn()
{
if (yc == null) yc = new NumView() {
@Override
public double value()
{
return y();
}
};
return yc;
}
/**
* @return Z constraint
*/
@Override
public final NumView zn()
{
if (zc == null) zc = new NumView() {
@Override
public double value()
{
return z();
}
};
return zc;
}
@Override
public VectView abs()
{
if (abs == null) abs = new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return Math.abs(t.x());
}
@Override
public double y()
{
return Math.abs(t.y());
}
@Override
public double z()
{
return Math.abs(t.z());
}
};
return abs;
}
@Override
public VectView add(Vect vec)
{
return add(vec.view().xn(), vec.view().yn(), vec.view().zn());
}
@Override
public VectView add(double x, double y)
{
return add(x, y, 0);
}
@Override
public VectView add(final double x, final double y, final double z)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return t.x() + x;
}
@Override
public double y()
{
return t.y() + y;
}
@Override
public double z()
{
return t.z() + z;
}
};
}
@Override
public VectView add(Num x, Num y)
{
return add(x, y, Num.ZERO);
}
@Override
public VectView add(final Num x, final Num y, final Num z)
{
return new VectView() {
final Vect t = VectMathDynamic.this;
@Override
public double x()
{
return t.x() + x.value();
}
@Override
public double y()
{
return t.y() + y.value();
}
@Override
public double z()
{
return t.z() + z.value();
}
};
}
@Override
public VectView half()
{
if (half == null) half = mul(0.5);
return half;
}
@Override
public VectView mul(double d)
{
return mul(d, d, d);
}
@Override
public VectView mul(Vect vec)
{
return mul(vec.view().xn(), vec.view().yn(), vec.view().zn());
}
@Override
public VectView mul(double x, double y)
{
return mul(x, y, 1);
}
@Override
public VectView mul(final double x, final double y, final double z)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return t.x() * x;
}
@Override
public double y()
{
return t.y() * y;
}
@Override
public double z()
{
return t.z() * z;
}
};
}
@Override
public VectView mul(final Num d)
{
return mul(d, d, d);
}
@Override
public VectView mul(final Num x, final Num y)
{
return mul(x, y, Num.ONE);
}
@Override
public VectView mul(final Num x, final Num y, final Num z)
{
return new VectView() {
final Vect t = VectMathDynamic.this;
@Override
public double x()
{
return t.x() * x.value();
}
@Override
public double y()
{
return t.y() * y.value();
}
@Override
public double z()
{
return t.z() * z.value();
}
};
}
@Override
public VectView round()
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return Math.round(t.x());
}
@Override
public double y()
{
return Math.round(t.y());
}
@Override
public double z()
{
return Math.round(t.z());
}
};
}
@Override
public VectView floor()
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return Math.floor(t.x());
}
@Override
public double y()
{
return Math.floor(t.y());
}
@Override
public double z()
{
return Math.floor(t.z());
}
};
}
@Override
public VectView ceil()
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return Math.ceil(t.x());
}
@Override
public double y()
{
return Math.ceil(t.y());
}
@Override
public double z()
{
return Math.ceil(t.z());
}
};
}
@Override
public VectView sub(Vect vec)
{
return sub(vec.view().xn(), vec.view().yn(), vec.view().zn());
}
@Override
public VectView sub(double x, double y)
{
return add(-x, -y, 0);
}
@Override
public VectView sub(double x, double y, double z)
{
return add(-x, -y, -z);
}
@Override
public VectView sub(Num x, Num y)
{
return sub(x, y, Num.ZERO);
}
@Override
public VectView sub(final Num x, final Num y, final Num z)
{
return new VectView() {
final Vect t = VectMathDynamic.this;
@Override
public double x()
{
return t.x() - x.value();
}
@Override
public double y()
{
return t.y() - y.value();
}
@Override
public double z()
{
return t.z() - z.value();
}
};
}
@Override
public VectView neg()
{
if (neg == null) neg = mul(-1);
return neg;
}
public VectView norm(final Num size)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
final double tSize = t.size().value();
final double nSize = size.value();
if (tSize == 0 || nSize == 0) return 0;
return x() / (nSize / tSize);
}
@Override
public double y()
{
final double tSize = t.size().value();
final double nSize = size.value();
if (tSize == 0 || nSize == 0) return 0;
return y() / (nSize / tSize);
}
@Override
public double z()
{
final double tSize = t.size().value();
final double nSize = size.value();
if (tSize == 0 || nSize == 0) return 0;
return z() / (nSize / tSize);
}
};
}
@Override
public VectView norm(final double size)
{
return norm(NumVal.make(size));
}
@Override
public NumView distTo(final Vect point)
{
return new NumView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double value()
{
final double dx = t.x() - point.x();
final double dy = t.y() - point.y();
final double dz = t.z() - point.z();
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
};
}
@Override
public final VectView midTo(final Vect point)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return (point.x() + t.x()) * 0.5;
}
@Override
public double y()
{
return (point.y() + t.y()) * 0.5;
}
@Override
public double z()
{
return (point.z() + t.z()) * 0.5;
}
};
}
@Override
public final VectView vectTo(final Vect point)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return (point.x() - t.x());
}
@Override
public double y()
{
return (point.y() - t.y());
}
@Override
public double z()
{
return (point.z() - t.z());
}
};
}
@Override
public final VectView cross(final Vect vec)
{
return new VectView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double x()
{
return t.y() * vec.z() - t.z() * vec.y();
}
@Override
public double y()
{
return t.z() * vec.x() - t.x() * vec.z();
}
@Override
public double z()
{
return t.x() * vec.y() - t.y() * vec.x();
}
};
}
@Override
public NumView dot(final Vect vec)
{
return new NumView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double value()
{
return t.x() * vec.x() + t.y() * vec.y() + t.z() * vec.z();
}
};
}
@Override
public Num size()
{
if (size == null) size = new NumView() {
final VectMathDynamic t = VectMathDynamic.this;
@Override
public double value()
{
final double x = t.x(), y = t.y(), z = t.z();
return Math.sqrt(x * x + y * y + z * z);
}
};
return size;
}
}

@ -1,280 +0,0 @@
package mightypork.utils.math.vect;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumVal;
/**
* Implementation of coordinate methods
*
* @author MightyPork
* @param <V> Return type of methods
*/
abstract class VectMathStatic<V extends VectMathStatic<V>> extends VectMath<V, NumVal> {
@Override
public NumVal xn()
{
return NumVal.make(x());
}
@Override
public NumVal yn()
{
return NumVal.make(yn());
}
@Override
public NumVal zn()
{
return NumVal.make(z());
}
/**
* <p>
* Some operation was performed and this result was obtained.
* </p>
* <p>
* It's now up to implementing class what to do - mutable ones can alter
* it's data values, immutable can return a new Vec.
* </p>
*
* @param x
* @param y
* @param z
* @return the result Vec
*/
public abstract V result(double x, double y, double z);
@Override
public V abs()
{
return result(Math.abs(x()), Math.abs(y()), Math.abs(z()));
}
@Override
public V add(Vect vec)
{
return add(vec.x(), vec.y(), vec.z());
}
@Override
public V add(double x, double y)
{
return add(x, y, 0);
}
@Override
public V add(double x, double y, double z)
{
return result(x() + x, y() + y, z() + z);
}
@Override
public V add(Num x, Num y)
{
return add(x, y, Num.ZERO);
}
@Override
public V add(final Num x, final Num y, final Num z)
{
return add(x.value(), y.value(), z.value());
}
@Override
public V half()
{
return mul(0.5);
}
@Override
public V mul(double d)
{
return mul(d, d, d);
}
@Override
public V mul(Vect vec)
{
return mul(vec.x(), vec.y(), vec.z());
}
@Override
public V mul(double x, double y)
{
return mul(x, y, 1);
}
@Override
public V mul(double x, double y, double z)
{
return result(x() * x, y() * y, z() * z);
}
@Override
public V mul(final Num d)
{
return mul(d, d, d);
}
@Override
public V mul(final Num x, final Num y)
{
return mul(x, y, Num.ONE);
}
@Override
public V mul(final Num x, final Num y, final Num z)
{
return mul(x.value(), y.value(), z.value());
}
@Override
public V round()
{
return result(Math.round(x()), Math.round(y()), Math.round(z()));
}
@Override
public V floor()
{
return result(Math.floor(x()), Math.floor(y()), Math.floor(z()));
}
@Override
public V ceil()
{
return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
}
@Override
public V sub(Vect vec)
{
return sub(vec.x(), vec.y(), vec.z());
}
@Override
public V sub(double x, double y)
{
return sub(x, y, 0);
}
@Override
public V sub(double x, double y, double z)
{
return result(x() - x, y() - y, z() - z);
}
@Override
public V sub(Num x, Num y)
{
return sub(x, y, Num.ZERO);
}
@Override
public V sub(final Num x, final Num y, final Num z)
{
return sub(x.value(), y.value(), z.value());
}
@Override
public V neg()
{
return result(-x(), -y(), -z());
}
@Override
public V norm(double size)
{
if (isZero()) return result(x(), y(), z()); // can't norm zero vector
final NumVal k = size().mul(1 / size);
return mul(k);
}
@Override
public NumVal distTo(Vect point)
{
final double dx = x() - point.x();
final double dy = y() - point.y();
final double dz = z() - point.z();
return NumVal.make(Math.sqrt(dx * dx + dy * dy + dz * dz));
}
@Override
public final V midTo(Vect point)
{
final double dx = (point.x() - x()) * 0.5;
final double dy = (point.y() - y()) * 0.5;
final double dz = (point.z() - z()) * 0.5;
return result(dx, dy, dz);
}
@Override
public final V vectTo(Vect point)
{
return result(point.x() - x(), point.y() - y(), point.z() - z());
}
@Override
public final V cross(Vect vec)
{
//@formatter:off
return result(
y() * vec.z() - z() * vec.y(),
z() * vec.x() - x() * vec.z(),
x() * vec.y() - y() * vec.x());
//@formatter:on
}
@Override
public NumVal dot(Vect vec)
{
return NumVal.make(x() * vec.x() + y() * vec.y() + z() * vec.z());
}
@Override
public NumVal size()
{
final double x = x(), y = y(), z = z();
return NumVal.make(Math.sqrt(x * x + y * y + z * z));
}
}

@ -1,81 +1,12 @@
package mightypork.utils.math.vect; package mightypork.utils.math.vect;
import mightypork.utils.annotations.FactoryMethod;
/** /**
* Mutable coord * Mutable coord
* *
* @author MightyPork * @author MightyPork
*/ */
public abstract class VectMutable extends VectView { // returns itself on edit abstract class VectMutable extends Vect {
/**
* Get a variable initialized as zero (0,0,0)
*
* @return new mutable vector
*/
@FactoryMethod
public static VectMutable zero()
{
return make(ZERO);
}
/**
* Get a variable initialized as one (1,1,1)
*
* @return one mutable vector
*/
@FactoryMethod
public static VectMutable one()
{
return make(ONE);
}
/**
* Make new from coords
*
* @param x X coordinate
* @param y Y coordinate
* @return mutable vector
*/
@FactoryMethod
public static VectMutable make(double x, double y)
{
return make(x, y, 0);
}
/**
* Make new as copy of another
*
* @param copied copied vec
* @return mutable vector
*/
@FactoryMethod
public static VectMutable make(Vect copied)
{
return make(copied.x(), copied.y(), copied.z());
}
/**
* Make new from coords
*
* @param x X coordinate
* @param y Y coordinate
* @param z Z coordinate
* @return mutable vector
*/
@FactoryMethod
public static VectMutable make(double x, double y, double z)
{
return new VectMutableImpl(x, y, z);
}
/** /**
* Set all to zeros. * Set all to zeros.

@ -10,21 +10,21 @@ import mightypork.utils.math.num.Num;
* *
* @author MightyPork * @author MightyPork
*/ */
class NumConstrVect extends VectView { class VectNumAdapter extends Vect {
private final Num constrX; private final Num constrX;
private final Num constrY; private final Num constrY;
private final Num constrZ; private final Num constrZ;
public NumConstrVect(Num x, Num y, Num z) { public VectNumAdapter(Num x, Num y, Num z) {
this.constrX = x; this.constrX = x;
this.constrY = y; this.constrY = y;
this.constrZ = z; this.constrZ = z;
} }
public NumConstrVect(Num x, Num y) { public VectNumAdapter(Num x, Num y) {
this.constrX = x; this.constrX = x;
this.constrY = y; this.constrY = y;
this.constrZ = Num.ZERO; this.constrZ = Num.ZERO;

@ -1,35 +0,0 @@
package mightypork.utils.math.vect;
/**
* View of another coordinate, immutable.<br>
* GetVec()
*
* @author MightyPork
*/
class VectProxy extends VectAdapter {
final Vect observed;
/**
* Protected, in order to enforce the use of view() method on Vec, which
* uses caching.
*
* @param observed
*/
public VectProxy(Vect observed) {
assert (!(observed instanceof VectView));
this.observed = observed;
}
@Override
protected Vect getSource()
{
return observed;
}
}

@ -1,145 +0,0 @@
package mightypork.utils.math.vect;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.num.Num;
/**
* Coordinate with immutable numeric values.<br>
* This coordinate is guaranteed to never change, as opposed to view.
*
* @author MightyPork
*/
public final class VectVal extends VectMathStatic<VectVal> {
@SuppressWarnings("hiding")
public static final VectVal ZERO = new VectVal(0, 0, 0);
@SuppressWarnings("hiding")
public static final VectVal ONE = new VectVal(1, 1, 1);
/**
* Make a constant vector
*
* @param value source vector
* @return new constant vec
*/
@FactoryMethod
public static VectVal make(Vect value)
{
return value.copy(); // let the vect handle it
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @return new constant vec
*/
@FactoryMethod
public static VectVal make(double x, double y)
{
return make(x, y, 0);
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @return new constant vec
*/
@FactoryMethod
public static VectVal make(Num x, Num y)
{
return make(x, y, Num.ZERO);
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @param z Z value
* @return new constant vector
*/
@FactoryMethod
public static VectVal make(double x, double y, double z)
{
return new VectVal(x, y, z);
}
/**
* Make a constant vector
*
* @param x X value
* @param y Y value
* @param z Z value
* @return new constant vector
*/
@FactoryMethod
public static VectVal make(Num x, Num y, Num z)
{
return new VectVal(x.value(), y.value(), z.value());
}
private final double x, y, z;
VectVal(Vect other) {
this(other.x(), other.y(), other.z());
}
VectVal(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public double x()
{
return x;
}
@Override
public double y()
{
return y;
}
@Override
public double z()
{
return z;
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public VectVal copy()
{
return this; // it's constant already
}
@Override
public VectVal result(double x, double y, double z)
{
return new VectVal(x, y, z);
}
}

@ -7,7 +7,7 @@ package mightypork.utils.math.vect;
* *
* @author MightyPork * @author MightyPork
*/ */
class VectMutableImpl extends VectMutable { public class VectVar extends VectMutable {
private double x, y, z; private double x, y, z;
@ -17,7 +17,7 @@ class VectMutableImpl extends VectMutable {
* @param y Y coordinate * @param y Y coordinate
* @param z Z coordinate * @param z Z coordinate
*/ */
public VectMutableImpl(double x, double y, double z) { VectVar(double x, double y, double z) {
super(); super();
this.x = x; this.x = x;
this.y = y; this.y = y;

@ -1,78 +0,0 @@
package mightypork.utils.math.vect;
import mightypork.utils.annotations.DefaultImpl;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.num.Num;
/**
* Read-only immutable dynamic view of a coordinate. When the values change,
* it'll be propagated in all derived coords.<br>
* To take a static copy, use the copy() method.
*
* @author MightyPork
*/
public abstract class VectView extends VectMathDynamic { // returns constant value on edit
/**
* Make a proxy view at a vector.
*
* @param observed vector to observe
* @return view
*/
@FactoryMethod
public static VectView make(Vect observed)
{
return observed.view(); // let the vect handle it
}
/**
* Make a view at number constraints, reflecting their future changes.
*
* @param xc X value
* @param yc Y value
* @return view at the values
*/
@FactoryMethod
public static VectView make(Num xc, Num yc)
{
return new NumConstrVect(xc, yc);
}
/**
* Make a view at number constraints, reflecting their future changes.
*
* @param xc X value
* @param yc Y value
* @param zc Z value
* @return view at the values
*/
@FactoryMethod
public static VectView make(Num xc, Num yc, Num zc)
{
return new NumConstrVect(xc, yc, zc);
}
@Override
@DefaultImpl
public double z()
{
return 0; // implemented for ease with 2D anonymous subtypes
}
/**
* @deprectaed No point in taking view of a view.
*/
@Override
@Deprecated
public VectView view()
{
return this;
}
}

@ -5,7 +5,7 @@ import mightypork.utils.logging.Log;
import mightypork.utils.math.Calc; import mightypork.utils.math.Calc;
import mightypork.utils.math.Range; import mightypork.utils.math.Range;
import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.Vect;
import mightypork.utils.math.vect.VectVal; import mightypork.utils.math.vect.VectConst;
/** /**
@ -152,11 +152,11 @@ public class Convert {
* @param def default value * @param def default value
* @return vector * @return vector
*/ */
public static VectVal toVect(Object o, Vect def) public static VectConst toVect(Object o, Vect def)
{ {
try { try {
if (o == null) return def.copy(); if (o == null) return def.freeze();
if (o instanceof Vect) return ((Vect) o).copy(); if (o instanceof Vect) return ((Vect) o).freeze();
if (o instanceof String) { if (o instanceof String) {
String s = ((String) o).trim(); String s = ((String) o).trim();
@ -180,19 +180,19 @@ public class Convert {
final double y = Double.parseDouble(parts[1].trim()); final double y = Double.parseDouble(parts[1].trim());
if (parts.length == 2) { if (parts.length == 2) {
return VectVal.make(x, y); return VectConst.make(x, y);
} }
final double z = Double.parseDouble(parts[2].trim()); final double z = Double.parseDouble(parts[2].trim());
return VectVal.make(x, y, z); return VectConst.make(x, y, z);
} }
} }
} catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) { } catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) {
// ignore // ignore
} }
return def.copy(); return def.freeze();
} }
@ -314,7 +314,7 @@ public class Convert {
* @param o object * @param o object
* @return Coord * @return Coord
*/ */
public static VectVal toVect(Object o) public static VectConst toVect(Object o)
{ {
return toVect(o, Vect.ZERO); return toVect(o, Vect.ZERO);
} }

Loading…
Cancel
Save