diff --git a/src/mightypork/test/TestConstr.java b/src/mightypork/test/TestConstr.java index c05de79..33ee2e6 100644 --- a/src/mightypork/test/TestConstr.java +++ b/src/mightypork/test/TestConstr.java @@ -83,7 +83,7 @@ public class TestConstr { { final NumVar a = Num.makeVar(100); - a.assign(a.mul(50).add(10).div(2)); + a.setTo(a.mul(50).add(10).div(2)); System.out.println(a); diff --git a/src/mightypork/utils/math/constraints/ConstraintFactory.java b/src/mightypork/utils/math/constraints/ConstraintFactory.java index 26b9026..72cb853 100644 --- a/src/mightypork/utils/math/constraints/ConstraintFactory.java +++ b/src/mightypork/utils/math/constraints/ConstraintFactory.java @@ -1,970 +1,970 @@ -//package mightypork.utils.math.constraints; -// -// -//import mightypork.gamecore.control.timing.Poller; -//import mightypork.utils.math.num.Num; -//import mightypork.utils.math.num.Num; -//import mightypork.utils.math.rect.Rect; -//import mightypork.utils.math.rect.RectVal; -//import mightypork.utils.math.rect.RectView; -//import mightypork.utils.math.rect.RectView; -//import mightypork.utils.math.vect.Vect; -//import mightypork.utils.math.vect.VectAdapter; -//import mightypork.utils.math.vect.VectVal; -//import mightypork.utils.math.vect.Vect; -// -// -///** -// * Constraint factory.
-// * Import statically for best experience. -// * -// * @author MightyPork -// */ -//public class ConstraintFactory { -// -// public static RectCache cached(final Poller poller, final RectBound rc) -// { -// return new RectCache(poller, rc); -// } -// -// -// /** -// * Convert {@link Number} to {@link NumBound} if needed -// * -// * @param o unknown numeric value -// * @return converted -// */ -// private static NumBound toNumberBound(final Object o) -// { -// if (o instanceof NumBound) return (NumBound) o; -// -// if (o instanceof Number) return new Num() { -// -// @Override -// public double value() -// { -// return ((Number) o).doubleValue(); -// } -// }; -// -// throw new IllegalArgumentException("Invalid numeric type."); -// } -// -// -// /** -// * Convert {@link Number} or {@link NumBound} to double (current value) -// * -// * @param o unknown numeric value -// * @return double value -// */ -// private static double eval(final Object o) -// { -// return o == null ? 0 : toNumberBound(o).getNum().value(); -// } -// -// -// /** -// * Convert as {@link VectBound} to a {@link Vect} -// * -// * @param vectBound vect bound -// * @return contained vect -// */ -// private static Vect eval(final VectBound vectBound) -// { -// return vectBound == null ? Vect.ZERO : vectBound.getVect(); -// } -// -// -// /** -// * Convert a {@link RectBound} to a {@link Rect} -// * -// * @param rectBound rect bound -// * @return contained rect -// */ -// private static RectView eval(final RectBound rectBound) -// { -// return rectBound == null ? Rect.ZERO : rectBound.getRect(); -// } -// -// -// public static Num min(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.min(eval(a), eval(b)); -// } -// }; -// } -// -// -// public static Num max(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.max(eval(a), eval(b)); -// } -// }; -// } -// -// -// public static Num abs(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.abs(a.value()); -// } -// }; -// } -// -// -// public static Num half(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return a.value() / 2; -// } -// }; -// } -// -// -// public static Num round(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.round(a.value()); -// } -// }; -// } -// -// -// public static RectBound round(final RectBound r) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).round(); -// } -// }; -// } -// -// -// public static Num ceil(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.ceil(a.value()); -// } -// }; -// } -// -// -// public static Num floor(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return Math.floor(a.value()); -// } -// }; -// } -// -// -// public static Num neg(final NumBound a) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return -a.value(); -// } -// }; -// } -// -// -// public static Num add(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return eval(a) + eval(b); -// } -// }; -// } -// -// -// public static Num sub(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return eval(a) - eval(b); -// } -// }; -// } -// -// -// public static Num mul(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return eval(a) * eval(b); -// } -// }; -// } -// -// -// public static Num half(final Object a) -// { -// return mul(a, 0.5); -// } -// -// -// public static Num div(final Object a, final Object b) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return eval(a) / eval(b); -// } -// }; -// } -// -// -// public static Num perc(final Object whole, final Object percent) -// { -// return new Num() { -// -// @Override -// public double value() -// { -// return eval(whole) * (eval(percent) / 100); -// } -// }; -// } -// -// -// public static RectBound row(final RectBound r, final int rows, final int index) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// -// final double height = rv.height(); -// final double perRow = height / rows; -// -// final VectVal origin = rv.origin().add(0, perRow * index); -// final VectVal size = rv.size().setY(perRow); -// -// return RectVal.make(origin, size); -// } -// }; -// } -// -// -// public static RectBound column(final RectBound r, final int columns, final int index) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// -// final double width = rv.width(); -// final double perCol = width / columns; -// -// final VectVal origin = rv.origin().add(perCol * index, 0); -// final VectVal size = rv.size().setX(perCol); -// -// return RectVal.make(origin, size); -// } -// }; -// } -// -// -// public static RectBound tile(final RectBound r, final int rows, final int cols, final int left, final int top) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// -// final double height = rv.height(); -// final double width = rv.height(); -// final double perRow = height / rows; -// final double perCol = width / cols; -// -// final VectVal origin = rv.origin().add(perCol * left, perRow * (rows - top - 1)); -// -// return RectVal.make(origin, perCol, perRow); -// } -// }; -// } -// -// -// public static RectBound shrink(RectBound r, Object shrink) -// { -// return shrink(r, shrink, shrink, shrink, shrink); -// } -// -// -// public static RectBound shrink(RectBound context, Object horiz, Object vert) -// { -// return shrink(context, horiz, vert, horiz, vert); -// } -// -// -// public static RectBound shrink(final RectBound r, final Object left, final Object top, final Object right, final Object bottom) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).shrink(eval(left), eval(top), eval(right), eval(bottom)); -// } -// }; -// } -// -// -// public static RectBound shrinkTop(final RectBound r, final Object shrink) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).shrink(0, eval(shrink), 0, 0); -// } -// }; -// } -// -// -// public static RectBound shrinkBottom(final RectBound r, final Object shrink) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).shrink(0, 0, 0, eval(shrink)); -// } -// }; -// } -// -// -// public static RectBound shrinkLeft(final RectBound r, final Object shrink) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).shrink(eval(shrink), 0, 0, 0); -// } -// }; -// } -// -// -// public static RectBound shrinkRight(final RectBound r, final Object shrink) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).shrink(0, 0, eval(shrink), 0); -// } -// }; -// } -// -// -// public static RectBound grow(RectBound r, Object grow) -// { -// return grow(r, grow, grow, grow, grow); -// } -// -// -// public static RectBound grow(RectBound r, Object horiz, Object vert) -// { -// return grow(r, horiz, vert, horiz, vert); -// } -// -// -// public static RectBound grow(final RectBound r, final Object left, final Object right, final Object top, final Object bottom) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).grow(eval(left), eval(right), eval(top), eval(bottom)); -// } -// }; -// } -// -// -// public static RectBound growUp(final RectBound r, final Object grow) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).grow(0, eval(grow), 0, 0); -// } -// }; -// } -// -// -// public static RectBound growDown(final RectBound r, final Object grow) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).grow(0, 0, 0, eval(grow)); -// } -// }; -// } -// -// -// public static RectBound growLeft(final RectBound r, final Object grow) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).grow(eval(grow), 0, 0, 0); -// } -// }; -// } -// -// -// public static RectBound growRight(final RectBound r, final Object grow) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).grow(0, 0, eval(grow), 0); -// } -// }; -// } -// -// -// public static RectBound box(final Object side) -// { -// return box(side, side); -// } -// -// -// public static RectBound box(final VectBound origin, final Object width, final Object height) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return RectVal.make(eval(origin), eval(width), eval(height)); -// } -// }; -// } -// -// -// public static RectBound box(final Object width, final Object height) -// { -// return box(Vect.ZERO, width, height); -// } -// -// -// public static RectBound box(final RectBound r, final Object width, final Object height) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final RectView origin = eval(r); -// -// return RectVal.make(origin.x(), origin.y(), eval(width), eval(height)); -// } -// }; -// } -// -// -// public static RectBound box(final RectBound r, final Object x, final Object y, final Object width, final Object height) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final VectVal origin = eval(r).origin(); -// -// return RectVal.make(origin.x() + eval(x), origin.y() + eval(y), eval(width), eval(height)); -// } -// }; -// } -// -// -// public static RectBound centerTo(final RectBound r, final RectBound centerTo) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final VectVal size = eval(r).size(); -// final VectVal center = eval(centerTo).center(); -// -// return RectVal.make(center.sub(size.half()), size); -// } -// }; -// } -// -// -// public static RectBound centerTo(final RectBound r, final VectBound centerTo) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final VectVal size = eval(r).size(); -// -// return RectVal.make(eval(centerTo).sub(size.half()), size); -// } -// }; -// } -// -// -// public static RectBound centerTo(final RectBound r, final Object x, final Object y) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final VectVal size = eval(r).size(); -// final VectVal v = VectVal.make(eval(x), eval(y)); -// -// return RectVal.make(v.sub(size.half()), size); -// } -// }; -// } -// -// -// public static RectBound move(final RectBound r, final VectBound move) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).move(eval(move)); -// } -// }; -// } -// -// -// public static RectBound move(final RectBound r, final Object x, final Object y) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// return eval(r).move(eval(x), eval(y)); -// } -// }; -// } -// -// -// /** -// * Make a rect around coord -// * -// * @param c coord -// * @param allSides size to grow on all sides -// * @return rect constraint -// */ -// public static RectBound expand(final VectBound c, final Object allSides) -// { -// return expand(c, allSides, allSides, allSides, allSides); -// } -// -// -// /** -// * Make a rect around coord -// * -// * @param c coord -// * @param horizontal horisontal grow (left, right) -// * @param vertical vertical grow (top, bottom) -// * @return rect constraint -// */ -// public static RectBound expand(final VectBound c, final Object horizontal, final Object vertical) -// { -// return expand(c, horizontal, vertical, horizontal, vertical); -// } -// -// -// /** -// * Make a rect around coord, growing by given amounts -// * -// * @param c coord -// * @param top -// * @param right -// * @param bottom -// * @param left -// * @return rect constraint -// */ -// public static RectBound expand(final VectBound c, final Object top, final Object right, final Object bottom, final Object left) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// final double t = eval(top); -// final double r = eval(right); -// final double b = eval(bottom); -// final double l = eval(left); -// -// final Vect v = eval(c); -// -// return RectVal.make(v.x() - l, v.y() - t, l + r, t + b); -// } -// }; -// } -// -// -// public static RectBound edgeLeft(final RectBound r) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView v = eval(r); -// -// return v.shrink(NumBound.ZERO, NumBound.ZERO, v.width(), NumBound.ZERO); -// } -// }; -// } -// -// -// public static RectBound edgeTop(final RectBound r) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// -// return rv.shrink(NumBound.ZERO,NumBound.ZERO, NumBound.ZERO, rv.height()); -// } -// }; -// } -// -// -// public static RectBound edgeRight(final RectBound r) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// return rv.shrink(rv.width(), NumBound.ZERO, NumBound.ZERO, NumBound.ZERO); -// } -// }; -// } -// -// -// public static RectBound edgeBottom(final RectBound r) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// RectView rv = eval(r); -// return rv.shrink(NumBound.ZERO, rv.height(), NumBound.ZERO, NumBound.ZERO); -// } -// }; -// } -// -// -// public static Vect neg(final VectBound c) -// { -// return mul(c, -1); -// } -// -// -// public static Vect half(final VectBound c) -// { -// return mul(c, 0.5); -// } -// -// -// public static Vect add(final VectBound c1, final VectBound c2) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c1).add(eval(c2)); -// } -// }; -// } -// -// -// public static Vect add(final VectBound c, final Object x, final Object y) -// { -// return add(c, x, y, 0); -// } -// -// -// public static Vect add(final VectBound c, final Object x, final Object y, final Object z) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c).add(eval(x), eval(y), eval(z)); -// } -// }; -// } -// -// -// public static Vect sub(final VectBound c1, final VectBound c2) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c1).sub(eval(c2)); -// } -// }; -// } -// -// -// public static Vect sub(final VectBound c, final Object x, final Object y) -// { -// return sub(c, x, y, 0); -// } -// -// -// public static Vect sub(final VectBound c, final Object x, final Object y, final Object z) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c).sub(eval(x), eval(y), eval(z)); -// } -// -// }; -// } -// -// -// public static Vect mul(final VectBound c, final Object mul) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c).mul(eval(mul)); -// } -// -// }; -// } -// -// -// public static Vect norm(final VectBound c, final Object norm) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(c).norm(eval(norm)); -// } -// -// }; -// } -// -// -// public static Vect origin(final RectBound r) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(r).origin(); -// } -// }; -// } -// -// -// public static Vect size(final RectBound r) -// { -// return new VectAdapter() { -// -// @Override -// public Vect getSource() -// { -// return eval(r).size(); -// } -// }; -// } -// -// -// public static Num height(final RectBound r) -// { -// return size(r).yC(); -// } -// -// -// public static Num width(final RectBound r) -// { -// return size(r).xC(); -// } -// -// -// public static Vect center(final RectBound r) -// { -// return add(origin(r), half(size(r))); -// } -// -// -// public static Vect topLeft(final RectBound r) -// { -// return origin(r); -// } -// -// -// public static Vect topRight(final RectBound r) -// { -// return add(origin(r), width(r), 0); -// } -// -// -// public static Vect bottomLeft(final RectBound r) -// { -// return add(origin(r), 0, width(r)); -// } -// -// -// public static Vect bottomRight(final RectBound r) -// { -// return add(origin(r), size(r)); -// } -// -// -// public static Vect topCenter(final RectBound r) -// { -// return add(origin(r), half(width(r)), 0); -// } -// -// -// public static Vect bottomCenter(final RectBound r) -// { -// return add(origin(r), half(width(r)), width(r)); -// } -// -// -// public static Vect centerLeft(final RectBound r) -// { -// return add(origin(r), 0, half(width(r))); -// } -// -// -// public static Vect centerRight(final RectBound r) -// { -// return add(origin(r), width(r), half(width(r))); -// } -// -// -// /** -// * Zero-sized RectView at given coord -// * -// * @param c coord -// * @return rect -// */ -// public static RectBound zeroRect(final VectBound c) -// { -// return new RectBound() { -// -// @Override -// public RectView getRect() -// { -// Vect cv = eval(c); -// -// return new RectView() { -// -// @Override -// public Vect size() -// { -// return Vect.ZERO; -// } -// -// -// @Override -// public Vect origin() -// { -// return null; -// } -// };RectVal.make.make(cv.x(), cv.y(), 0, 0); -// } -// }; -// } -// -//} +package mightypork.utils.math.constraints; + + +import mightypork.gamecore.control.timing.Poller; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.rect.Rect; +import mightypork.utils.math.rect.RectVal; +import mightypork.utils.math.rect.RectView; +import mightypork.utils.math.rect.RectView; +import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectAdapter; +import mightypork.utils.math.vect.VectVal; +import mightypork.utils.math.vect.Vect; + + +/** + * Constraint factory.
+ * Import statically for best experience. + * + * @author MightyPork + */ +public class ConstraintFactory { + + public static RectCache cached(final Poller poller, final RectBound rc) + { + return new RectCache(poller, rc); + } + + + /** + * Convert {@link Number} to {@link NumBound} if needed + * + * @param o unknown numeric value + * @return converted + */ + private static NumBound toNumberBound(final Object o) + { + if (o instanceof NumBound) return (NumBound) o; + + if (o instanceof Number) return new Num() { + + @Override + public double value() + { + return ((Number) o).doubleValue(); + } + }; + + throw new IllegalArgumentException("Invalid numeric type."); + } + + + /** + * Convert {@link Number} or {@link NumBound} to double (current value) + * + * @param o unknown numeric value + * @return double value + */ + private static double eval(final Object o) + { + return o == null ? 0 : toNumberBound(o).getNum().value(); + } + + + /** + * Convert as {@link VectBound} to a {@link Vect} + * + * @param vectBound vect bound + * @return contained vect + */ + private static Vect eval(final VectBound vectBound) + { + return vectBound == null ? Vect.ZERO : vectBound.getVect(); + } + + + /** + * Convert a {@link RectBound} to a {@link Rect} + * + * @param rectBound rect bound + * @return contained rect + */ + private static RectView eval(final RectBound rectBound) + { + return rectBound == null ? Rect.ZERO : rectBound.getRect(); + } + + + public static Num min(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return Math.min(eval(a), eval(b)); + } + }; + } + + + public static Num max(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return Math.max(eval(a), eval(b)); + } + }; + } + + + public static Num abs(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return Math.abs(a.value()); + } + }; + } + + + public static Num half(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return a.value() / 2; + } + }; + } + + + public static Num round(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return Math.round(a.value()); + } + }; + } + + + public static RectBound round(final RectBound r) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).round(); + } + }; + } + + + public static Num ceil(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return Math.ceil(a.value()); + } + }; + } + + + public static Num floor(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return Math.floor(a.value()); + } + }; + } + + + public static Num neg(final NumBound a) + { + return new Num() { + + @Override + public double value() + { + return -a.value(); + } + }; + } + + + public static Num add(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return eval(a) + eval(b); + } + }; + } + + + public static Num sub(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return eval(a) - eval(b); + } + }; + } + + + public static Num mul(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return eval(a) * eval(b); + } + }; + } + + + public static Num half(final Object a) + { + return mul(a, 0.5); + } + + + public static Num div(final Object a, final Object b) + { + return new Num() { + + @Override + public double value() + { + return eval(a) / eval(b); + } + }; + } + + + public static Num perc(final Object whole, final Object percent) + { + return new Num() { + + @Override + public double value() + { + return eval(whole) * (eval(percent) / 100); + } + }; + } + + + public static RectBound row(final RectBound r, final int rows, final int index) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + + final double height = rv.height(); + final double perRow = height / rows; + + final VectVal origin = rv.origin().add(0, perRow * index); + final VectVal size = rv.size().setY(perRow); + + return RectVal.make(origin, size); + } + }; + } + + + public static RectBound column(final RectBound r, final int columns, final int index) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + + final double width = rv.width(); + final double perCol = width / columns; + + final VectVal origin = rv.origin().add(perCol * index, 0); + final VectVal size = rv.size().setX(perCol); + + return RectVal.make(origin, size); + } + }; + } + + + public static RectBound tile(final RectBound r, final int rows, final int cols, final int left, final int top) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + + final double height = rv.height(); + final double width = rv.height(); + final double perRow = height / rows; + final double perCol = width / cols; + + final VectVal origin = rv.origin().add(perCol * left, perRow * (rows - top - 1)); + + return RectVal.make(origin, perCol, perRow); + } + }; + } + + + public static RectBound shrink(RectBound r, Object shrink) + { + return shrink(r, shrink, shrink, shrink, shrink); + } + + + public static RectBound shrink(RectBound context, Object horiz, Object vert) + { + return shrink(context, horiz, vert, horiz, vert); + } + + + public static RectBound shrink(final RectBound r, final Object left, final Object top, final Object right, final Object bottom) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).shrink(eval(left), eval(top), eval(right), eval(bottom)); + } + }; + } + + + public static RectBound shrinkTop(final RectBound r, final Object shrink) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).shrink(0, eval(shrink), 0, 0); + } + }; + } + + + public static RectBound shrinkBottom(final RectBound r, final Object shrink) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).shrink(0, 0, 0, eval(shrink)); + } + }; + } + + + public static RectBound shrinkLeft(final RectBound r, final Object shrink) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).shrink(eval(shrink), 0, 0, 0); + } + }; + } + + + public static RectBound shrinkRight(final RectBound r, final Object shrink) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).shrink(0, 0, eval(shrink), 0); + } + }; + } + + + public static RectBound grow(RectBound r, Object grow) + { + return grow(r, grow, grow, grow, grow); + } + + + public static RectBound grow(RectBound r, Object horiz, Object vert) + { + return grow(r, horiz, vert, horiz, vert); + } + + + public static RectBound grow(final RectBound r, final Object left, final Object right, final Object top, final Object bottom) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).grow(eval(left), eval(right), eval(top), eval(bottom)); + } + }; + } + + + public static RectBound growUp(final RectBound r, final Object grow) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).grow(0, eval(grow), 0, 0); + } + }; + } + + + public static RectBound growDown(final RectBound r, final Object grow) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).grow(0, 0, 0, eval(grow)); + } + }; + } + + + public static RectBound growLeft(final RectBound r, final Object grow) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).grow(eval(grow), 0, 0, 0); + } + }; + } + + + public static RectBound growRight(final RectBound r, final Object grow) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).grow(0, 0, eval(grow), 0); + } + }; + } + + + public static RectBound box(final Object side) + { + return box(side, side); + } + + + public static RectBound box(final VectBound origin, final Object width, final Object height) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return RectVal.make(eval(origin), eval(width), eval(height)); + } + }; + } + + + public static RectBound box(final Object width, final Object height) + { + return box(Vect.ZERO, width, height); + } + + + public static RectBound box(final RectBound r, final Object width, final Object height) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final RectView origin = eval(r); + + return RectVal.make(origin.x(), origin.y(), eval(width), eval(height)); + } + }; + } + + + public static RectBound box(final RectBound r, final Object x, final Object y, final Object width, final Object height) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final VectVal origin = eval(r).origin(); + + return RectVal.make(origin.x() + eval(x), origin.y() + eval(y), eval(width), eval(height)); + } + }; + } + + + public static RectBound centerTo(final RectBound r, final RectBound centerTo) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final VectVal size = eval(r).size(); + final VectVal center = eval(centerTo).center(); + + return RectVal.make(center.sub(size.half()), size); + } + }; + } + + + public static RectBound centerTo(final RectBound r, final VectBound centerTo) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final VectVal size = eval(r).size(); + + return RectVal.make(eval(centerTo).sub(size.half()), size); + } + }; + } + + + public static RectBound centerTo(final RectBound r, final Object x, final Object y) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final VectVal size = eval(r).size(); + final VectVal v = VectVal.make(eval(x), eval(y)); + + return RectVal.make(v.sub(size.half()), size); + } + }; + } + + + public static RectBound move(final RectBound r, final VectBound move) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).move(eval(move)); + } + }; + } + + + public static RectBound move(final RectBound r, final Object x, final Object y) + { + return new RectBound() { + + @Override + public RectView getRect() + { + return eval(r).move(eval(x), eval(y)); + } + }; + } + + + /** + * Make a rect around coord + * + * @param c coord + * @param allSides size to grow on all sides + * @return rect constraint + */ + public static RectBound expand(final VectBound c, final Object allSides) + { + return expand(c, allSides, allSides, allSides, allSides); + } + + + /** + * Make a rect around coord + * + * @param c coord + * @param horizontal horisontal grow (left, right) + * @param vertical vertical grow (top, bottom) + * @return rect constraint + */ + public static RectBound expand(final VectBound c, final Object horizontal, final Object vertical) + { + return expand(c, horizontal, vertical, horizontal, vertical); + } + + + /** + * Make a rect around coord, growing by given amounts + * + * @param c coord + * @param top + * @param right + * @param bottom + * @param left + * @return rect constraint + */ + public static RectBound expand(final VectBound c, final Object top, final Object right, final Object bottom, final Object left) + { + return new RectBound() { + + @Override + public RectView getRect() + { + final double t = eval(top); + final double r = eval(right); + final double b = eval(bottom); + final double l = eval(left); + + final Vect v = eval(c); + + return RectVal.make(v.x() - l, v.y() - t, l + r, t + b); + } + }; + } + + + public static RectBound edgeLeft(final RectBound r) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView v = eval(r); + + return v.shrink(NumBound.ZERO, NumBound.ZERO, v.width(), NumBound.ZERO); + } + }; + } + + + public static RectBound edgeTop(final RectBound r) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + + return rv.shrink(NumBound.ZERO,NumBound.ZERO, NumBound.ZERO, rv.height()); + } + }; + } + + + public static RectBound edgeRight(final RectBound r) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + return rv.shrink(rv.width(), NumBound.ZERO, NumBound.ZERO, NumBound.ZERO); + } + }; + } + + + public static RectBound edgeBottom(final RectBound r) + { + return new RectBound() { + + @Override + public RectView getRect() + { + RectView rv = eval(r); + return rv.shrink(NumBound.ZERO, rv.height(), NumBound.ZERO, NumBound.ZERO); + } + }; + } + + + public static Vect neg(final VectBound c) + { + return mul(c, -1); + } + + + public static Vect half(final VectBound c) + { + return mul(c, 0.5); + } + + + public static Vect add(final VectBound c1, final VectBound c2) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c1).add(eval(c2)); + } + }; + } + + + public static Vect add(final VectBound c, final Object x, final Object y) + { + return add(c, x, y, 0); + } + + + public static Vect add(final VectBound c, final Object x, final Object y, final Object z) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c).add(eval(x), eval(y), eval(z)); + } + }; + } + + + public static Vect sub(final VectBound c1, final VectBound c2) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c1).sub(eval(c2)); + } + }; + } + + + public static Vect sub(final VectBound c, final Object x, final Object y) + { + return sub(c, x, y, 0); + } + + + public static Vect sub(final VectBound c, final Object x, final Object y, final Object z) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c).sub(eval(x), eval(y), eval(z)); + } + + }; + } + + + public static Vect mul(final VectBound c, final Object mul) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c).mul(eval(mul)); + } + + }; + } + + + public static Vect norm(final VectBound c, final Object norm) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(c).norm(eval(norm)); + } + + }; + } + + + public static Vect origin(final RectBound r) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(r).origin(); + } + }; + } + + + public static Vect size(final RectBound r) + { + return new VectAdapter() { + + @Override + public Vect getSource() + { + return eval(r).size(); + } + }; + } + + + public static Num height(final RectBound r) + { + return size(r).yC(); + } + + + public static Num width(final RectBound r) + { + return size(r).xC(); + } + + + public static Vect center(final RectBound r) + { + return add(origin(r), half(size(r))); + } + + + public static Vect topLeft(final RectBound r) + { + return origin(r); + } + + + public static Vect topRight(final RectBound r) + { + return add(origin(r), width(r), 0); + } + + + public static Vect bottomLeft(final RectBound r) + { + return add(origin(r), 0, width(r)); + } + + + public static Vect bottomRight(final RectBound r) + { + return add(origin(r), size(r)); + } + + + public static Vect topCenter(final RectBound r) + { + return add(origin(r), half(width(r)), 0); + } + + + public static Vect bottomCenter(final RectBound r) + { + return add(origin(r), half(width(r)), width(r)); + } + + + public static Vect centerLeft(final RectBound r) + { + return add(origin(r), 0, half(width(r))); + } + + + public static Vect centerRight(final RectBound r) + { + return add(origin(r), width(r), half(width(r))); + } + + + /** + * Zero-sized RectView at given coord + * + * @param c coord + * @return rect + */ + public static RectBound zeroRect(final VectBound c) + { + return new RectBound() { + + @Override + public RectView getRect() + { + Vect cv = eval(c); + + return new RectView() { + + @Override + public Vect size() + { + return Vect.ZERO; + } + + + @Override + public Vect origin() + { + return null; + } + };RectVal.make.make(cv.x(), cv.y(), 0, 0); + } + }; + } + +} diff --git a/src/mightypork/utils/math/constraints/builder/Bounds.java b/src/mightypork/utils/math/constraints/builder/Bounds.java deleted file mode 100644 index 5b17a08..0000000 --- a/src/mightypork/utils/math/constraints/builder/Bounds.java +++ /dev/null @@ -1,41 +0,0 @@ -//package mightypork.utils.math.constraints.builder; -// -// -//import mightypork.utils.math.constraints.ConstraintFactory; -//import mightypork.utils.math.constraints.RectBound; -//import mightypork.utils.math.constraints.VectBound; -//import mightypork.utils.math.rect.Rect; -//import mightypork.utils.math.vect.Vect; -// -// -//public class Bounds { -// -// public RectBB box(Object side) { -// return wrap(ConstraintFactory.box(side)); -// } -// public RectBB box(VectBound origin, Object width, Object height){ -// return wrap(ConstraintFactory.box(origin, width, height)); -// } -// -// public RectBB box(Object width, Object height){ -// return wrap(ConstraintFactory.box(width, height)); -// } -// -// public RectBB box(Object x, Object y, Object width, Object height){ -// return wrap(ConstraintFactory.box(Rect.ZERO, x,y,width,height)); -// } -// -// public RectBB wrap(RectBound rb) { -// return new RectBB(rb); -// } -// -// public static class RectBB { -// -// private final RectBound parent; -// -// public RectBB(RectBound parent) { -// this.parent = parent; -// } -// -// } -//} diff --git a/src/mightypork/utils/math/num/Num.java b/src/mightypork/utils/math/num/Num.java index 634d74a..56d96ce 100644 --- a/src/mightypork/utils/math/num/Num.java +++ b/src/mightypork/utils/math/num/Num.java @@ -38,7 +38,7 @@ public abstract class Num implements NumBound { @FactoryMethod public static NumVar makeVar(Num copied) { - return new NumVar(eval(copied)); + return new NumVar(copied.value()); } private Num p_ceil; @@ -59,33 +59,20 @@ public abstract class Num implements NumBound { private Num p_abs; - /** - * Convert to double, turning null into zero. - * - * @param a num - * @return double - */ - protected static double eval(final NumBound a) + public NumConst freeze() { - return toNum(a).value(); + return new NumConst(value()); } /** - * Convert {@link NumBound} to {@link Num}, turning null to Num.ZERO. + * Get a snapshot of the current state, to be used for processing. * - * @param a numeric bound - * @return num + * @return digest */ - protected static Num toNum(final NumBound a) + public NumDigest digest() { - return (a == null) ? Num.ZERO : (a.getNum() == null ? Num.ZERO : a.getNum()); - } - - - public NumConst freeze() - { - return new NumConst(value()); + return new NumDigest(this); } @@ -102,27 +89,25 @@ public abstract class Num implements NumBound { public abstract double value(); - public Num abs() + public Num add(final double addend) { - if (p_abs == null) p_abs = new Num() { + return new Num() { - final Num t = Num.this; + private final Num t = Num.this; @Override public double value() { - return Math.abs(t.value()); + return t.value() + addend; } }; - - return p_abs; } - public Num acos() + public Num add(final Num addend) { - if (p_acos == null) p_acos = new Num() { + return new Num() { final Num t = Num.this; @@ -130,31 +115,37 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.acos(t.value()); + return t.value() + addend.value(); } }; - - return p_acos; } - public Num add(final double addend) + public Num sub(final double subtrahend) { - return new Num() { + return add(-subtrahend); + } + + + public Num abs() + { + if (p_abs == null) p_abs = new Num() { - private final Num t = Num.this; + final Num t = Num.this; @Override public double value() { - return t.value() + addend; + return Math.abs(t.value()); } }; + + return p_abs; } - public Num add(final Num addend) + public Num sub(final Num subtrahend) { return new Num() { @@ -164,15 +155,22 @@ public abstract class Num implements NumBound { @Override public double value() { - return t.value() + eval(addend); + return t.value() - subtrahend.value(); } }; } - public Num asin() + public Num div(final double factor) { - if (p_asin == null) p_asin = new Num() { + return mul(1 / factor); + } + + + public Num div(final Num factor) + { + + return new Num() { final Num t = Num.this; @@ -180,34 +178,31 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.asin(t.value()); + return t.value() / factor.value(); } }; - - return p_asin; } - public Num atan() + public Num mul(final double factor) { - if (p_atan == null) p_atan = new Num() { + return new Num() { - final Num t = Num.this; + private final Num t = Num.this; @Override public double value() { - return Math.atan(t.value()); + return t.value() * factor; } }; - - return p_atan; } - public Num average(final double other) + public Num mul(final Num factor) { + return new Num() { final Num t = Num.this; @@ -216,13 +211,13 @@ public abstract class Num implements NumBound { @Override public double value() { - return (t.value() + other) / 2; + return t.value() * factor.value(); } }; } - public Num average(final Num other) + public Num average(final double other) { return new Num() { @@ -232,15 +227,15 @@ public abstract class Num implements NumBound { @Override public double value() { - return (t.value() + eval(other)) / 2; + return (t.value() + other) / 2; } }; } - public Num cbrt() + public Num average(final Num other) { - if (p_cbrt == null) p_cbrt = new Num() { + return new Num() { final Num t = Num.this; @@ -248,17 +243,21 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.cbrt(t.value()); + return (t.value() + other.value()) / 2; } }; - - return p_cbrt; } - public Num ceil() + public Num perc(final double percent) { - if (p_ceil == null) p_ceil = new Num() { + return mul(percent / 100); + } + + + public Num perc(final Num percent) + { + return new Num() { final Num t = Num.this; @@ -266,11 +265,9 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.round(t.value()); + return t.value() * (percent.value() / 100); } }; - - return p_ceil; } @@ -292,9 +289,9 @@ public abstract class Num implements NumBound { } - public Num cube() + public Num acos() { - if (p_cube == null) p_cube = new Num() { + if (p_acos == null) p_acos = new Num() { final Num t = Num.this; @@ -302,25 +299,17 @@ public abstract class Num implements NumBound { @Override public double value() { - final double v = t.value(); - return v * v * v; + return Math.acos(t.value()); } }; - return p_cube; - } - - - public Num div(final double factor) - { - return mul(1 / factor); + return p_acos; } - public Num div(final Num factor) + public Num sin() { - - return new Num() { + if (p_sin == null) p_sin = new Num() { final Num t = Num.this; @@ -328,27 +317,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return t.value() / eval(factor); + return Math.sin(t.value()); } }; + + return p_sin; } - public boolean eq(double other) - { - return value() == other; - } - - - public boolean eq(final Num a) - { - return eq(eval(a)); - } - - - public Num floor() + public Num asin() { - if (p_floor == null) p_floor = new Num() { + if (p_asin == null) p_asin = new Num() { final Num t = Num.this; @@ -356,89 +335,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.floor(t.value()); + return Math.asin(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); + return p_asin; } - public Num max(final double other) + public Num tan() { - return new Num() { + if (p_tan == null) p_tan = new Num() { final Num t = Num.this; @@ -446,15 +353,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.max(t.value(), other); + return Math.tan(t.value()); } }; + + return p_tan; } - public Num max(final Num other) + public Num atan() { - return new Num() { + if (p_atan == null) p_atan = new Num() { final Num t = Num.this; @@ -462,15 +371,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.max(t.value(), eval(other)); + return Math.atan(t.value()); } }; + + return p_atan; } - public Num min(final double other) + public Num cbrt() { - return new Num() { + if (p_cbrt == null) p_cbrt = new Num() { final Num t = Num.this; @@ -478,15 +389,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.min(t.value(), other); + return Math.cbrt(t.value()); } }; + + return p_cbrt; } - public Num min(final Num other) + public Num sqrt() { - return new Num() { + if (p_sqrt == null) p_sqrt = new Num() { final Num t = Num.this; @@ -494,32 +407,35 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.min(t.value(), eval(other)); + return Math.sqrt(t.value()); } }; + + return p_sqrt; } - public Num mul(final double factor) + public Num neg() { - return new Num() { + if (p_neg == null) p_neg = new Num() { - private final Num t = Num.this; + final Num t = Num.this; @Override public double value() { - return t.value() * factor; + return -1 * t.value(); } }; + + return p_neg; } - public Num mul(final Num factor) + public Num round() { - - return new Num() { + if (p_round == null) p_round = new Num() { final Num t = Num.this; @@ -527,15 +443,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return t.value() * eval(factor); + return Math.round(t.value()); } }; + + return p_round; } - public Num neg() + public Num floor() { - if (p_neg == null) p_neg = new Num() { + if (p_floor == null) p_floor = new Num() { final Num t = Num.this; @@ -543,23 +461,17 @@ public abstract class Num implements NumBound { @Override public double value() { - return -1 * t.value(); + return Math.floor(t.value()); } }; - return p_neg; - } - - - public Num perc(final double percent) - { - return mul(percent / 100); + return p_floor; } - public Num perc(final Num percent) + public Num ceil() { - return new Num() { + if (p_ceil == null) p_ceil = new Num() { final Num t = Num.this; @@ -567,9 +479,11 @@ public abstract class Num implements NumBound { @Override public double value() { - return t.value() * (eval(percent) / 100); + return Math.round(t.value()); } }; + + return p_ceil; } @@ -599,15 +513,15 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.pow(t.value(), eval(power)); + return Math.pow(t.value(), power.value()); } }; } - public Num round() + public Num cube() { - if (p_round == null) p_round = new Num() { + if (p_cube == null) p_cube = new Num() { final Num t = Num.this; @@ -615,17 +529,18 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.round(t.value()); + final double v = t.value(); + return v * v * v; } }; - return p_round; + return p_cube; } - public Num signum() + public Num square() { - if (p_sgn == null) p_sgn = new Num() { + if (p_square == null) p_square = new Num() { final Num t = Num.this; @@ -633,17 +548,24 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.signum(t.value()); + final double v = t.value(); + return v * v; } }; - return p_sgn; + return p_square; } - public Num sin() + public Num half() { - if (p_sin == null) p_sin = new Num() { + return mul(0.5); + } + + + public Num max(final double other) + { + return new Num() { final Num t = Num.this; @@ -651,17 +573,15 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.sin(t.value()); + return Math.max(t.value(), other); } }; - - return p_sin; } - public Num sqrt() + public Num max(final Num other) { - if (p_sqrt == null) p_sqrt = new Num() { + return new Num() { final Num t = Num.this; @@ -669,17 +589,15 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.sqrt(t.value()); + return Math.max(t.value(), other.value()); } }; - - return p_sqrt; } - public Num square() + public Num min(final Num other) { - if (p_square == null) p_square = new Num() { + return new Num() { final Num t = Num.this; @@ -687,22 +605,13 @@ public abstract class Num implements NumBound { @Override public double value() { - final double v = t.value(); - return v * v; + return Math.min(t.value(), other.value()); } }; - - return p_square; } - public Num sub(final double subtrahend) - { - return add(-subtrahend); - } - - - public Num sub(final Num subtrahend) + public Num min(final double other) { return new Num() { @@ -712,15 +621,15 @@ public abstract class Num implements NumBound { @Override public double value() { - return t.value() - eval(subtrahend); + return Math.min(t.value(), other); } }; } - public Num tan() + public Num signum() { - if (p_tan == null) p_tan = new Num() { + if (p_sgn == null) p_sgn = new Num() { final Num t = Num.this; @@ -728,11 +637,29 @@ public abstract class Num implements NumBound { @Override public double value() { - return Math.tan(t.value()); + return Math.signum(t.value()); } }; - return p_tan; + return p_sgn; + } + + + public boolean isNegative() + { + return value() < 0; + } + + + public boolean isPositive() + { + return value() > 0; + } + + + public boolean isZero() + { + return value() == 0; } @@ -756,7 +683,7 @@ public abstract class Num implements NumBound { if (!(obj instanceof Num)) return false; final Num other = (Num) obj; - return eq(other); + return value() == other.value(); } diff --git a/src/mightypork/utils/math/num/NumConst.java b/src/mightypork/utils/math/num/NumConst.java index 54a6246..3204280 100644 --- a/src/mightypork/utils/math/num/NumConst.java +++ b/src/mightypork/utils/math/num/NumConst.java @@ -1,17 +1,15 @@ package mightypork.utils.math.num; -import mightypork.utils.math.constraints.NumBound; - - /** - * Constant number {@link NumBound} + * Constant number * * @author MightyPork */ public class NumConst extends Num { private final double value; + private NumDigest digest; NumConst(Num copied) { @@ -32,7 +30,7 @@ public class NumConst extends Num { /** - * No good to copy a constant. + * @deprecated No good to copy a constant. */ @Override @Deprecated @@ -41,6 +39,11 @@ public class NumConst extends Num { return this; } + @Override + public NumDigest digest() + { + return (digest != null) ? digest : (digest = super.digest()); + } @Override public NumConst add(double addend) @@ -49,6 +52,12 @@ public class NumConst extends Num { } + public NumConst add(NumConst addend) + { + return Num.make(value + addend.value); + } + + @Override public NumConst sub(double subtrahend) { @@ -56,6 +65,12 @@ public class NumConst extends Num { } + public NumConst sub(NumConst addend) + { + return Num.make(value - addend.value); + } + + @Override public NumConst mul(double factor) { @@ -63,6 +78,12 @@ public class NumConst extends Num { } + public NumConst mul(NumConst addend) + { + return Num.make(value * addend.value); + } + + @Override public NumConst div(double factor) { @@ -70,6 +91,12 @@ public class NumConst extends Num { } + public NumConst div(NumConst addend) + { + return Num.make(value / addend.value); + } + + @Override public NumConst perc(double percents) { @@ -198,6 +225,12 @@ public class NumConst extends Num { } + public NumConst average(NumConst other) + { + return super.average(other).freeze(); + } + + @Override public NumConst round() { @@ -225,27 +258,4 @@ public class NumConst extends Num { 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); - } } diff --git a/src/mightypork/utils/math/num/NumDigest.java b/src/mightypork/utils/math/num/NumDigest.java new file mode 100644 index 0000000..e9677d0 --- /dev/null +++ b/src/mightypork/utils/math/num/NumDigest.java @@ -0,0 +1,15 @@ +package mightypork.utils.math.num; + + +public class NumDigest { + + public final NumConst source; + + public final double value; + + + public NumDigest(Num num) { + this.value = num.value(); + this.source = num.freeze(); + } +} diff --git a/src/mightypork/utils/math/num/NumMutable.java b/src/mightypork/utils/math/num/NumMutable.java index 56bcc69..b0b1e9d 100644 --- a/src/mightypork/utils/math/num/NumMutable.java +++ b/src/mightypork/utils/math/num/NumMutable.java @@ -21,9 +21,9 @@ public abstract class NumMutable extends Num { * * @param value new value */ - public void assign(Num value) + public void setTo(Num value) { - setTo(eval(value)); + setTo(value); } diff --git a/src/mightypork/utils/math/num/NumVar.java b/src/mightypork/utils/math/num/NumVar.java index 0258118..3219f7f 100644 --- a/src/mightypork/utils/math/num/NumVar.java +++ b/src/mightypork/utils/math/num/NumVar.java @@ -12,7 +12,7 @@ public class NumVar extends NumMutable { public NumVar(Num value) { - this.value = eval(value); + this(value.value()); } diff --git a/src/mightypork/utils/math/rect/Rect.java b/src/mightypork/utils/math/rect/Rect.java index db86dba..ec6186a 100644 --- a/src/mightypork/utils/math/rect/Rect.java +++ b/src/mightypork/utils/math/rect/Rect.java @@ -147,6 +147,10 @@ public abstract class Rect implements RectBound { private Num p_r; private Num p_t; private Num p_b; + private Rect p_edge_l; + private Rect p_edge_r; + private Rect p_edge_t; + private Rect p_edge_b; /** @@ -161,6 +165,17 @@ public abstract class Rect implements RectBound { } + /** + * Get a snapshot of the current state, to be used for processing. + * + * @return digest + */ + public RectDigest digest() + { + return new RectDigest(this); + } + + @Override public Rect getRect() { @@ -313,25 +328,7 @@ public abstract class Rect implements RectBound { */ 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 grow(-left, -right, -top, -bottom); } @@ -574,6 +571,30 @@ public abstract class Rect implements RectBound { } + public Rect leftEdge() + { + return p_edge_l != null ? p_edge_l : (p_edge_l = topLeft().expand(Num.ZERO, Num.ZERO, Num.ZERO, height())); + } + + + public Rect rightEdge() + { + return p_edge_r != null ? p_edge_r : (p_edge_r = topRight().expand(Num.ZERO, Num.ZERO, Num.ZERO, height())); + } + + + public Rect topEdge() + { + return p_edge_t != null ? p_edge_t : (p_edge_t = topLeft().expand(Num.ZERO, width(), Num.ZERO, Num.ZERO)); + } + + + public Rect bottomEdge() + { + return p_edge_b != null ? p_edge_b : (p_edge_b = bottomLeft().expand(Num.ZERO, width(), Num.ZERO, Num.ZERO)); + } + + /** * Center to given point * @@ -623,6 +644,12 @@ public abstract class Rect implements RectBound { } + /** + * Center to given rect's center + * + * @param parent rect to center to + * @return centered + */ public Rect centerTo(Rect parent) { return centerTo(parent.center()); diff --git a/src/mightypork/utils/math/rect/RectAdapter.java b/src/mightypork/utils/math/rect/RectAdapter.java index 0966f33..3a495d5 100644 --- a/src/mightypork/utils/math/rect/RectAdapter.java +++ b/src/mightypork/utils/math/rect/RectAdapter.java @@ -14,7 +14,7 @@ import mightypork.utils.math.vect.VectAdapter; 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. + // (is replaced). This way, references to origin and rect will stay intact. private final VectAdapter originAdapter = new VectAdapter() { diff --git a/src/mightypork/utils/math/rect/RectConst.java b/src/mightypork/utils/math/rect/RectConst.java index 5ce4bce..87dbefb 100644 --- a/src/mightypork/utils/math/rect/RectConst.java +++ b/src/mightypork/utils/math/rect/RectConst.java @@ -28,6 +28,11 @@ public class RectConst extends Rect { private VectConst v_bl; private VectConst v_bc; private RectConst v_round; + private RectConst v_edge_l; + private RectConst v_edge_r; + private RectConst v_edge_t; + private RectConst v_edge_b; + private RectDigest digest; /** @@ -76,7 +81,13 @@ public class RectConst extends Rect { { return this; // already constant } + + @Override + public RectDigest digest() + { + return (digest != null) ? digest : (digest = super.digest()); + } @Override public VectConst origin() @@ -104,12 +115,18 @@ public class RectConst extends Rect { { return Rect.make(pos.add(x, y), size); } + + + public RectConst move(NumConst x, NumConst y) + { + return super.move(x, y).freeze(); + } @Override public RectConst shrink(double left, double right, double top, double bottom) { - return Rect.make(pos.add(left, top), size.sub(left + right, top + bottom)).freeze(); + return super.shrink(left, right, top, bottom).freeze(); } @@ -117,15 +134,14 @@ public class RectConst extends Rect { @Override public RectConst grow(double left, double right, double top, double bottom) { - return Rect.make(pos.sub(left, top), size.add(left + right, top + bottom)).freeze(); + return super.grow(left, right, top, bottom).freeze(); } @Override public RectConst round() { - if (v_round == null) v_round = Rect.make(pos.round(), size.round()); - return v_round; + return (v_round != null) ? v_round : (v_round = Rect.make(pos.round(), size.round())); } @@ -167,8 +183,7 @@ public class RectConst extends Rect { @Override public NumConst right() { - if (v_r == null) v_r = super.right().freeze(); - return v_r; + return (v_r != null) ? v_r : (v_r = super.right().freeze()); } @@ -182,8 +197,7 @@ public class RectConst extends Rect { @Override public NumConst bottom() { - if (v_b == null) v_b = super.bottom().freeze(); - return v_b; + return (v_b != null) ? v_b : (v_b = super.bottom().freeze()); } @@ -197,64 +211,123 @@ public class RectConst extends Rect { @Override public VectConst topCenter() { - if (v_tc == null) v_tc = super.topCenter().freeze(); - return v_tc; + return (v_tc != null) ? v_tc : (v_tc = super.topCenter().freeze()); } @Override public VectConst topRight() { - if (v_tr == null) v_tr = super.topRight().freeze(); - return v_tr; + return (v_tr != null) ? v_tr : (v_tr = super.topRight().freeze()); } @Override public VectConst centerLeft() { - if (v_cl == null) v_cl = super.centerLeft().freeze(); - return v_cl; + return (v_cl != null) ? v_cl : (v_cl = super.centerLeft().freeze()); } @Override public VectConst center() { - if (v_c == null) v_c = super.center().freeze(); - return v_c; + return (v_c != null) ? v_c : (v_c = super.center().freeze()); } @Override public VectConst centerRight() { - if (v_cr == null) v_cr = super.centerRight().freeze(); - return v_cr; + return (v_cr != null) ? v_cr : (v_cr = super.centerRight().freeze()); } @Override public VectConst bottomLeft() { - if (v_bl == null) v_bl = super.bottomLeft().freeze(); - return v_bl; + return (v_bl != null) ? v_bl : (v_bl = super.bottomLeft().freeze()); } @Override public VectConst bottomCenter() { - if (v_bc == null) v_bc = super.bottomCenter().freeze(); - return v_bc; + return (v_bc != null) ? v_bc : (v_bc = super.bottomCenter().freeze()); } @Override public VectConst bottomRight() { - if (v_br == null) v_br = super.bottomRight().freeze(); - return v_br; + return (v_br != null) ? v_br : (v_br = super.bottomRight().freeze()); + } + + + + @Override + public RectConst leftEdge() + { + return (v_edge_l != null) ? v_edge_l : (v_edge_l = super.leftEdge().freeze()); + } + + + @Override + public RectConst rightEdge() + { + return (v_edge_r != null) ? v_edge_r : (v_edge_r = super.rightEdge().freeze()); + } + + + @Override + public RectConst topEdge() + { + return (v_edge_t != null) ? v_edge_t : (v_edge_t = super.topEdge().freeze()); + } + + + @Override + public RectConst bottomEdge() + { + return (v_edge_b != null) ? v_edge_b : (v_edge_b = super.bottomEdge().freeze()); + } + + + @Override + public Rect shrink(Vect shrink) + { + return super.shrink(shrink); + } + + + @Override + public RectConst shrink(double x, double y) + { + return super.shrink(x, y).freeze(); + } + + + public RectConst shrink(NumConst left, NumConst right, NumConst top, NumConst bottom) + { + return super.shrink(left, right, top, bottom).freeze(); + } + + + public RectConst grow(NumConst left, NumConst right, NumConst top, NumConst bottom) + { + return super.grow(left, right, top, bottom).freeze(); + } + + + public RectConst centerTo(VectConst point) + { + return super.centerTo(point).freeze(); + } + + + public RectConst centerTo(RectConst parent) + { + return super.centerTo(parent).freeze(); } } diff --git a/src/mightypork/utils/math/rect/RectDigest.java b/src/mightypork/utils/math/rect/RectDigest.java new file mode 100644 index 0000000..1d369c6 --- /dev/null +++ b/src/mightypork/utils/math/rect/RectDigest.java @@ -0,0 +1,41 @@ +package mightypork.utils.math.rect; + + +import mightypork.utils.math.vect.VectConst; + + +public class RectDigest { + + public final RectConst source; + public final VectConst origin; + public final VectConst size; + + public final double x; + public final double y; + public final double width; + public final double height; + + public final double left; + public final double right; + public final double top; + public final double bottom; + + + public RectDigest(Rect rect) { + this.source = rect.freeze(); + + this.origin = rect.origin().freeze(); + this.size = rect.size().freeze(); + + this.x = rect.x().value(); + this.y = rect.y().value(); + + this.width = rect.width().value(); + this.height = rect.height().value(); + + this.left = rect.left().value(); + this.right = rect.right().value(); + this.top = rect.top().value(); + this.bottom = rect.bottom().value(); + } +} diff --git a/src/mightypork/utils/math/vect/Vect.java b/src/mightypork/utils/math/vect/Vect.java index 5592df7..4b377cf 100644 --- a/src/mightypork/utils/math/vect/Vect.java +++ b/src/mightypork/utils/math/vect/Vect.java @@ -8,6 +8,7 @@ import mightypork.utils.math.constraints.VectBound; import mightypork.utils.math.num.Num; import mightypork.utils.math.num.NumConst; import mightypork.utils.math.rect.Rect; +import mightypork.utils.math.rect.RectDigest; /** @@ -235,6 +236,18 @@ public abstract class Vect implements VectBound { { return new VectConst(this); } + + + + /** + * Get a snapshot of the current state, to be used for processing. + * + * @return digest + */ + public VectDigest digest() + { + return new VectDigest(this); + } /** diff --git a/src/mightypork/utils/math/vect/VectConst.java b/src/mightypork/utils/math/vect/VectConst.java index 2b2008b..5bbb696 100644 --- a/src/mightypork/utils/math/vect/VectConst.java +++ b/src/mightypork/utils/math/vect/VectConst.java @@ -3,6 +3,7 @@ package mightypork.utils.math.vect; import mightypork.utils.math.num.Num; import mightypork.utils.math.num.NumConst; +import mightypork.utils.math.rect.RectConst; /** @@ -25,6 +26,7 @@ public final class VectConst extends Vect { private NumConst v_xc; private NumConst v_yc; private NumConst v_zc; + private VectDigest digest; VectConst(Vect other) { @@ -63,43 +65,40 @@ public final class VectConst extends Vect { /** * @return X constraint */ + @Override public final NumConst xn() { - if (v_xc == null) v_xc = Num.make(this.x); - - return v_xc; + return (v_xc != null) ? v_xc : (v_xc = Num.make(this.x)); } /** * @return Y constraint */ + @Override public final NumConst yn() { - if (v_yc == null) v_yc = Num.make(this.y); - - return v_yc; + return (v_yc != null) ? v_yc : (v_yc = Num.make(this.y)); } /** * @return Z constraint */ + @Override public final NumConst zn() { - - if (v_zc == null) v_zc = Num.make(this.z); - - return v_zc; + return (v_zc != null) ? v_zc : (v_zc = Num.make(this.z)); } /** * @deprecated it's useless to copy a constant */ + @Override @Deprecated public VectConst freeze() @@ -108,70 +107,73 @@ public final class VectConst extends Vect { } + @Override + public VectDigest digest() + { + return (digest != null) ? digest : (digest = super.digest()); + } + + @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())); + return (v_abs != null) ? v_abs : (v_abs = super.abs().freeze()); } @Override public VectConst add(double x, double y) { - return add(x, y, 0); + return super.add(x, y).freeze(); } @Override public VectConst add(double x, double y, double z) { - return Vect.make(x() + x, y() + y, z() + z); + return super.add(x, y, z).freeze(); } @Override public VectConst half() { - if (v_half != null) return v_half; - return v_half = mul(0.5); + return (v_half != null) ? v_half : (v_half = super.half().freeze()); } @Override public VectConst mul(double d) { - return mul(d, d, d); + return super.mul(d).freeze(); } @Override public VectConst mul(double x, double y) { - return mul(x, y, 1); + return super.mul(x, y).freeze(); } @Override public VectConst mul(double x, double y, double z) { - return Vect.make(x() * x, y() * y, z() * z); + return super.mul(x, y, z).freeze(); } @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())); + return (v_round != null) ? v_round : (v_round = super.round().freeze()); } @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())); + return (v_floor != null) ? v_floor : (v_floor = super.floor().freeze()); } @@ -179,50 +181,184 @@ public final class VectConst extends Vect { public VectConst ceil() { if (v_ceil != null) return v_ceil; - return v_ceil = Vect.make(Math.ceil(x()), Math.ceil(y()), Math.ceil(z())); + return v_ceil = super.ceil().freeze(); } @Override public VectConst sub(double x, double y) { - return sub(x, y, 0); + return super.sub(x, y).freeze(); } @Override public VectConst sub(double x, double y, double z) { - return Vect.make(x() - x, y() - y, z() - z); + return super.sub(x, y, z).freeze(); } @Override public VectConst neg() { - if (v_neg != null) return v_neg; - return v_neg = Vect.make(-x(), -y(), -z()); + return (v_neg != null) ? v_neg : (v_neg = super.neg().freeze()); } @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); + return super.norm(size).freeze(); } @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)); + return (v_size != null) ? v_size : (v_size = super.size().freeze()); + } + + + @Override + public VectConst withX(double x) + { + return super.withX(x).freeze(); + } + + + @Override + public VectConst withY(double y) + { + return super.withY(y).freeze(); + } + + + @Override + public VectConst withZ(double z) + { + return super.withZ(z).freeze(); + } + + + public VectConst withX(NumConst x) + { + return super.withX(x).freeze(); + } + + + public VectConst withY(NumConst y) + { + return super.withY(y).freeze(); + } + + + public VectConst withZ(NumConst z) + { + return super.withZ(z).freeze(); + } + + + public VectConst add(VectConst vec) + { + return super.add(vec).freeze(); + } + + + public VectConst add(NumConst x, NumConst y) + { + return super.add(x, y).freeze(); + } + + + public VectConst add(NumConst x, NumConst y, NumConst z) + { + return super.add(x, y, z).freeze(); + } + + + public VectConst mul(VectConst vec) + { + return super.mul(vec).freeze(); + } + + + public VectConst mul(NumConst d) + { + return super.mul(d).freeze(); + } + + + public VectConst mul(NumConst x, NumConst y) + { + return super.mul(x, y).freeze(); + } + + + public VectConst mul(NumConst x, NumConst y, NumConst z) + { + return super.mul(x, y, z).freeze(); + } + + + public VectConst sub(VectConst vec) + { + return super.sub(vec).freeze(); + } + + + public VectConst sub(NumConst x, NumConst y) + { + return super.sub(x, y).freeze(); + } + + + public VectConst sub(NumConst x, NumConst y, NumConst z) + { + return super.sub(x, y, z).freeze(); + } + + + public VectConst norm(NumConst size) + { + return super.norm(size).freeze(); + } + + + public NumConst dist(VectConst point) + { + return super.dist(point).freeze(); + } + + + public VectConst midTo(VectConst point) + { + return super.midTo(point).freeze(); + } + + + public VectConst vectTo(VectConst point) + { + return super.vectTo(point).freeze(); + } + + + public NumConst dot(VectConst vec) + { + return super.dot(vec).freeze(); + } + + + @Override + public RectConst expand(int left, int right, int top, int bottom) + { + return super.expand(left, right, top, bottom).freeze(); + } + + + public RectConst expand(NumConst left, NumConst right, NumConst top, NumConst bottom) + { + return super.expand(left, right, top, bottom).freeze(); } } diff --git a/src/mightypork/utils/math/vect/VectDigest.java b/src/mightypork/utils/math/vect/VectDigest.java new file mode 100644 index 0000000..21d1774 --- /dev/null +++ b/src/mightypork/utils/math/vect/VectDigest.java @@ -0,0 +1,20 @@ +package mightypork.utils.math.vect; + + +public class VectDigest { + + public final VectConst source; + + public final double x; + public final double y; + public final double z; + + + public VectDigest(Vect vect) { + this.source = vect.freeze(); + + this.x = vect.x(); + this.y = vect.y(); + this.z = vect.z(); + } +}