diff --git a/src/mightypork/gamecore/audio/players/LoopPlayer.java b/src/mightypork/gamecore/audio/players/LoopPlayer.java index ef60706..a5dd532 100644 --- a/src/mightypork/gamecore/audio/players/LoopPlayer.java +++ b/src/mightypork/gamecore/audio/players/LoopPlayer.java @@ -109,7 +109,7 @@ public class LoopPlayer extends BaseAudioPlayer implements Updateable, Pauseable fadeAnim.update(delta); - final double gain = getGain(fadeAnim.now()); + final double gain = getGain(fadeAnim.value()); if (!paused && gain != lastUpdateGain) { AL10.alSourcef(sourceID, AL10.AL_GAIN, (float) gain); lastUpdateGain = gain; diff --git a/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java b/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java index a6a9706..9a9bfa6 100644 --- a/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java +++ b/src/mightypork/gamecore/control/bus/events/MouseMotionEvent.java @@ -3,6 +3,7 @@ package mightypork.gamecore.control.bus.events; import mightypork.gamecore.control.bus.events.types.UnloggedEvent; import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectVal; import mightypork.utils.math.vect.VectView; @@ -14,8 +15,8 @@ import mightypork.utils.math.vect.VectView; @UnloggedEvent public class MouseMotionEvent implements Event { - private final VectView move; - private final VectView pos; + private final VectVal move; + private final VectVal pos; /** @@ -31,7 +32,7 @@ public class MouseMotionEvent implements Event { /** * @return movement since last {@link MouseMotionEvent} */ - public VectView getMove() + public VectVal getMove() { return move; } @@ -40,7 +41,7 @@ public class MouseMotionEvent implements Event { /** * @return current mouse position */ - public VectView getPos() + public VectVal getPos() { return pos; } diff --git a/src/mightypork/gamecore/gui/components/AbstractComponent.java b/src/mightypork/gamecore/gui/components/AbstractComponent.java new file mode 100644 index 0000000..de63853 --- /dev/null +++ b/src/mightypork/gamecore/gui/components/AbstractComponent.java @@ -0,0 +1,32 @@ +package mightypork.gamecore.gui.components; + + +import mightypork.gamecore.control.AppAccess; +import mightypork.gamecore.control.AppSubModule; +import mightypork.gamecore.control.bus.BusAccess; +import mightypork.gamecore.control.bus.clients.BusNode; +import mightypork.utils.math.constraints.RectBound; +import mightypork.utils.math.rect.RectView; + + +public abstract class AbstractComponent extends AppSubModule implements PluggableRenderable { + + private RectBound context; + + public AbstractComponent(AppAccess app) { + super(app); + } + + @Override + public void setContext(RectBound context) + { + this.context = context; + } + + @Override + public RectView getRect() + { + return context.getRect(); + } + +} diff --git a/src/mightypork/gamecore/gui/components/PluggableRenderable.java b/src/mightypork/gamecore/gui/components/PluggableRenderable.java index 19300e4..b8fa952 100644 --- a/src/mightypork/gamecore/gui/components/PluggableRenderable.java +++ b/src/mightypork/gamecore/gui/components/PluggableRenderable.java @@ -1,7 +1,7 @@ package mightypork.gamecore.gui.components; -import mightypork.utils.math.constraints.PluggableRect; +import mightypork.utils.math.constraints.PluggableRectBound; import mightypork.utils.math.constraints.RectBound; import mightypork.utils.math.rect.RectView; @@ -11,7 +11,7 @@ import mightypork.utils.math.rect.RectView; * * @author MightyPork */ -public interface PluggableRenderable extends Renderable, PluggableRect { +public interface PluggableRenderable extends Renderable, PluggableRectBound { @Override void render(); diff --git a/src/mightypork/gamecore/gui/components/layout/ElementHolder.java b/src/mightypork/gamecore/gui/components/layout/AbstractLayout.java similarity index 63% rename from src/mightypork/gamecore/gui/components/layout/ElementHolder.java rename to src/mightypork/gamecore/gui/components/layout/AbstractLayout.java index c193f9b..cfd1f25 100644 --- a/src/mightypork/gamecore/gui/components/layout/ElementHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/AbstractLayout.java @@ -5,30 +5,28 @@ import java.util.LinkedList; import mightypork.gamecore.control.AppAccess; import mightypork.gamecore.control.bus.EventBus; -import mightypork.gamecore.control.bus.clients.BusNode; +import mightypork.gamecore.gui.components.AbstractComponent; import mightypork.gamecore.gui.components.PluggableRenderable; -import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.gui.components.Renderable; +import mightypork.gamecore.gui.components.painters.AbstractPainter; import mightypork.utils.math.constraints.RectBound; -import mightypork.utils.math.rect.RectView; /** - * Bag for {@link PluggableRenderer} elements with constraints.
+ * Bag for {@link AbstractPainter} elements with constraints.
* Elements are exposed to {@link EventBus}. * * @author MightyPork */ -public abstract class ElementHolder extends BusNode implements PluggableRenderable { +public abstract class AbstractLayout extends AbstractComponent { - private final LinkedList elements = new LinkedList<>(); - private RectBound context; + final LinkedList elements = new LinkedList<>(); /** * @param app app access */ - public ElementHolder(AppAccess app) { + public AbstractLayout(AppAccess app) { super(app); } @@ -37,35 +35,12 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab * @param app app access * @param context boudning context */ - public ElementHolder(AppAccess app, RectBound context) { + public AbstractLayout(AppAccess app, RectBound context) { super(app); setContext(context); } - @Override - public void setContext(RectBound context) - { - this.context = context; - } - - - @Override - public void render() - { - for (final Renderable element : elements) { - element.render(); - } - } - - - @Override - public RectView getRect() - { - return context.getRect(); - } - - /** * Add element to the holder, setting it's context.
* Element must then be attached using the attach method. @@ -88,4 +63,13 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab addChildClient(elem); } + + @Override + public void render() + { + for (final Renderable element : elements) { + element.render(); + } + } + } diff --git a/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java b/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java index d4171e3..8d23294 100644 --- a/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/ColumnHolder.java @@ -12,7 +12,7 @@ import mightypork.utils.math.constraints.RectBound; * * @author MightyPork */ -public class ColumnHolder extends ElementHolder { +public class ColumnHolder extends AbstractLayout { private final int cols; private int col = 0; diff --git a/src/mightypork/gamecore/gui/components/layout/RowHolder.java b/src/mightypork/gamecore/gui/components/layout/RowHolder.java index 0daf6ef..7000ed2 100644 --- a/src/mightypork/gamecore/gui/components/layout/RowHolder.java +++ b/src/mightypork/gamecore/gui/components/layout/RowHolder.java @@ -12,7 +12,7 @@ import mightypork.utils.math.constraints.RectBound; * * @author MightyPork */ -public class RowHolder extends ElementHolder { +public class RowHolder extends AbstractLayout { private final int rows; private int row = 0; diff --git a/src/mightypork/gamecore/gui/components/PluggableRenderer.java b/src/mightypork/gamecore/gui/components/painters/AbstractPainter.java similarity index 58% rename from src/mightypork/gamecore/gui/components/PluggableRenderer.java rename to src/mightypork/gamecore/gui/components/painters/AbstractPainter.java index be78b78..1a1179e 100644 --- a/src/mightypork/gamecore/gui/components/PluggableRenderer.java +++ b/src/mightypork/gamecore/gui/components/painters/AbstractPainter.java @@ -1,7 +1,9 @@ -package mightypork.gamecore.gui.components; +package mightypork.gamecore.gui.components.painters; -import mightypork.utils.math.constraints.ContextAdapter; +import mightypork.gamecore.gui.components.PluggableRenderable; +import mightypork.gamecore.gui.components.Renderable; +import mightypork.utils.math.constraints.RectBoundAdapter; import mightypork.utils.math.constraints.RectBound; import mightypork.utils.math.rect.RectView; @@ -11,7 +13,7 @@ import mightypork.utils.math.rect.RectView; * * @author MightyPork */ -public abstract class PluggableRenderer extends ContextAdapter implements PluggableRenderable { +public abstract class AbstractPainter extends RectBoundAdapter implements PluggableRenderable { @Override public abstract void render(); diff --git a/src/mightypork/gamecore/gui/components/painters/ImagePainter.java b/src/mightypork/gamecore/gui/components/painters/ImagePainter.java index 5069388..6b229bf 100644 --- a/src/mightypork/gamecore/gui/components/painters/ImagePainter.java +++ b/src/mightypork/gamecore/gui/components/painters/ImagePainter.java @@ -1,7 +1,6 @@ package mightypork.gamecore.gui.components.painters; -import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.render.Render; import mightypork.gamecore.render.textures.TxQuad; @@ -11,7 +10,7 @@ import mightypork.gamecore.render.textures.TxQuad; * * @author MightyPork */ -public class ImagePainter extends PluggableRenderer { +public class ImagePainter extends AbstractPainter { private TxQuad texture; diff --git a/src/mightypork/gamecore/gui/components/painters/QuadPainter.java b/src/mightypork/gamecore/gui/components/painters/QuadPainter.java index 9000e76..8aee05a 100644 --- a/src/mightypork/gamecore/gui/components/painters/QuadPainter.java +++ b/src/mightypork/gamecore/gui/components/painters/QuadPainter.java @@ -1,7 +1,6 @@ package mightypork.gamecore.gui.components.painters; -import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.render.Render; import mightypork.utils.annotations.FactoryMethod; import mightypork.utils.math.color.RGB; @@ -12,7 +11,7 @@ import mightypork.utils.math.color.RGB; * * @author MightyPork */ -public class QuadPainter extends PluggableRenderer { +public class QuadPainter extends AbstractPainter { private final RGB colorHMinVMin; private final RGB colorHMaxVMin; diff --git a/src/mightypork/gamecore/gui/components/painters/TextPainter.java b/src/mightypork/gamecore/gui/components/painters/TextPainter.java index d102ac2..6d29fe9 100644 --- a/src/mightypork/gamecore/gui/components/painters/TextPainter.java +++ b/src/mightypork/gamecore/gui/components/painters/TextPainter.java @@ -1,7 +1,6 @@ package mightypork.gamecore.gui.components.painters; -import mightypork.gamecore.gui.components.PluggableRenderer; import mightypork.gamecore.render.fonts.FontRenderer; import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.gamecore.render.fonts.GLFont; @@ -20,7 +19,7 @@ import mightypork.utils.string.StringProvider.StringWrapper; * * @author MightyPork */ -public class TextPainter extends PluggableRenderer { +public class TextPainter extends AbstractPainter { private final FontRenderer font; private RGB color; diff --git a/src/mightypork/gamecore/render/Render.java b/src/mightypork/gamecore/render/Render.java index 64fea4f..d500430 100644 --- a/src/mightypork/gamecore/render/Render.java +++ b/src/mightypork/gamecore/render/Render.java @@ -10,6 +10,7 @@ import mightypork.utils.files.FileUtils; import mightypork.utils.logging.Log; import mightypork.utils.math.color.RGB; import mightypork.utils.math.rect.Rect; +import mightypork.utils.math.rect.RectView; import mightypork.utils.math.vect.Vect; import mightypork.utils.math.vect.VectVal; import mightypork.utils.math.vect.VectView; @@ -351,10 +352,12 @@ public class Render { */ public static void quad(Rect quad) { - final double x1 = quad.left(); - final double y1 = quad.top(); - final double x2 = quad.right(); - final double y2 = quad.bottom(); + RectView rv = quad.view(); + + final double x1 = rv.left().value(); + final double y1 = rv.top().value(); + final double x2 = rv.right().value(); + final double y2 = rv.bottom().value(); // draw with color unbindTexture(); diff --git a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java index 8632028..3603a0c 100644 --- a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java +++ b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java @@ -6,7 +6,7 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*; import java.util.Random; import mightypork.gamecore.control.timing.Updateable; -import mightypork.gamecore.gui.components.PluggableRenderer; +import mightypork.gamecore.gui.components.painters.AbstractPainter; import mightypork.gamecore.render.Render; import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.Easing; @@ -15,7 +15,7 @@ import mightypork.utils.math.constraints.NumBound; import mightypork.utils.math.constraints.RectBound; -public class BouncyBox extends PluggableRenderer implements Updateable { +public class BouncyBox extends AbstractPainter implements Updateable { private final Random rand = new Random(); diff --git a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java index de95427..ee3514e 100644 --- a/src/mightypork/rogue/screens/test_font/ScreenTestFont.java +++ b/src/mightypork/rogue/screens/test_font/ScreenTestFont.java @@ -23,7 +23,7 @@ public class ScreenTestFont extends Screen { tp = new TextPainter(Res.getFont("default"), Align.CENTER, RGB.GREEN); tp.setText("Hello World!"); - final NumBound fontHeight = mul(getDisplay().getSize().yc(), 0.1); + final NumBound fontHeight = mul(getDisplay().getSize().yn(), 0.1); final RectBound strbox = centerTo(box(fontHeight), this); diff --git a/src/mightypork/test/TestConstr.java b/src/mightypork/test/TestConstr.java index b3e43ec..6fbe357 100644 --- a/src/mightypork/test/TestConstr.java +++ b/src/mightypork/test/TestConstr.java @@ -1,8 +1,5 @@ package mightypork.test; - -import static mightypork.utils.math.constraints.ConstraintFactory.*; - import java.util.Locale; import mightypork.utils.math.rect.RectVal; @@ -16,28 +13,13 @@ public class TestConstr { { Locale.setDefault(Locale.ENGLISH); - final RectView rect = RectVal.make(0, 0, 10, 10); + final RectVal rect = RectVal.make(0, 0, 10, 10); final VectVal point = VectVal.make(50, 50); System.out.println(rect); System.out.println(point); - System.out.println(centerTo(rect, point).getRect()); - -// final RectValue rm = RectValue.make(0, 0, 100, 100); -// System.out.println(rm); -// -// final RectValue added = rm.move(10, 10); -// -// System.out.println(added); -// System.out.println(added.getOrigin()); -// System.out.println(added.getSize()); -// -// System.out.println(added.getOrigin().add(added.getSize())); + System.out.println(rect.view().centerTo(point)); -// VecMutable vv = new MutableCoord(0,0); -// -// System.out.println(vv); -// System.out.println(vv.add(50,50)); } } diff --git a/src/mightypork/test/TestConvert.java b/src/mightypork/test/TestConvert.java index f515bb1..df1a7fa 100644 --- a/src/mightypork/test/TestConvert.java +++ b/src/mightypork/test/TestConvert.java @@ -1,6 +1,8 @@ package mightypork.test; +import java.util.Locale; + import mightypork.utils.objects.Convert; @@ -8,6 +10,8 @@ public class TestConvert { public static void main(String[] args) { + Locale.setDefault(Locale.ENGLISH); + System.out.println(Convert.toVect("(10:20:30)")); System.out.println(Convert.toVect("30.6 ; 80")); System.out.println(Convert.toVect("30.6")); diff --git a/src/mightypork/test/TestCoords.java b/src/mightypork/test/TestCoords.java new file mode 100644 index 0000000..aac38f1 --- /dev/null +++ b/src/mightypork/test/TestCoords.java @@ -0,0 +1,49 @@ +package mightypork.test; + + +import java.util.Locale; + +import mightypork.utils.math.constraints.NumBound; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumView; +import mightypork.utils.math.vect.VectMutable; +import mightypork.utils.math.vect.VectView; + + +public class TestCoords { + + public static void main(String[] args) + { + Locale.setDefault(Locale.ENGLISH); + + // test + VectMutable var = VectMutable.make(1, 2, 3); + + VectView cubicRoot = var.view().mul(var).mul(var); + VectView half = var.view().half(); + + System.out.println("x, x^3, x/5"); + System.out.println(var); + System.out.println(cubicRoot); + System.out.println(half); + + var.mul(10); + + System.out.println("x = x*10; x, x^3, x/5"); + System.out.println(var); + System.out.println(cubicRoot); + System.out.println(half); + + NumView y = var.view().yn(); + System.out.println("y: "+y.value()); + + var.add(100,100); + + System.out.println("x = x*100; x.y(), x, x^3, x/5"); + System.out.println(y.value()); + System.out.println(var); + System.out.println(cubicRoot); + System.out.println(half); + + } +} diff --git a/src/mightypork/utils/config/PropertyManager.java b/src/mightypork/utils/config/PropertyManager.java index eaafe9e..47223b9 100644 --- a/src/mightypork/utils/config/PropertyManager.java +++ b/src/mightypork/utils/config/PropertyManager.java @@ -11,6 +11,7 @@ import java.util.TreeMap; import mightypork.utils.math.Range; import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectVal; import mightypork.utils.math.vect.VectView; import mightypork.utils.objects.Convert; @@ -362,7 +363,7 @@ public class PropertyManager { * @param n key * @return the coord found, or null */ - public VectView getCoord(String n) + public VectVal getCoord(String n) { return Convert.toVect(get(n).value); } diff --git a/src/mightypork/utils/math/animation/AnimDouble.java b/src/mightypork/utils/math/animation/AnimDouble.java index cbf7692..37d0b54 100644 --- a/src/mightypork/utils/math/animation/AnimDouble.java +++ b/src/mightypork/utils/math/animation/AnimDouble.java @@ -5,6 +5,8 @@ import mightypork.gamecore.control.timing.Pauseable; import mightypork.gamecore.control.timing.Updateable; import mightypork.utils.math.Calc; import mightypork.utils.math.constraints.NumBound; +import mightypork.utils.math.num.NumMutable; +import mightypork.utils.math.num.NumVal; /** @@ -12,7 +14,7 @@ import mightypork.utils.math.constraints.NumBound; * * @author MightyPork */ -public class AnimDouble implements Updateable, Pauseable, NumBound { +public class AnimDouble extends NumMutable implements Updateable, Pauseable { /** target double */ protected double to = 0; @@ -32,6 +34,9 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { /** Easing fn */ protected Easing easing = Easing.LINEAR; + /** Default duration (seconds) */ + private double defaultDuration = 0; + /** * Create linear animator @@ -50,7 +55,7 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { * @param easing easing function */ public AnimDouble(double value, Easing easing) { - setTo(value); + this(value); setEasing(easing); } @@ -105,37 +110,55 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { } + /** + * @return current animation duration (seconds) + */ public double getDuration() { return duration; } + /** + * @return elapsed time in current animation (seconds) + */ public double getElapsed() { return elapsedTime; } + /** + * @return default animation duration (seconds) + */ + public double getDefaultDuration() + { + return defaultDuration; + } + + + /** + * @param defaultDuration default animation duration (seconds) + */ + public void setDefaultDuration(double defaultDuration) + { + this.defaultDuration = defaultDuration; + } + + /** * Get value at delta time * * @return the value */ - public double now() + @Override + public double value() { if (duration == 0) return to; return Calc.interpolate(from, to, (elapsedTime / duration), easing); } - @Override - public double getValue() - { - return now(); - } - - /** * Get how much of the animation is already finished * @@ -178,11 +201,13 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { * * @param value */ - public void setTo(double value) + @Override + public AnimDouble setTo(double value) { from = to = value; elapsedTime = 0; - duration = 0; + duration = defaultDuration; + return this; } @@ -199,6 +224,7 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { this.elapsedTime = other.elapsedTime; this.paused = other.paused; this.easing = other.easing; + this.defaultDuration = other.defaultDuration; } @@ -211,7 +237,7 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { */ public void animate(double from, double to, double time) { - final double current = now(); + final double current = value(); this.from = from; this.to = to; @@ -256,7 +282,7 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { */ public void animate(double to, double duration) { - this.from = now(); + this.from = value(); this.to = to; this.duration = duration; this.elapsedTime = 0; @@ -290,7 +316,8 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { * * @return copy */ - public AnimDouble copy() + @Override + public AnimDouble clone() { return new AnimDouble(this); } @@ -320,7 +347,7 @@ public class AnimDouble implements Updateable, Pauseable, NumBound { */ public void stop() { - from = to = now(); + from = to = value(); elapsedTime = 0; duration = 0; } diff --git a/src/mightypork/utils/math/animation/AnimDoubleDeg.java b/src/mightypork/utils/math/animation/AnimDoubleDeg.java index a98bdd7..56a448c 100644 --- a/src/mightypork/utils/math/animation/AnimDoubleDeg.java +++ b/src/mightypork/utils/math/animation/AnimDoubleDeg.java @@ -28,7 +28,7 @@ public class AnimDoubleDeg extends AnimDouble { @Override - public double now() + public double value() { if (duration == 0) return Deg.norm(to); return Calc.interpolateDeg(from, to, (elapsedTime / duration), easing); diff --git a/src/mightypork/utils/math/animation/AnimDoubleRad.java b/src/mightypork/utils/math/animation/AnimDoubleRad.java index e358013..7665664 100644 --- a/src/mightypork/utils/math/animation/AnimDoubleRad.java +++ b/src/mightypork/utils/math/animation/AnimDoubleRad.java @@ -28,7 +28,7 @@ public class AnimDoubleRad extends AnimDouble { @Override - public double now() + public double value() { if (duration == 0) return Rad.norm(to); return Calc.interpolateRad(from, to, (elapsedTime / duration), easing); diff --git a/src/mightypork/utils/math/constraints/ConstraintFactory.java b/src/mightypork/utils/math/constraints/ConstraintFactory.java index 202fff2..1fd78be 100644 --- a/src/mightypork/utils/math/constraints/ConstraintFactory.java +++ b/src/mightypork/utils/math/constraints/ConstraintFactory.java @@ -1,919 +1,970 @@ -package mightypork.utils.math.constraints; - - -import mightypork.gamecore.control.timing.Poller; -import mightypork.utils.math.rect.RectVal; -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.VectView; - - -/** - * 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 NumBound() { - - @Override - public double getValue() - { - 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 toNumberBound(o).getValue(); - } - - - public static NumBound min(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.min(eval(a), eval(b)); - } - }; - } - - - public static NumBound max(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.max(eval(a), eval(b)); - } - }; - } - - - public static NumBound abs(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.abs(a.getValue()); - } - }; - } - - - public static NumBound half(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return a.getValue() / 2; - } - }; - } - - - public static NumBound round(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.round(a.getValue()); - } - }; - } - - - public static RectBound round(final RectBound r) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().round(); - } - }; - } - - - public static NumBound ceil(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.ceil(a.getValue()); - } - }; - } - - - public static NumBound floor(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return Math.floor(a.getValue()); - } - }; - } - - - public static NumBound neg(final NumBound a) - { - return new NumBound() { - - @Override - public double getValue() - { - return -a.getValue(); - } - }; - } - - - public static NumBound add(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return eval(a) + eval(b); - } - }; - } - - - public static NumBound sub(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return eval(a) - eval(b); - } - }; - } - - - public static NumBound mul(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return eval(a) * eval(b); - } - }; - } - - - public static NumBound half(final Object a) - { - return mul(a, 0.5); - } - - - public static NumBound div(final Object a, final Object b) - { - return new NumBound() { - - @Override - public double getValue() - { - return eval(a) / eval(b); - } - }; - } - - - public static NumBound perc(final Object whole, final Object percent) - { - return new NumBound() { - - @Override - public double getValue() - { - 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() - { - final double height = r.getRect().height(); - final double perRow = height / rows; - - final Vect origin = r.getRect().origin().add(0, perRow * index); - final Vect size = r.getRect().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() - { - final double width = r.getRect().width(); - final double perCol = width / columns; - - final Vect origin = r.getRect().origin().add(perCol * index, 0); - final Vect size = r.getRect().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() - { - final double height = r.getRect().height(); - final double width = r.getRect().height(); - final double perRow = height / rows; - final double perCol = width / cols; - - final Vect origin = r.getRect().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 r.getRect().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 r.getRect().shrink(0, eval(shrink), 0, 0); - } - }; - } - - - public static RectBound shrinkBottom(final RectBound r, final Object shrink) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(0, 0, 0, eval(shrink)); - } - }; - } - - - public static RectBound shrinkLeft(final RectBound r, final Object shrink) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(eval(shrink), 0, 0, 0); - } - }; - } - - - public static RectBound shrinkRight(final RectBound r, final Object shrink) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().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 r.getRect().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 r.getRect().grow(0, eval(grow), 0, 0); - } - }; - } - - - public static RectBound growDown(final RectBound r, final Object grow) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().grow(0, 0, 0, eval(grow)); - } - }; - } - - - public static RectBound growLeft(final RectBound r, final Object grow) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().grow(eval(grow), 0, 0, 0); - } - }; - } - - - public static RectBound growRight(final RectBound r, final Object grow) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().grow(0, 0, eval(grow), 0); - } - }; - } - - - public static RectBound box(final Object side) - { - return box(side, side); - } - - - public static RectBound box(final Vect origin, final Object width, final Object height) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return RectVal.make(origin, eval(width), eval(height)); - } - }; - } - - - public static RectBound box(final Object width, final Object height) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return RectVal.make(0, 0, eval(width), eval(height)); - } - }; - } - - - public static RectBound box(final RectBound r, final Object width, final Object height) - { - return new RectBound() { - - @Override - public RectView getRect() - { - final Vect origin = r.getRect().origin(); - - 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 Vect origin = r.getRect().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 VectView size = r.getRect().size(); - final VectView center = centerTo.getRect().center(); - - return RectVal.make(center.sub(size.half()), size); - } - }; - } - - - public static RectBound centerTo(final RectBound r, final Vect centerTo) - { - return new RectBound() { - - @Override - public RectView getRect() - { - final VectView size = r.getRect().size(); - - return RectVal.make(centerTo.copy().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 VectView size = r.getRect().size(); - final VectView v = VectVal.make(eval(x), eval(y)); - - return RectVal.make(v.sub(size.half()), size); - } - }; - } - - - public static RectBound move(final RectBound r, final Vect move) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().move(move); - } - }; - } - - - public static RectBound move(final RectBound r, final Object x, final Object y) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().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 Vect 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 Vect 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 Vect 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); - - return RectVal.make(c.x() - l, c.y() - t, l + r, t + b); - } - }; - } - - - public static RectBound edgeLeft(final RectBound r) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(0, 0, r.getRect().width(), 0); - } - }; - } - - - public static RectBound edgeTop(final RectBound r) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(0, 0, 0, r.getRect().height()); - } - }; - } - - - public static RectBound edgeRight(final RectBound r) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(r.getRect().width(), 0, 0, 0); - } - }; - } - - - public static RectBound edgeBottom(final RectBound r) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return r.getRect().shrink(0, r.getRect().height(), 0, 0); - } - }; - } - - - public static VectView neg(final Vect c) - { - return mul(c, -1); - } - - - public static VectView half(final Vect c) - { - return mul(c, 0.5); - } - - - public static VectView add(final Vect c1, final Vect c2) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c1.copy().add(c2); - } - }; - } - - - public static VectView add(final Vect c, final Object x, final Object y) - { - return add(c, x, y, 0); - } - - - public static VectView add(final Vect c, final Object x, final Object y, final Object z) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c.copy().add(eval(x), eval(y), eval(z)); - } - }; - } - - - public static VectView sub(final Vect c1, final Vect c2) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c1.copy().sub(c2); - } - }; - } - - - public static VectView sub(final Vect c, final Object x, final Object y) - { - return sub(c, x, y, 0); - } - - - public static VectView sub(final Vect c, final Object x, final Object y, final Object z) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c.copy().sub(eval(x), eval(y), eval(z)); - } - - }; - } - - - public static VectView mul(final Vect c, final Object mul) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c.copy().mul(eval(mul)); - } - - }; - } - - - public static VectView norm(final Vect c, final Object norm) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return c.copy().norm(eval(norm)); - } - - }; - } - - - public static VectView origin(final RectBound r) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return r.getRect().origin(); - } - }; - } - - - public static VectView size(final RectBound r) - { - return new VectAdapter() { - - @Override - public VectView getSource() - { - return r.getRect().size(); - } - }; - } - - - public static NumBound height(final RectBound r) - { - return size(r).yc(); - } - - - public static NumBound width(final RectBound r) - { - return size(r).xc(); - } - - - public static VectView center(final RectBound r) - { - return add(origin(r), half(size(r))); - } - - - public static VectView topLeft(final RectBound r) - { - return origin(r); - } - - - public static VectView topRight(final RectBound r) - { - return add(origin(r), width(r), 0); - } - - - public static VectView bottomLeft(final RectBound r) - { - return add(origin(r), 0, width(r)); - } - - - public static VectView bottomRight(final RectBound r) - { - return add(origin(r), size(r)); - } - - - public static VectView topCenter(final RectBound r) - { - return add(origin(r), half(width(r)), 0); - } - - - public static VectView bottomCenter(final RectBound r) - { - return add(origin(r), half(width(r)), width(r)); - } - - - public static VectView centerLeft(final RectBound r) - { - return add(origin(r), 0, half(width(r))); - } - - - public static VectView 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 Vect c) - { - return new RectBound() { - - @Override - public RectView getRect() - { - return RectVal.make(c.x(), c.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.NumView; +//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.VectView; +// +// +///** +// * 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 NumView() { +// +// @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 VectView eval(final VectBound vectBound) +// { +// return vectBound == null ? Vect.ZERO.view() : 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.view() : rectBound.getRect(); +// } +// +// +// public static NumView min(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return Math.min(eval(a), eval(b)); +// } +// }; +// } +// +// +// public static NumView max(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return Math.max(eval(a), eval(b)); +// } +// }; +// } +// +// +// public static NumView abs(final NumBound a) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return Math.abs(a.value()); +// } +// }; +// } +// +// +// public static NumView half(final NumBound a) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return a.value() / 2; +// } +// }; +// } +// +// +// public static NumView round(final NumBound a) +// { +// return new NumView() { +// +// @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 NumView ceil(final NumBound a) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return Math.ceil(a.value()); +// } +// }; +// } +// +// +// public static NumView floor(final NumBound a) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return Math.floor(a.value()); +// } +// }; +// } +// +// +// public static NumView neg(final NumBound a) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return -a.value(); +// } +// }; +// } +// +// +// public static NumView add(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return eval(a) + eval(b); +// } +// }; +// } +// +// +// public static NumView sub(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return eval(a) - eval(b); +// } +// }; +// } +// +// +// public static NumView mul(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return eval(a) * eval(b); +// } +// }; +// } +// +// +// public static NumView half(final Object a) +// { +// return mul(a, 0.5); +// } +// +// +// public static NumView div(final Object a, final Object b) +// { +// return new NumView() { +// +// @Override +// public double value() +// { +// return eval(a) / eval(b); +// } +// }; +// } +// +// +// public static NumView perc(final Object whole, final Object percent) +// { +// return new NumView() { +// +// @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 VectView 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 VectView neg(final VectBound c) +// { +// return mul(c, -1); +// } +// +// +// public static VectView half(final VectBound c) +// { +// return mul(c, 0.5); +// } +// +// +// public static VectView add(final VectBound c1, final VectBound c2) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c1).add(eval(c2)); +// } +// }; +// } +// +// +// public static VectView add(final VectBound c, final Object x, final Object y) +// { +// return add(c, x, y, 0); +// } +// +// +// public static VectView add(final VectBound c, final Object x, final Object y, final Object z) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c).add(eval(x), eval(y), eval(z)); +// } +// }; +// } +// +// +// public static VectView sub(final VectBound c1, final VectBound c2) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c1).sub(eval(c2)); +// } +// }; +// } +// +// +// public static VectView sub(final VectBound c, final Object x, final Object y) +// { +// return sub(c, x, y, 0); +// } +// +// +// public static VectView sub(final VectBound c, final Object x, final Object y, final Object z) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c).sub(eval(x), eval(y), eval(z)); +// } +// +// }; +// } +// +// +// public static VectView mul(final VectBound c, final Object mul) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c).mul(eval(mul)); +// } +// +// }; +// } +// +// +// public static VectView norm(final VectBound c, final Object norm) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(c).norm(eval(norm)); +// } +// +// }; +// } +// +// +// public static VectView origin(final RectBound r) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(r).origin(); +// } +// }; +// } +// +// +// public static VectView size(final RectBound r) +// { +// return new VectAdapter() { +// +// @Override +// public VectView getSource() +// { +// return eval(r).size(); +// } +// }; +// } +// +// +// public static NumView height(final RectBound r) +// { +// return size(r).yC(); +// } +// +// +// public static NumView width(final RectBound r) +// { +// return size(r).xC(); +// } +// +// +// public static VectView center(final RectBound r) +// { +// return add(origin(r), half(size(r))); +// } +// +// +// public static VectView topLeft(final RectBound r) +// { +// return origin(r); +// } +// +// +// public static VectView topRight(final RectBound r) +// { +// return add(origin(r), width(r), 0); +// } +// +// +// public static VectView bottomLeft(final RectBound r) +// { +// return add(origin(r), 0, width(r)); +// } +// +// +// public static VectView bottomRight(final RectBound r) +// { +// return add(origin(r), size(r)); +// } +// +// +// public static VectView topCenter(final RectBound r) +// { +// return add(origin(r), half(width(r)), 0); +// } +// +// +// public static VectView bottomCenter(final RectBound r) +// { +// return add(origin(r), half(width(r)), width(r)); +// } +// +// +// public static VectView centerLeft(final RectBound r) +// { +// return add(origin(r), 0, half(width(r))); +// } +// +// +// public static VectView 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() +// { +// VectView cv = eval(c); +// +// return new RectView() { +// +// @Override +// public VectView size() +// { +// return Vect.ZERO.view(); +// } +// +// +// @Override +// public VectView origin() +// { +// return null; +// } +// };RectVal.make.make(cv.x(), cv.y(), 0, 0); +// } +// }; +// } +// +//} diff --git a/src/mightypork/utils/math/constraints/NumBound.java b/src/mightypork/utils/math/constraints/NumBound.java index 46636bc..0ca18ec 100644 --- a/src/mightypork/utils/math/constraints/NumBound.java +++ b/src/mightypork/utils/math/constraints/NumBound.java @@ -1,20 +1,20 @@ package mightypork.utils.math.constraints; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumVal; +import mightypork.utils.math.num.NumView; + /** * Numeric constraint * * @author MightyPork */ -public interface NumBound { - - public static final NumBound ZERO = new NumberConst(0); - public static final NumBound ONE = new NumberConst(1); - +public interface NumBound { /** * @return current value */ - double getValue(); + Num getNum(); } diff --git a/src/mightypork/utils/math/constraints/NumberConst.java b/src/mightypork/utils/math/constraints/NumberConst.java deleted file mode 100644 index 596b94c..0000000 --- a/src/mightypork/utils/math/constraints/NumberConst.java +++ /dev/null @@ -1,25 +0,0 @@ -package mightypork.utils.math.constraints; - - -/** - * Constant number {@link NumBound} - * - * @author MightyPork - */ -public class NumberConst implements NumBound { - - private final double value; - - - public NumberConst(double value) { - this.value = value; - } - - - @Override - public double getValue() - { - return value; - } - -} diff --git a/src/mightypork/utils/math/constraints/PluggableRect.java b/src/mightypork/utils/math/constraints/PluggableRectBound.java similarity index 61% rename from src/mightypork/utils/math/constraints/PluggableRect.java rename to src/mightypork/utils/math/constraints/PluggableRectBound.java index 1581ed3..bce7532 100644 --- a/src/mightypork/utils/math/constraints/PluggableRect.java +++ b/src/mightypork/utils/math/constraints/PluggableRectBound.java @@ -1,23 +1,16 @@ package mightypork.utils.math.constraints; -import mightypork.utils.math.rect.RectView; - - /** * Interface for constraints that can be assigned context * * @author MightyPork */ -public interface PluggableRect extends RectBound { +public interface PluggableRectBound extends RectBound { /** * @param rect context to set */ abstract void setContext(RectBound rect); - - @Override - abstract RectView getRect(); - } diff --git a/src/mightypork/utils/math/constraints/ContextAdapter.java b/src/mightypork/utils/math/constraints/RectBoundAdapter.java similarity index 50% rename from src/mightypork/utils/math/constraints/ContextAdapter.java rename to src/mightypork/utils/math/constraints/RectBoundAdapter.java index 5e73d3a..61eb549 100644 --- a/src/mightypork/utils/math/constraints/ContextAdapter.java +++ b/src/mightypork/utils/math/constraints/RectBoundAdapter.java @@ -1,16 +1,18 @@ package mightypork.utils.math.constraints; -import mightypork.utils.math.rect.RectView; +import mightypork.utils.math.rect.Rect; +import mightypork.utils.math.rect.RectAdapter; /** - * Basic pluggable context implementation + * Pluggable rect bound adapter * * @author MightyPork */ -public abstract class ContextAdapter implements PluggableRect { - +public abstract class RectBoundAdapter extends RectAdapter implements PluggableRectBound { + + private RectBound backing = null; @@ -20,11 +22,10 @@ public abstract class ContextAdapter implements PluggableRect { this.backing = rect; } - @Override - public RectView getRect() - { + public Rect getSource() { return backing.getRect(); } + } diff --git a/src/mightypork/utils/math/constraints/VectBound.java b/src/mightypork/utils/math/constraints/VectBound.java index c74c569..239773a 100644 --- a/src/mightypork/utils/math/constraints/VectBound.java +++ b/src/mightypork/utils/math/constraints/VectBound.java @@ -2,6 +2,7 @@ package mightypork.utils.math.constraints; import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectView; /** @@ -14,5 +15,5 @@ public interface VectBound { /** * @return the current vector. */ - public Vect getVect(); + public VectView getVect(); } diff --git a/src/mightypork/utils/math/constraints/builder/Bounds.java b/src/mightypork/utils/math/constraints/builder/Bounds.java index af75a28..5b17a08 100644 --- a/src/mightypork/utils/math/constraints/builder/Bounds.java +++ b/src/mightypork/utils/math/constraints/builder/Bounds.java @@ -1,19 +1,41 @@ -package mightypork.utils.math.constraints.builder; - - -import mightypork.utils.math.constraints.RectBound; - - -public class Bounds { - - public static class RectBB { - - private final RectBound parent; - - - public RectBB(RectBound parent) { - this.parent = parent; - } - - } -} +//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/AbstractNum.java b/src/mightypork/utils/math/num/AbstractNum.java new file mode 100644 index 0000000..fc09e83 --- /dev/null +++ b/src/mightypork/utils/math/num/AbstractNum.java @@ -0,0 +1,23 @@ +package mightypork.utils.math.num; + + +public abstract class AbstractNum implements Num { + + @Override + public Num getNum() { + return this; + } + + @Override + public NumView view() + { + return new NumProxy(this); + } + + @Override + public NumVal copy() + { + return new NumVal(this); + } + +} diff --git a/src/mightypork/utils/math/num/Num.java b/src/mightypork/utils/math/num/Num.java new file mode 100644 index 0000000..81a9481 --- /dev/null +++ b/src/mightypork/utils/math/num/Num.java @@ -0,0 +1,20 @@ +package mightypork.utils.math.num; + + +import mightypork.utils.math.constraints.NumBound; + + +public interface Num extends NumBound { + + Num ZERO = NumVal.make(0); + Num ONE = NumVal.make(1); + + + double value(); + + + NumView view(); + + + NumVal copy(); +} diff --git a/src/mightypork/utils/math/num/NumAdapter.java b/src/mightypork/utils/math/num/NumAdapter.java new file mode 100644 index 0000000..1a260f7 --- /dev/null +++ b/src/mightypork/utils/math/num/NumAdapter.java @@ -0,0 +1,14 @@ +package mightypork.utils.math.num; + + +public abstract class NumAdapter extends NumView { + + protected abstract Num getSource(); + + @Override + public double value() + { + return getSource().value(); + } + +} diff --git a/src/mightypork/utils/math/num/NumMath.java b/src/mightypork/utils/math/num/NumMath.java new file mode 100644 index 0000000..f6eb108 --- /dev/null +++ b/src/mightypork/utils/math/num/NumMath.java @@ -0,0 +1,157 @@ +package mightypork.utils.math.num; + + +/** + * Math operations for numbers + * + * @author MightyPork + * @param functions return type + */ +interface NumMath> 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(); +} diff --git a/src/mightypork/utils/math/num/NumMathBase.java b/src/mightypork/utils/math/num/NumMathBase.java new file mode 100644 index 0000000..dd0e64a --- /dev/null +++ b/src/mightypork/utils/math/num/NumMathBase.java @@ -0,0 +1,151 @@ +package mightypork.utils.math.num; + +import mightypork.utils.math.constraints.NumBound; + + +public abstract class NumMathBase> extends AbstractNum implements NumMath { + + /** + * 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; + NumMathBase other = (NumMathBase) obj; + + return eq(other); + } +} diff --git a/src/mightypork/utils/math/num/NumMathDynamic.java b/src/mightypork/utils/math/num/NumMathDynamic.java new file mode 100644 index 0000000..7f28b42 --- /dev/null +++ b/src/mightypork/utils/math/num/NumMathDynamic.java @@ -0,0 +1,558 @@ +package mightypork.utils.math.num; + + +public abstract class NumMathDynamic extends NumMathBase{ + + 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 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 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; + } + }; + } +} diff --git a/src/mightypork/utils/math/num/NumMathStatic.java b/src/mightypork/utils/math/num/NumMathStatic.java new file mode 100644 index 0000000..8bd9c41 --- /dev/null +++ b/src/mightypork/utils/math/num/NumMathStatic.java @@ -0,0 +1,261 @@ +package mightypork.utils.math.num; + + +public abstract class NumMathStatic> extends NumMathBase { + + 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 String toString() + { + return String.format("{%.1f}", value()); + } +} diff --git a/src/mightypork/utils/math/num/NumMutable.java b/src/mightypork/utils/math/num/NumMutable.java new file mode 100644 index 0000000..5dea2b2 --- /dev/null +++ b/src/mightypork/utils/math/num/NumMutable.java @@ -0,0 +1,104 @@ +package mightypork.utils.math.num; + + +import mightypork.utils.annotations.FactoryMethod; +import mightypork.utils.math.constraints.NumBound; + + +/** + * Mutable numeric variable + * + * @author MightyPork + */ +public abstract class NumMutable extends NumMathStatic { + + /** + * 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)); + } + + + @Override + protected NumMutable result(double a) + { + return setTo(a); + } + + + /** + * Assign a value + * + * @param value new value + * @return this + */ + public NumMutable set(Num value) + { + return setTo(eval(value)); + } + + + /** + * Assign a value + * + * @param value new value + * @return this + */ + public abstract NumMutable setTo(double value); + +} diff --git a/src/mightypork/utils/math/num/NumMutableImpl.java b/src/mightypork/utils/math/num/NumMutableImpl.java new file mode 100644 index 0000000..e6742e7 --- /dev/null +++ b/src/mightypork/utils/math/num/NumMutableImpl.java @@ -0,0 +1,38 @@ +package mightypork.utils.math.num; + + +/** + * Mutable numeric variable. + * + * @author MightyPork + */ +class NumMutableImpl extends NumMutable { + + private double value; + + + public NumMutableImpl(Num value) { + this.value = eval(value); + } + + + public NumMutableImpl(double value) { + this.value = value; + } + + + @Override + public double value() + { + return value; + } + + + @Override + public NumMutable setTo(double value) + { + this.value = value; + return this; + } + +} diff --git a/src/mightypork/utils/math/num/NumProxy.java b/src/mightypork/utils/math/num/NumProxy.java new file mode 100644 index 0000000..131d431 --- /dev/null +++ b/src/mightypork/utils/math/num/NumProxy.java @@ -0,0 +1,19 @@ +package mightypork.utils.math.num; + + +public class NumProxy extends NumAdapter { + + private final Num observed; + + + public NumProxy(Num observed) { + this.observed = observed; + } + + @Override + protected Num getSource() + { + return observed; + } + +} diff --git a/src/mightypork/utils/math/num/NumVal.java b/src/mightypork/utils/math/num/NumVal.java new file mode 100644 index 0000000..274b7bc --- /dev/null +++ b/src/mightypork/utils/math/num/NumVal.java @@ -0,0 +1,95 @@ +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 { + + @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; + + + public NumVal(Num copied) { + this.value = copied.value(); + } + + + public 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(); + } + +} diff --git a/src/mightypork/utils/math/num/NumView.java b/src/mightypork/utils/math/num/NumView.java new file mode 100644 index 0000000..514ab74 --- /dev/null +++ b/src/mightypork/utils/math/num/NumView.java @@ -0,0 +1,16 @@ +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 + } + +} diff --git a/src/mightypork/utils/math/rect/AbstractRect.java b/src/mightypork/utils/math/rect/AbstractRect.java index 9e0de1e..678a996 100644 --- a/src/mightypork/utils/math/rect/AbstractRect.java +++ b/src/mightypork/utils/math/rect/AbstractRect.java @@ -1,10 +1,6 @@ package mightypork.utils.math.rect; -import mightypork.utils.math.vect.Vect; -import mightypork.utils.math.vect.VectVal; - - /** * Abstract {@link Rect}, implementing all but the data getters * @@ -12,7 +8,7 @@ import mightypork.utils.math.vect.VectVal; */ public abstract class AbstractRect implements Rect { - private RectProxy proxy; + private RectProxy proxy; @Override @@ -22,130 +18,10 @@ public abstract class AbstractRect implements Rect { } - @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().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().x(), size().y()); - } - - - @Override - public double x() - { - return origin().x(); - } - - - @Override - public double y() - { - return origin().y(); - } - - - @Override - public double width() - { - return size().x(); - } - - - @Override - public double height() - { - return size().y(); - } - - - @Override - public double left() - { - return origin().x(); - } - - - @Override - public double right() - { - return origin().x() + size().x(); - } - - - @Override - public double top() - { - return origin().y(); - } - - - @Override - public double bottom() - { - return origin().y() + size().y(); - } - - @Override public RectView view() { - // must NOT call VectView.make, it'd cause infinite recursion. - + // must NOT call RectView.make, it'd cause infinite recursion. if (proxy == null) proxy = new RectProxy(this); return proxy; @@ -156,29 +32,17 @@ public abstract class AbstractRect implements Rect { public RectVal copy() { // must NOT call RectVal.make, it'd cause infinite recursion. - return new RectVal(x(), y(), width(), height()); - } - - - @Override - 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; + return new RectVal(this); } @Override public String toString() { - return String.format("Rect { %s - %s }", topLeft(), bottomRight()); + return String.format( + "Rect { %s - %s }", + origin(), + origin().view().add(size())); } } diff --git a/src/mightypork/utils/math/rect/Rect.java b/src/mightypork/utils/math/rect/Rect.java index a769dfe..542cb2a 100644 --- a/src/mightypork/utils/math/rect/Rect.java +++ b/src/mightypork/utils/math/rect/Rect.java @@ -2,8 +2,8 @@ package mightypork.utils.math.rect; import mightypork.utils.math.constraints.RectBound; +import mightypork.utils.math.num.Num; import mightypork.utils.math.vect.Vect; -import mightypork.utils.math.vect.VectVal; /** @@ -13,8 +13,8 @@ import mightypork.utils.math.vect.VectVal; */ public interface Rect extends RectBound { - RectVal ONE = new RectVal(0, 0, 1, 1); - RectVal ZERO = new RectVal(0, 0, 0, 0); + Rect ZERO = new RectVal(0, 0, 0, 0); + Rect ONE = new RectVal(0, 0, 1, 1); /** @@ -34,125 +34,119 @@ public interface Rect extends RectBound { /** + * Origin (top left). + * * @return origin (top left) */ - VectVal origin(); + Vect origin(); /** + * Size (spanning right down from Origin). + * * @return size vector */ - VectVal size(); + Vect size(); /** * @return current width */ - double width(); + public abstract Num width(); /** * @return current height */ - double height(); + public abstract Num height(); /** * @return origin X */ - double x(); + public abstract Num x(); /** * @return origin Y */ - double y(); + public abstract Num y(); /** * @return left X (low) */ - double left(); + public abstract Num left(); /** * @return right X (high) */ - double right(); + public abstract Num right(); /** * @return top Y (low) */ - double top(); + public abstract Num top(); /** * @return bottom Y (high) */ - double bottom(); + public abstract Num bottom(); /** * @return top left corner position */ - VectVal topLeft(); + public abstract Vect topLeft(); /** * @return top center position */ - VectVal topCenter(); + public abstract Vect topCenter(); /** * @return top right corner position */ - VectVal topRight(); + public abstract Vect topRight(); /** * @return left center position */ - VectVal centerLeft(); + public abstract Vect centerLeft(); /** * @return center position */ - VectVal center(); + public abstract Vect center(); /** * @return right center position */ - VectVal centerRight(); + public abstract Vect centerRight(); /** * @return bottom left corner position */ - VectVal bottomLeft(); + public abstract Vect bottomLeft(); /** * @return bottom center position */ - VectVal bottomCenter(); + public abstract Vect bottomCenter(); /** * @return bottom right corner position */ - VectVal bottomRight(); - - - /** - * Check if point is inside this rectangle - * - * @param point point to test - * @return is inside - */ - boolean contains(Vect point); - + public abstract Vect bottomRight(); } diff --git a/src/mightypork/utils/math/rect/RectAdapter.java b/src/mightypork/utils/math/rect/RectAdapter.java new file mode 100644 index 0000000..e9ed2ac --- /dev/null +++ b/src/mightypork/utils/math/rect/RectAdapter.java @@ -0,0 +1,55 @@ +package mightypork.utils.math.rect; + + +import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectAdapter; +import mightypork.utils.math.vect.VectView; + + +/** + * Rect proxy with abstract method for plugging in / generating rect + * dynamically. + * + * @author MightyPork + */ +public abstract class RectAdapter extends RectView { + + private VectAdapter originAdapter = new VectAdapter() { + + @Override + protected Vect getSource() + { + return RectAdapter.this.getSource().origin(); + } + }; + + private VectAdapter sizeAdapter = new VectAdapter() { + + @Override + protected Vect getSource() + { + return RectAdapter.this.getSource().size(); + } + }; + + + /** + * @return the proxied coord + */ + protected abstract Rect getSource(); + + + @Override + public VectView origin() + { + return originAdapter; + } + + + @Override + public VectView size() + { + return sizeAdapter; + } + +} diff --git a/src/mightypork/utils/math/rect/RectMath.java b/src/mightypork/utils/math/rect/RectMath.java index 5b3c444..744f64c 100644 --- a/src/mightypork/utils/math/rect/RectMath.java +++ b/src/mightypork/utils/math/rect/RectMath.java @@ -1,10 +1,96 @@ package mightypork.utils.math.rect; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumView; import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectAdapter; +import mightypork.utils.math.vect.VectView; -abstract class RectMath extends AbstractRect { +abstract class RectMath extends AbstractRect { + + protected NumView p_x; + protected NumView p_y; + protected NumView p_width; + protected NumView p_height; + + protected NumView p_left; + protected NumView p_top; + protected NumView p_right; + protected NumView p_bottom; + + protected VectView p_tl; + protected VectView p_tc; + protected VectView p_tr; + + protected VectView p_cl; + protected VectView p_cc; + protected VectView p_cr; + + protected VectView p_bl; + protected VectView p_bc; + protected VectView p_br; + + protected VectView p_origin; + protected VectView p_size; + + + public RectMath() { + + p_origin = new VectAdapter() { + + @Override + protected Vect getSource() + { + return RectMath.this.origin(); + } + }; + + p_size = new VectAdapter() { + + @Override + protected Vect getSource() + { + return RectMath.this.size(); + } + }; + + // setup proxies + + // origin, size disassembled + p_width = p_size.xn(); + p_height = p_size.yn(); + p_x = p_origin.xn(); + p_y = p_origin.yn(); + + // coordinates + p_top = p_y; + p_left = p_x; + p_right = p_left.add(p_width); + p_bottom = p_top.add(p_height); + + // corners + // -- top line -- + Num width_half = p_width.half(); + Num ya = Num.ZERO; + p_tl = p_origin; + p_tc = p_origin.add(width_half, ya); + p_tr = p_origin.add(p_width, ya); + + // --center line-- + ya = p_height.half(); + p_cl = p_origin.add(Num.ZERO, ya); + p_cc = p_origin.add(width_half, ya); + p_cr = p_origin.add(p_width, ya); + + // -- bottom line -- + ya = p_height; + p_bl = p_origin.add(Num.ZERO, ya); + p_bc = p_origin.add(width_half, ya); + p_br = p_origin.add(p_width, ya); + } + /** * Add vector to origin @@ -12,10 +98,7 @@ abstract class RectMath extends AbstractRect { * @param move offset vector * @return result */ - public T move(Vect move) - { - return move(move.x(), move.y()); - } + public abstract R move(Vect move); /** @@ -25,7 +108,7 @@ abstract class RectMath extends AbstractRect { * @param y y to add * @return result */ - public abstract T move(double x, double y); + public abstract R move(double x, double y); /** @@ -35,7 +118,7 @@ abstract class RectMath extends AbstractRect { * @return result */ - public T shrink(Vect shrink) + public R shrink(Vect shrink) { return shrink(shrink.x(), shrink.y()); } @@ -48,7 +131,7 @@ abstract class RectMath extends AbstractRect { * @param y vertical shrink * @return result */ - public T shrink(double x, double y) + public R shrink(double x, double y) { return shrink(x, x, y, y); } @@ -63,7 +146,7 @@ abstract class RectMath extends AbstractRect { * @param bottom shrink * @return result */ - public abstract T shrink(double left, double right, double top, double bottom); + public abstract R shrink(double left, double right, double top, double bottom); /** @@ -72,7 +155,7 @@ abstract class RectMath extends AbstractRect { * @param grow grow size (added to each side) * @return grown copy */ - public final T grow(Vect grow) + public final R grow(Vect grow) { return grow(grow.x(), grow.y()); } @@ -85,7 +168,7 @@ abstract class RectMath extends AbstractRect { * @param y vertical grow * @return result */ - public final T grow(double x, double y) + public final R grow(double x, double y) { return grow(x, x, y, y); } @@ -100,7 +183,7 @@ abstract class RectMath extends AbstractRect { * @param bottom growth * @return result */ - public abstract T grow(double left, double right, double top, double bottom); + public abstract R grow(double left, double right, double top, double bottom); /** @@ -108,5 +191,34 @@ abstract class RectMath extends AbstractRect { * * @return result */ - public abstract T round(); + 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; + } } diff --git a/src/mightypork/utils/math/rect/RectMathDynamic.java b/src/mightypork/utils/math/rect/RectMathDynamic.java new file mode 100644 index 0000000..b8687b0 --- /dev/null +++ b/src/mightypork/utils/math/rect/RectMathDynamic.java @@ -0,0 +1,360 @@ +package mightypork.utils.math.rect; + + +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumView; +import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectView; + + +public abstract class RectMathDynamic extends RectMath { + + @Override + public abstract VectView origin(); + + + @Override + public abstract VectView size(); + + + @Override + public RectView move(final Vect move) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size; + } + + + @Override + public VectView origin() + { + return t.p_origin.add(move); + } + + }; + } + + + @Override + public RectView move(final double xd, final double yd) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size; + } + + + @Override + public VectView origin() + { + return t.p_origin.add(xd, yd); + } + + }; + } + + + public RectView move(final Num xd, final Num yd) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size; + } + + + @Override + public VectView origin() + { + return t.p_origin.add(xd, yd); + } + + }; + } + + + @Override + public RectView shrink(final double leftd, final double rightd, final double topd, final double bottomd) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size.sub(leftd + rightd, topd + bottomd); + } + + + @Override + public VectView origin() + { + return t.p_origin.add(leftd, topd); + } + + }; + } + + + @Override + public RectView grow(final double leftd, final double rightd, final double topd, final double bottomd) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size.add(leftd + rightd, topd + bottomd); + } + + + @Override + public VectView origin() + { + return t.p_origin.sub(leftd, topd); + } + + }; + } + + + public RectView shrink(final Num leftd, final Num rightd, final Num topd, final Num bottomd) + { + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size.sub(leftd.view().add(rightd), topd.view().add(bottomd)); + } + + + @Override + public VectView origin() + { + return t.p_origin.add(leftd, topd); + } + + }; + } + + + public RectView grow(final Num leftd, final Num rightd, final Num topd, final Num bottomd) + { + + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size.add(leftd.view().add(rightd), topd.view().add(bottomd)); + } + + + @Override + public VectView origin() + { + return t.p_origin.sub(leftd, topd); + } + + }; + } + + + @Override + public RectView round() + { + + return new RectView() { + + private RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size.round(); + } + + + @Override + public VectView origin() + { + return t.p_origin.round(); + } + + }; + } + + + @Override + public NumView x() + { + return p_x; + } + + + @Override + public NumView y() + { + return p_y; + } + + + @Override + public NumView width() + { + return p_width; + } + + + @Override + public NumView height() + { + return p_height; + } + + + @Override + public NumView left() + { + return p_left; + } + + + @Override + public NumView right() + { + return p_right; + } + + + @Override + public NumView top() + { + return p_top; + } + + + @Override + public NumView bottom() + { + return p_bottom; + } + + + @Override + public VectView topLeft() + { + return p_tl; + } + + + @Override + public VectView topCenter() + { + return p_tc; + } + + + @Override + public VectView topRight() + { + return p_tr; + } + + + @Override + public VectView centerLeft() + { + return p_cl; + } + + + @Override + public VectView center() + { + return p_cc; + } + + + @Override + public VectView centerRight() + { + return p_cr; + } + + + @Override + public VectView bottomLeft() + { + return p_bl; + } + + + @Override + public VectView bottomCenter() + { + return p_bc; + } + + + @Override + public VectView bottomRight() + { + return p_br; + } + + + @Override + public RectView centerTo(final Vect point) + { + return new RectView() { + + RectMathDynamic t = RectMathDynamic.this; + + + @Override + public VectView size() + { + return t.p_size; + } + + + @Override + public VectView origin() + { + return point.view().sub(t.p_size.half()); + } + }; + } +} diff --git a/src/mightypork/utils/math/rect/RectMathStatic.java b/src/mightypork/utils/math/rect/RectMathStatic.java new file mode 100644 index 0000000..663d2eb --- /dev/null +++ b/src/mightypork/utils/math/rect/RectMathStatic.java @@ -0,0 +1,179 @@ +package mightypork.utils.math.rect; + + +import mightypork.utils.math.num.NumVal; +import mightypork.utils.math.vect.Vect; +import mightypork.utils.math.vect.VectVal; + + +public abstract class RectMathStatic> extends RectMath { + + @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(p_origin.add(x,y), p_size); + } + + + @Override + public R shrink(double left, double right, double top, double bottom) + { + return result(p_origin.add(left, top), p_size.sub(left + right, top + bottom)); + } + + + @Override + public R grow(double left, double right, double top, double bottom) + { + return result(p_origin.sub(left, top), p_size.add(left + right, top + bottom)); + } + + + @Override + public R centerTo(final Vect point) + { + return result(p_origin.sub(p_size.half()), p_size); + } + + + @Override + public R round() + { + return result(p_origin.round(), p_size.round()); + } + + + protected abstract R result(Vect newOrigin, Vect newSize); + + + @Override + public NumVal x() + { + return p_x.copy(); + } + + + @Override + public NumVal y() + { + return p_y.copy(); + } + + + @Override + public NumVal width() + { + return p_width.copy(); + } + + + @Override + public NumVal height() + { + return p_height.copy(); + } + + + @Override + public NumVal left() + { + return p_left.copy(); + } + + + @Override + public NumVal right() + { + return p_right.copy(); + } + + + @Override + public NumVal top() + { + return p_top.copy(); + } + + + @Override + public NumVal bottom() + { + return p_bottom.copy(); + } + + + @Override + public VectVal topLeft() + { + return p_tl.copy(); + } + + + @Override + public VectVal topCenter() + { + return p_tc.copy(); + } + + + @Override + public VectVal topRight() + { + return p_tr.copy(); + } + + + @Override + public VectVal centerLeft() + { + return p_cl.copy(); + } + + + @Override + public VectVal center() + { + return p_cc.copy(); + } + + + @Override + public VectVal centerRight() + { + return p_cr.copy(); + } + + + @Override + public VectVal bottomLeft() + { + return p_bl.copy(); + } + + + @Override + public VectVal bottomCenter() + { + return p_bc.copy(); + } + + + @Override + public VectVal bottomRight() + { + return p_br.copy(); + } +} diff --git a/src/mightypork/utils/math/rect/RectMutable.java b/src/mightypork/utils/math/rect/RectMutable.java index aacdad6..809ff66 100644 --- a/src/mightypork/utils/math/rect/RectMutable.java +++ b/src/mightypork/utils/math/rect/RectMutable.java @@ -11,7 +11,7 @@ import mightypork.utils.math.vect.VectVal; * * @author MightyPork */ -public abstract class RectMutable extends RectMath { +public abstract class RectMutable extends RectMathStatic { /** * Create at 0,0 with zero size diff --git a/src/mightypork/utils/math/rect/RectMutableImpl.java b/src/mightypork/utils/math/rect/RectMutableImpl.java index dc332b1..aad007e 100644 --- a/src/mightypork/utils/math/rect/RectMutableImpl.java +++ b/src/mightypork/utils/math/rect/RectMutableImpl.java @@ -26,85 +26,29 @@ class RectMutableImpl extends RectMutable { } - /** - * Add X and Y to origin - * - * @param x x to add - * @param y y to add - * @return result - */ @Override - public RectMutable move(double x, double y) - { - pos.add(x, y); - return this; - } - - - /** - * Shrink the rect - * - * @param left shrink - * @param right shrink - * @param top shrink - * @param bottom shrink - * @return result - */ - @Override - public RectMutable shrink(double left, double right, double top, double bottom) + public VectVal origin() { - pos.add(left, top); - size.sub(left + right, top + bottom).abs(); - return this; + return pos.copy(); } - /** - * Grow the rect - * - * @param left growth - * @param right growth - * @param top growth - * @param bottom growth - * @return result - */ @Override - public RectMutable grow(double left, double right, double top, double bottom) + public VectVal size() { - pos.sub(left, top); - size.add(left + right, top + bottom).abs(); - return this; + return size.copy(); } - /** - * Round coords - * - * @return result - */ @Override - public RectMutable round() + protected RectMutable result(Vect newOrigin, Vect newSize) { - pos.round(); - size.round(); + setOrigin(newOrigin); + setSize(newSize); return this; } - @Override - public VectVal origin() - { - return pos.copy(); - } - - - @Override - public VectVal size() - { - return size.copy(); - } - - @Override public RectMutable setOrigin(Vect origin) { diff --git a/src/mightypork/utils/math/rect/RectProxy.java b/src/mightypork/utils/math/rect/RectProxy.java index 7221cdc..ae77b45 100644 --- a/src/mightypork/utils/math/rect/RectProxy.java +++ b/src/mightypork/utils/math/rect/RectProxy.java @@ -1,7 +1,6 @@ package mightypork.utils.math.rect; - -import mightypork.utils.math.vect.VectVal; +import mightypork.utils.math.vect.VectView; /** @@ -11,25 +10,24 @@ import mightypork.utils.math.vect.VectVal; */ public class RectProxy extends RectView { - private final Rect observed; + private final RectView observed; public RectProxy(Rect observed) { - this.observed = observed; + this.observed = observed.view(); } @Override - public VectVal origin() + public VectView origin() { - return observed.origin(); + return observed.p_origin; } @Override - public VectVal size() + public VectView size() { - return observed.size(); + return observed.p_size; } - } diff --git a/src/mightypork/utils/math/rect/RectVal.java b/src/mightypork/utils/math/rect/RectVal.java index ac54644..fbf1f0c 100644 --- a/src/mightypork/utils/math/rect/RectVal.java +++ b/src/mightypork/utils/math/rect/RectVal.java @@ -11,7 +11,13 @@ import mightypork.utils.math.vect.VectVal; * * @author MightyPork */ -public class RectVal extends RectView { +public class RectVal extends RectMathStatic { + + @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 @@ -110,16 +116,42 @@ public class RectVal extends RectView { * @param height */ public RectVal(double x, double y, double width, double height) { - pos = VectVal.make(x, y); - size = VectVal.make(width, height); + this.pos = VectVal.make(x, y); + this.size = VectVal.make(width, height); } + /** + * Create at given origin, with given size. + * + * @param origin + * @param size + */ + public RectVal(Vect origin, Vect size) { + this.pos = origin.copy(); + this.size = size.copy(); + } + + + /** + * Create at given origin, with given size. + * + * @param another other coord + */ + public 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() { - // must NOT call RectVal.make, it'd cause infinite recursion. - return this; // nothing can change. + return this; // already constant } @@ -136,4 +168,11 @@ public class RectVal extends RectView { return size; } + + @Override + protected RectVal result(Vect newOrigin, Vect newSize) + { + return make(newOrigin, newSize); + } + } diff --git a/src/mightypork/utils/math/rect/RectView.java b/src/mightypork/utils/math/rect/RectView.java index c4fbfa0..0fec77c 100644 --- a/src/mightypork/utils/math/rect/RectView.java +++ b/src/mightypork/utils/math/rect/RectView.java @@ -1,5 +1,6 @@ package mightypork.utils.math.rect; + import mightypork.utils.annotations.FactoryMethod; @@ -8,7 +9,13 @@ import mightypork.utils.annotations.FactoryMethod; * * @author MightyPork */ -public abstract class RectView extends RectMath { +public abstract class RectView extends RectMathDynamic { + + @SuppressWarnings("hiding") + public static final RectView ZERO = Rect.ZERO.view(); + @SuppressWarnings("hiding") + public static final RectView ONE = Rect.ONE.view(); + /** * Get a proxy at given rect @@ -23,34 +30,11 @@ public abstract class RectView extends RectMath { } + /** + * @deprecated No point in taking view of a view + */ @Override - public RectVal move(double x, double y) - { - return RectVal.make(origin().add(x, y), size()); - } - - - @Override - public RectVal shrink(double left, double right, double top, double bottom) - { - return RectVal.make(origin().add(left, top), size().sub(left + right, top + bottom)); - } - - - @Override - public RectVal grow(double left, double right, double top, double bottom) - { - return RectVal.make(origin().sub(left, top), size().add(left + right, top + bottom)); - } - - - @Override - public RectVal round() - { - return RectVal.make(origin().round(), size().round()); - } - - @Override + @Deprecated public RectView view() { // must NOT call RectView.make, it'd cause infinite recursion. diff --git a/src/mightypork/utils/math/vect/AbstractVect.java b/src/mightypork/utils/math/vect/AbstractVect.java index 8c16c5f..57f6640 100644 --- a/src/mightypork/utils/math/vect/AbstractVect.java +++ b/src/mightypork/utils/math/vect/AbstractVect.java @@ -1,27 +1,11 @@ package mightypork.utils.math.vect; -import mightypork.utils.math.constraints.NumBound; public abstract class AbstractVect implements Vect { private VectView proxy; - private NumBound xc; - private NumBound yc; - private NumBound zc; - - - @Override - public abstract double x(); - - - @Override - public abstract double y(); - - - @Override - public abstract double z(); @Override @@ -46,72 +30,9 @@ public abstract class AbstractVect implements Vect { @Override - public final NumBound xc() - { - if (xc == null) xc = new NumBound() { - - @Override - public double getValue() - { - return x(); - } - }; - - return xc; - } - - - @Override - public final NumBound yc() - { - if (yc == null) yc = new NumBound() { - - @Override - public double getValue() - { - return y(); - } - }; - - return yc; - } - - - @Override - public final NumBound zc() - { - if (zc == null) zc = new NumBound() { - - @Override - public double getValue() - { - return z(); - } - }; - - return zc; - } - - - @Override - public final Vect getVect() - { - return this; - } - - - @Override - public final double size() - { - final double x = x(), y = y(), z = z(); - return Math.sqrt(x * x + y * y + z * z); - } - - - @Override - public final boolean isZero() + public final VectView getVect() { - return x() == 0 && y() == 0 && z() == 0; + return view(); } diff --git a/src/mightypork/utils/math/vect/NumConstrVect.java b/src/mightypork/utils/math/vect/NumConstrVect.java index 967cbe9..223907a 100644 --- a/src/mightypork/utils/math/vect/NumConstrVect.java +++ b/src/mightypork/utils/math/vect/NumConstrVect.java @@ -2,6 +2,7 @@ package mightypork.utils.math.vect; import mightypork.utils.math.constraints.NumBound; +import mightypork.utils.math.num.Num; /** @@ -11,43 +12,43 @@ import mightypork.utils.math.constraints.NumBound; */ class NumConstrVect extends VectView { - private final NumBound constrX; - private final NumBound constrY; - private final NumBound constrZ; + private final Num constrX; + private final Num constrY; + private final Num constrZ; - public NumConstrVect(NumBound x, NumBound y, NumBound z) { + public NumConstrVect(Num x, Num y, Num z) { this.constrX = x; this.constrY = y; this.constrZ = z; } - public NumConstrVect(NumBound x, NumBound y) { + public NumConstrVect(Num x, Num y) { this.constrX = x; this.constrY = y; - this.constrZ = NumBound.ZERO; + this.constrZ = Num.ZERO; } @Override public double x() { - return constrX.getValue(); + return constrX.value(); } @Override public double y() { - return constrY.getValue(); + return constrY.value(); } @Override public double z() { - return constrZ.getValue(); + return constrZ.value(); } } diff --git a/src/mightypork/utils/math/vect/Vect.java b/src/mightypork/utils/math/vect/Vect.java index 353c1aa..381c00d 100644 --- a/src/mightypork/utils/math/vect/Vect.java +++ b/src/mightypork/utils/math/vect/Vect.java @@ -1,7 +1,6 @@ package mightypork.utils.math.vect; -import mightypork.utils.math.constraints.NumBound; import mightypork.utils.math.constraints.VectBound; @@ -12,8 +11,8 @@ import mightypork.utils.math.constraints.VectBound; */ public interface Vect extends VectBound { - VectVal ZERO = new VectVal(0, 0, 0); - VectVal ONE = new VectVal(0, 0, 0); + Vect ZERO = new VectVal(0, 0, 0); + Vect ONE = new VectVal(1, 1, 1); /** @@ -52,40 +51,14 @@ public interface Vect extends VectBound { int zi(); - /** - * @return X constraint - */ - NumBound xc(); - - - /** - * @return Y constraint - */ - NumBound yc(); - - - /** - * @return Z constraint - */ - NumBound zc(); - - - /** - * Get vector size - * - * @return size - */ - double size(); - - /** * @return true if zero */ - public boolean isZero(); + boolean isZero(); /** - * Get a view at current state, not propagating further changes. + * Get a static immutable copy of the current state. * * @return a immutable copy */ @@ -93,7 +66,7 @@ public interface Vect extends VectBound { /** - * Get immutable proxy view at this vec + * Get dynamic immutable view at this rect * * @return immutable view */ diff --git a/src/mightypork/utils/math/vect/VectAdapter.java b/src/mightypork/utils/math/vect/VectAdapter.java index a3709e0..2e5356b 100644 --- a/src/mightypork/utils/math/vect/VectAdapter.java +++ b/src/mightypork/utils/math/vect/VectAdapter.java @@ -1,6 +1,12 @@ package mightypork.utils.math.vect; +/** + * Vect proxy with abstract method for plugging in / generating coordinates + * dynamically. + * + * @author MightyPork + */ public abstract class VectAdapter extends VectView { /** @@ -12,57 +18,21 @@ public abstract class VectAdapter extends VectView { @Override public double x() { - return processX(getSource().x()); + return getSource().x(); } @Override public double y() { - return processY(getSource().y()); + return getSource().y(); } @Override public double z() { - return processZ(getSource().z()); - } - - - /** - * Process X before it's returned by x() - * - * @param x original X - * @return output X - */ - protected double processX(double x) - { - return x; - } - - - /** - * Process Y before it's returned by y() - * - * @param y original Y - * @return output Y - */ - protected double processY(double y) - { - return y; - } - - - /** - * Process Z before it's returned by z() - * - * @param z original Z - * @return output Z - */ - protected double processZ(double z) - { - return z; + return getSource().z(); } } diff --git a/src/mightypork/utils/math/vect/VectAnimated.java b/src/mightypork/utils/math/vect/VectAnimated.java index 480648c..c92a8cb 100644 --- a/src/mightypork/utils/math/vect/VectAnimated.java +++ b/src/mightypork/utils/math/vect/VectAnimated.java @@ -92,62 +92,82 @@ public class VectAnimated extends VectMutable implements Pauseable, Updateable { @Override public double x() { - return x.now(); + return x.value(); } @Override public double y() { - return y.now(); + return y.value(); } @Override public double z() { - return z.now(); + return z.value(); } - /** - * @return the default duration (seconds) - */ - public double getDefaultDuration() + @Override + public VectAnimated result(double x, double y, double z) { - return defaultDuration; + setX(x); + setY(y); + setZ(z); + + return this; } - /** - * Set default animation duration (when changed without using animate()) - * - * @param defaultDuration default duration (seconds) - */ - public void setDefaultDuration(double defaultDuration) + @Override + public VectMutable setX(double x) { - this.defaultDuration = defaultDuration; + this.x.animate(x, defaultDuration); + return this; } @Override - public VectAnimated result(double x, double y, double z) + public VectMutable setY(double y) { - this.x.animate(x, defaultDuration); this.y.animate(y, defaultDuration); + return this; + } + + + @Override + public VectMutable setZ(double z) + { this.z.animate(z, defaultDuration); - return this; } - public VectAnimated add(Vect offset, double speed) + /** + * Add offset with animation + * + * @param offset added offset + * @param duration animation time (seconds) + * @return this + */ + public VectAnimated add(Vect offset, double duration) { - animate(view().add(offset), speed); + animate(view().add(offset), duration); return this; } + /** + * Animate to given coordinates in given amount of time + * + * @param x + * @param y + * @param z + * @param duration animation time (seconds) + * @return this + */ public VectAnimated animate(double x, double y, double z, double duration) { this.x.animate(x, duration); @@ -157,6 +177,13 @@ public class VectAnimated extends VectMutable implements Pauseable, Updateable { } + /** + * Animate to given vec in given amount of time. + * + * @param target target (only it's current value will be used) + * @param duration animation time (seconds) + * @return this + */ public VectAnimated animate(Vect target, double duration) { animate(target.x(), target.y(), target.z(), duration); @@ -164,6 +191,26 @@ public class VectAnimated extends VectMutable implements Pauseable, Updateable { } + /** + * @return the default duration (seconds) + */ + public double getDefaultDuration() + { + return defaultDuration; + } + + + /** + * Set default animation duration (when changed without using animate()) + * + * @param defaultDuration default duration (seconds) + */ + public void setDefaultDuration(double defaultDuration) + { + this.defaultDuration = defaultDuration; + } + + @Override public void update(double delta) { diff --git a/src/mightypork/utils/math/vect/VectMath.java b/src/mightypork/utils/math/vect/VectMath.java index 8666a68..0992d68 100644 --- a/src/mightypork/utils/math/vect/VectMath.java +++ b/src/mightypork/utils/math/vect/VectMath.java @@ -1,29 +1,36 @@ package mightypork.utils.math.vect; +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumVal; + + /** - * Implementation of coordinate methods + * Vec operations * * @author MightyPork - * @param Return type of methods + * @param return type of vector functions + * @param return type of numeric functions + * @param return type of boolean functions */ -abstract class VectMath extends AbstractVect { +abstract class VectMath extends AbstractVect { /** - *

- * Some operation was performed and this result was obtained. - *

- *

- * It's now up to implementing class what to do - mutable ones can alter - * it's data values, immutable can return a new Vec. - *

- * - * @param x - * @param y - * @param z - * @return the result Vec + * @return X constraint + */ + public abstract Num xn(); + + + /** + * @return Y constraint */ - public abstract V result(double x, double y, double z); + public abstract Num yn(); + + + /** + * @return Z constraint + */ + public abstract Num zn(); /** @@ -32,9 +39,9 @@ abstract class VectMath extends AbstractVect { * @param x x coordinate * @return result */ - public V setX(double x) + public VectView withX(double x) { - return result(x, y(), z()); + return withX(NumVal.make(x)); } @@ -44,9 +51,9 @@ abstract class VectMath extends AbstractVect { * @param y y coordinate * @return result */ - public V setY(double y) + public VectView withY(double y) { - return result(x(), y, z()); + return withY(NumVal.make(y)); } @@ -56,9 +63,99 @@ abstract class VectMath extends AbstractVect { * @param z z coordinate * @return result */ - public V setZ(double z) + public VectView withZ(double z) + { + return withZ(NumVal.make(z)); + } + + + public VectView withX(final Num x) { - return result(x(), y(), z); + 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(); + } + }; } @@ -67,10 +164,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V abs() - { - return result(Math.abs(x()), Math.abs(y()), Math.abs(z())); - } + public abstract V abs(); /** @@ -79,10 +173,7 @@ abstract class VectMath extends AbstractVect { * @param vec offset * @return result */ - public V add(Vect vec) - { - return add(vec.x(), vec.y(), vec.z()); - } + public abstract V add(Vect vec); /** @@ -93,10 +184,7 @@ abstract class VectMath extends AbstractVect { * @param y y offset * @return result */ - public V add(double x, double y) - { - return add(x, y, 0); - } + public abstract V add(double x, double y); /** @@ -107,10 +195,13 @@ abstract class VectMath extends AbstractVect { * @param z z offset * @return result */ - public V add(double x, double y, double z) - { - return result(x() + x, y() + y, z() + z); - } + 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); /** @@ -118,10 +209,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V half() - { - return mul(0.5); - } + public abstract V half(); /** @@ -130,10 +218,7 @@ abstract class VectMath extends AbstractVect { * @param d multiplier * @return result */ - public V mul(double d) - { - return mul(d, d, d); - } + public abstract V mul(double d); /** @@ -142,10 +227,7 @@ abstract class VectMath extends AbstractVect { * @param vec vector of multipliers * @return result */ - public V mul(Vect vec) - { - return mul(vec.x(), vec.y(), vec.z()); - } + public abstract V mul(Vect vec); /** @@ -156,10 +238,7 @@ abstract class VectMath extends AbstractVect { * @param y y multiplier * @return result */ - public V mul(double x, double y) - { - return mul(x, y, 1); - } + public abstract V mul(double x, double y); /** @@ -170,10 +249,16 @@ abstract class VectMath extends AbstractVect { * @param z z multiplier * @return result */ - public V mul(double x, double y, double z) - { - return result(x() * x, y() * y, z() * z); - } + public abstract V mul(double x, double y, double z); + + + public abstract V mul(final Num d); + + + public abstract V mul(final Num x, final Num y); + + + public abstract V mul(final Num x, final Num y, final Num z); /** @@ -181,10 +266,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V round() - { - return result(Math.round(x()), Math.round(y()), Math.round(z())); - } + public abstract V round(); /** @@ -192,10 +274,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V floor() - { - return result(Math.floor(x()), Math.floor(y()), Math.floor(z())); - } + public abstract V floor(); /** @@ -203,10 +282,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V ceil() - { - return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z())); - } + public abstract V ceil(); /** @@ -215,10 +291,7 @@ abstract class VectMath extends AbstractVect { * @param vec offset * @return result */ - public V sub(Vect vec) - { - return sub(vec.x(), vec.y(), vec.z()); - } + public abstract V sub(Vect vec); /** @@ -229,10 +302,7 @@ abstract class VectMath extends AbstractVect { * @param y y offset * @return result */ - public V sub(double x, double y) - { - return sub(x, y, 0); - } + public abstract V sub(double x, double y); /** @@ -243,10 +313,13 @@ abstract class VectMath extends AbstractVect { * @param z z offset * @return result */ - public V sub(double x, double y, double z) - { - return result(x() - x, y() - y, z() - z); - } + 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); /** @@ -254,10 +327,7 @@ abstract class VectMath extends AbstractVect { * * @return result */ - public V neg() - { - return result(-x(), -y(), -z()); - } + public abstract V neg(); /** @@ -266,14 +336,7 @@ abstract class VectMath extends AbstractVect { * @param size size we need * @return result */ - public V norm(double size) - { - if (isZero()) return result(x(), y(), z()); // can't norm zero vector - - final double k = size / size(); - - return mul(k); - } + public abstract V norm(double size); /** @@ -282,14 +345,7 @@ abstract class VectMath extends AbstractVect { * @param point other point * @return distance */ - public final double distTo(Vect point) - { - final double dx = x() - point.x(); - final double dy = y() - point.y(); - final double dz = z() - point.z(); - - return Math.sqrt(dx * dx + dy * dy + dz * dz); - } + public abstract N distTo(Vect point); /** @@ -298,14 +354,7 @@ abstract class VectMath extends AbstractVect { * @param point other point * @return result */ - public final VectVal 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 VectVal.make(dx, dy, dz); - } + public abstract V midTo(Vect point); /** @@ -314,10 +363,7 @@ abstract class VectMath extends AbstractVect { * @param point second point * @return result */ - public final VectVal vectTo(Vect point) - { - return VectVal.make(point.x() - x(), point.y() - y(), point.z() - z()); - } + public abstract V vectTo(Vect point); /** @@ -326,15 +372,7 @@ abstract class VectMath extends AbstractVect { * @param vec other vector * @return result */ - public final VectVal cross(Vect vec) - { - //@formatter:off - return VectVal.make( - y() * vec.z() - z() * vec.y(), - z() * vec.x() - x() * vec.z(), - x() * vec.y() - y() * vec.x()); - //@formatter:on - } + public abstract V cross(Vect vec); /** @@ -343,8 +381,21 @@ abstract class VectMath extends AbstractVect { * @param vec other vector * @return dot product */ - public final double dot(Vect vec) + public abstract N dot(Vect vec); + + + /** + * Get vector size + * + * @return size + */ + public abstract N size(); + + + @Override + public boolean isZero() { - return x() * vec.x() + y() * vec.y() + z() * vec.z(); + return x() == 0 && y() == 0 && z() == 0; } + } diff --git a/src/mightypork/utils/math/vect/VectMathDynamic.java b/src/mightypork/utils/math/vect/VectMathDynamic.java new file mode 100644 index 0000000..0015d70 --- /dev/null +++ b/src/mightypork/utils/math/vect/VectMathDynamic.java @@ -0,0 +1,665 @@ +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 { + + 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; + } +} diff --git a/src/mightypork/utils/math/vect/VectMathStatic.java b/src/mightypork/utils/math/vect/VectMathStatic.java new file mode 100644 index 0000000..7039d5b --- /dev/null +++ b/src/mightypork/utils/math/vect/VectMathStatic.java @@ -0,0 +1,280 @@ +package mightypork.utils.math.vect; + + +import mightypork.utils.math.num.Num; +import mightypork.utils.math.num.NumVal; + + +/** + * Implementation of coordinate methods + * + * @author MightyPork + * @param Return type of methods + */ +public abstract class VectMathStatic> extends VectMath { + + @Override + public NumVal xn() + { + return NumVal.make(x()); + } + + + @Override + public NumVal yn() + { + return NumVal.make(yn()); + } + + + @Override + public NumVal zn() + { + return NumVal.make(z()); + } + + + /** + *

+ * Some operation was performed and this result was obtained. + *

+ *

+ * It's now up to implementing class what to do - mutable ones can alter + * it's data values, immutable can return a new Vec. + *

+ * + * @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)); + } +} diff --git a/src/mightypork/utils/math/vect/VectMutable.java b/src/mightypork/utils/math/vect/VectMutable.java index 5421894..960e28a 100644 --- a/src/mightypork/utils/math/vect/VectMutable.java +++ b/src/mightypork/utils/math/vect/VectMutable.java @@ -1,8 +1,7 @@ package mightypork.utils.math.vect; -import mightypork.utils.annotations.FactoryMethod; - +import mightypork.utils.annotations.FactoryMethod; /** @@ -10,7 +9,7 @@ import mightypork.utils.annotations.FactoryMethod; * * @author MightyPork */ -public abstract class VectMutable extends VectMath { // returns itself on edit +public abstract class VectMutable extends VectMathStatic { // returns itself on edit /** * Get a variable initialized as zero (0,0,0) @@ -74,10 +73,15 @@ public abstract class VectMutable extends VectMath { // returns its @FactoryMethod public static VectMutable make(double x, double y, double z) { - return new VectMutableVariable(x, y, z); + return new VectMutableImpl(x, y, z); } + /** + * Set all to zeros. + * + * @return this + */ public VectMutable reset() { return result(0, 0, 0); @@ -88,7 +92,7 @@ public abstract class VectMutable extends VectMath { // returns its * Set coordinates to match other coord. * * @param copied coord whose coordinates are used - * @return result + * @return this */ public VectMutable setTo(Vect copied) { @@ -102,7 +106,7 @@ public abstract class VectMutable extends VectMath { // returns its * * @param x x coordinate * @param y y coordinate - * @return result + * @return this */ public VectMutable setTo(double x, double y) { @@ -116,10 +120,37 @@ public abstract class VectMutable extends VectMath { // returns its * @param x x coordinate * @param y y coordinate * @param z z coordinate - * @return result + * @return this */ public VectMutable setTo(double x, double y, double z) { return result(x, y, z); } + + + /** + * Set X coordinate. + * + * @param x x coordinate + * @return this + */ + public abstract VectMutable setX(double x); + + + /** + * Set Y coordinate. + * + * @param y y coordinate + * @return this + */ + public abstract VectMutable setY(double y); + + + /** + * Set Z coordinate. + * + * @param z z coordinate + * @return this + */ + public abstract VectMutable setZ(double z); } diff --git a/src/mightypork/utils/math/vect/VectMutableVariable.java b/src/mightypork/utils/math/vect/VectMutableImpl.java similarity index 64% rename from src/mightypork/utils/math/vect/VectMutableVariable.java rename to src/mightypork/utils/math/vect/VectMutableImpl.java index 2b8927e..c7eb845 100644 --- a/src/mightypork/utils/math/vect/VectMutableVariable.java +++ b/src/mightypork/utils/math/vect/VectMutableImpl.java @@ -7,7 +7,7 @@ package mightypork.utils.math.vect; * * @author MightyPork */ -class VectMutableVariable extends VectMutable { +class VectMutableImpl extends VectMutable { private double x, y, z; @@ -17,7 +17,7 @@ class VectMutableVariable extends VectMutable { * @param y Y coordinate * @param z Z coordinate */ - public VectMutableVariable(double x, double y, double z) { + public VectMutableImpl(double x, double y, double z) { super(); this.x = x; this.y = y; @@ -55,4 +55,28 @@ class VectMutableVariable extends VectMutable { return this; } + + + @Override + public VectMutable setX(double x) + { + this.x = x; + return this; + } + + + @Override + public VectMutable setY(double y) + { + this.y = y; + return this; + } + + + @Override + public VectMutable setZ(double z) + { + this.z = z; + return this; + } } diff --git a/src/mightypork/utils/math/vect/VectVal.java b/src/mightypork/utils/math/vect/VectVal.java index 359d140..71ada34 100644 --- a/src/mightypork/utils/math/vect/VectVal.java +++ b/src/mightypork/utils/math/vect/VectVal.java @@ -1,5 +1,6 @@ package mightypork.utils.math.vect; + import mightypork.utils.annotations.FactoryMethod; @@ -9,7 +10,13 @@ import mightypork.utils.annotations.FactoryMethod; * * @author MightyPork */ -public final class VectVal extends VectView { +public final class VectVal extends VectMathStatic { + + @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 @@ -88,10 +95,21 @@ public final class VectVal extends VectView { } + /** + * @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); + } + } diff --git a/src/mightypork/utils/math/vect/VectView.java b/src/mightypork/utils/math/vect/VectView.java index 93ccd41..86d0ec0 100644 --- a/src/mightypork/utils/math/vect/VectView.java +++ b/src/mightypork/utils/math/vect/VectView.java @@ -3,17 +3,24 @@ package mightypork.utils.math.vect; import mightypork.utils.annotations.DefaultImpl; import mightypork.utils.annotations.FactoryMethod; -import mightypork.utils.math.constraints.NumBound; +import mightypork.utils.math.num.Num; /** - * Read-only coordinate, whose values cannot be changed directly. To keep - * current state, use the value() method. + * Read-only immutable dynamic view of a coordinate. When the values change, + * it'll be propagated in all derived coords.
+ * To take a static copy, use the copy() method. * * @author MightyPork */ -public abstract class VectView extends VectMath { // returns constant value on edit +public abstract class VectView extends VectMathDynamic { // returns constant value on edit + @SuppressWarnings("hiding") + public static final VectView ZERO = new VectVal(0, 0, 0).view(); + @SuppressWarnings("hiding") + public static final VectView ONE = new VectVal(1, 1, 1).view(); + + /** * Make a proxy view at a vector. * @@ -35,7 +42,7 @@ public abstract class VectView extends VectMath { // returns constant v * @return view at the values */ @FactoryMethod - public static VectView make(NumBound xc, NumBound yc) + public static VectView make(Num xc, Num yc) { return new NumConstrVect(xc, yc); } @@ -50,32 +57,28 @@ public abstract class VectView extends VectMath { // returns constant v * @return view at the values */ @FactoryMethod - public static VectView make(NumBound xc, NumBound yc, NumBound zc) + public static VectView make(Num xc, Num yc, Num zc) { return new NumConstrVect(xc, yc, zc); } @Override - public VectVal result(double x, double y, double z) + @DefaultImpl + public double z() { - return VectVal.make(x, y, z); + return 0; // implemented for ease with 2D anonymous subtypes } + /** + * @deprectaed No point in taking view of a view. + */ @Override + @Deprecated public VectView view() { - // must NOT call VectView.copy, it'd cause infinite recursion. - return this; // already a view - } - - - @Override - @DefaultImpl - public double z() - { - return 0; // implemented for ease with 2D anonymous subtypes + return super.view(); } } diff --git a/src/mightypork/utils/objects/Convert.java b/src/mightypork/utils/objects/Convert.java index ceda391..a6d172a 100644 --- a/src/mightypork/utils/objects/Convert.java +++ b/src/mightypork/utils/objects/Convert.java @@ -315,7 +315,7 @@ public class Convert { * @param o object * @return Coord */ - public static VectView toVect(Object o) + public static VectVal toVect(Object o) { return toVect(o, Vect.ZERO); }