Not all fixed yet, but OMG that bounds system! Wow!
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<MouseMotionEvent.Listener> {
|
||||
|
||||
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<MouseMotionEvent.Listener> {
|
||||
/**
|
||||
* @return movement since last {@link MouseMotionEvent}
|
||||
*/
|
||||
public VectView getMove()
|
||||
public VectVal getMove()
|
||||
{
|
||||
return move;
|
||||
}
|
||||
@@ -40,7 +41,7 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
|
||||
/**
|
||||
* @return current mouse position
|
||||
*/
|
||||
public VectView getPos()
|
||||
public VectVal getPos()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
+16
-32
@@ -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.<br>
|
||||
* Bag for {@link AbstractPainter} elements with constraints.<br>
|
||||
* 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<PluggableRenderable> elements = new LinkedList<>();
|
||||
private RectBound context;
|
||||
final LinkedList<PluggableRenderable> 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.<br>
|
||||
* Element must then be attached using the <code>attach</code> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+5
-3
@@ -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();
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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());
|
||||
System.out.println(rect.view().centerTo(point));
|
||||
|
||||
// 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()));
|
||||
|
||||
// VecMutable vv = new MutableCoord(0,0);
|
||||
//
|
||||
// System.out.println(vv);
|
||||
// System.out.println(vv.add(50,50));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-8
@@ -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();
|
||||
|
||||
}
|
||||
+8
-7
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package mightypork.utils.math.num;
|
||||
|
||||
|
||||
/**
|
||||
* Math operations for numbers
|
||||
*
|
||||
* @author MightyPork
|
||||
* @param <N> functions return type
|
||||
*/
|
||||
interface NumMath<N extends NumMath<N>> extends Num {
|
||||
|
||||
double CMP_EPSILON = 0.0000001;
|
||||
|
||||
|
||||
N add(Num addend);
|
||||
|
||||
|
||||
N sub(Num subtrahend);
|
||||
|
||||
|
||||
N mul(Num factor);
|
||||
|
||||
|
||||
N div(Num factor);
|
||||
|
||||
|
||||
N perc(Num percent);
|
||||
|
||||
|
||||
N max(Num other);
|
||||
|
||||
|
||||
N min(Num other);
|
||||
|
||||
|
||||
N pow(Num other);
|
||||
|
||||
|
||||
N average(Num other);
|
||||
|
||||
|
||||
N add(double addend);
|
||||
|
||||
|
||||
N sub(double subtrahend);
|
||||
|
||||
|
||||
N mul(double factor);
|
||||
|
||||
|
||||
N div(double factor);
|
||||
|
||||
|
||||
N perc(double percent);
|
||||
|
||||
|
||||
N neg();
|
||||
|
||||
|
||||
N abs();
|
||||
|
||||
|
||||
N max(double other);
|
||||
|
||||
|
||||
N min(double other);
|
||||
|
||||
|
||||
N pow(double other);
|
||||
|
||||
|
||||
N square();
|
||||
|
||||
|
||||
N cube();
|
||||
|
||||
|
||||
N sqrt();
|
||||
|
||||
|
||||
N cbrt();
|
||||
|
||||
|
||||
N sin();
|
||||
|
||||
|
||||
N cos();
|
||||
|
||||
|
||||
N tan();
|
||||
|
||||
|
||||
N asin();
|
||||
|
||||
|
||||
N acos();
|
||||
|
||||
|
||||
N atan();
|
||||
|
||||
|
||||
N round();
|
||||
|
||||
|
||||
N floor();
|
||||
|
||||
|
||||
N ceil();
|
||||
|
||||
|
||||
N signum();
|
||||
|
||||
|
||||
N half();
|
||||
|
||||
|
||||
N average(double other);
|
||||
|
||||
|
||||
boolean lt(Num other);
|
||||
|
||||
|
||||
boolean lte(Num other);
|
||||
|
||||
|
||||
boolean gt(Num other);
|
||||
|
||||
|
||||
boolean gte(Num other);
|
||||
|
||||
|
||||
boolean eq(Num other);
|
||||
|
||||
|
||||
boolean lt(double other);
|
||||
|
||||
|
||||
boolean lte(double other);
|
||||
|
||||
|
||||
boolean gt(double other);
|
||||
|
||||
|
||||
boolean gte(double other);
|
||||
|
||||
|
||||
boolean eq(double other);
|
||||
|
||||
|
||||
boolean isNegative();
|
||||
|
||||
|
||||
boolean isPositive();
|
||||
|
||||
|
||||
boolean isZero();
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package mightypork.utils.math.num;
|
||||
|
||||
import mightypork.utils.math.constraints.NumBound;
|
||||
|
||||
|
||||
public abstract class NumMathBase<N extends NumMath<N>> extends AbstractNum implements NumMath<N> {
|
||||
|
||||
/**
|
||||
* Convert to double, turning null into zero.
|
||||
*
|
||||
* @param a num
|
||||
* @return double
|
||||
*/
|
||||
protected static double eval(final NumBound a)
|
||||
{
|
||||
return toNum(a).value();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert {@link NumBound} to {@link Num}, turning null to Num.ZERO.
|
||||
* @param a numeric bound
|
||||
* @return num
|
||||
*/
|
||||
protected static Num toNum(final NumBound a)
|
||||
{
|
||||
return (a == null) ? Num.ZERO : (a.getNum() == null ? Num.ZERO : a.getNum());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Num getNum()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean lt(double other)
|
||||
{
|
||||
return !gte(other);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean lt(final Num other)
|
||||
{
|
||||
return !gte(other);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean lte(double other)
|
||||
{
|
||||
return !gt(other);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean lte(final Num other)
|
||||
{
|
||||
return !gt(other);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean gt(double other)
|
||||
{
|
||||
return Math.signum(value() - other) >= 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean gt(final Num other)
|
||||
{
|
||||
return gt(eval(other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean gte(double other)
|
||||
{
|
||||
return Math.signum(value() - other) >= 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean gte(final Num other)
|
||||
{
|
||||
return gte(eval(other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean eq(double other)
|
||||
{
|
||||
return Math.abs(value() - other) <= CMP_EPSILON;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean eq(final Num a)
|
||||
{
|
||||
return eq(eval(a));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isNegative()
|
||||
{
|
||||
return Math.signum(value()) < 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isPositive()
|
||||
{
|
||||
return Math.signum(value()) > 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isZero()
|
||||
{
|
||||
return Math.abs(value()) <= CMP_EPSILON;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(value());
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (!(obj instanceof NumMathBase)) return false;
|
||||
NumMathBase<?> other = (NumMathBase<?>) obj;
|
||||
|
||||
return eq(other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,558 @@
|
||||
package mightypork.utils.math.num;
|
||||
|
||||
|
||||
public abstract class NumMathDynamic extends NumMathBase<NumView>{
|
||||
|
||||
private NumView ceil;
|
||||
private NumView floor;
|
||||
private NumView sgn;
|
||||
private NumView round;
|
||||
private NumView atan;
|
||||
private NumView acos;
|
||||
private NumView asin;
|
||||
private NumView tan;
|
||||
private NumView cos;
|
||||
private NumView sin;
|
||||
private NumView cbrt;
|
||||
private NumView sqrt;
|
||||
private NumView cube;
|
||||
private NumView square;
|
||||
private NumView neg;
|
||||
private NumView abs;
|
||||
|
||||
@Override
|
||||
public NumView add(final double addend)
|
||||
{
|
||||
return new NumView() {
|
||||
|
||||
private 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package mightypork.utils.math.num;
|
||||
|
||||
|
||||
public abstract class NumMathStatic<N extends NumMathStatic<N>> extends NumMathBase<N> {
|
||||
|
||||
protected abstract N result(double a);
|
||||
|
||||
|
||||
@Override
|
||||
public N add(double addend)
|
||||
{
|
||||
return result(value() + addend);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N sub(double subtrahend)
|
||||
{
|
||||
return add(-subtrahend);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N mul(double factor)
|
||||
{
|
||||
return result(value() * factor);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N div(double factor)
|
||||
{
|
||||
return mul(1 / factor);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N perc(double percents)
|
||||
{
|
||||
return mul(percents / 100);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N neg()
|
||||
{
|
||||
return mul(-1);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N abs()
|
||||
{
|
||||
return result(Math.abs(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N max(double other)
|
||||
{
|
||||
return result(Math.max(value(), other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N min(double other)
|
||||
{
|
||||
return result(Math.min(value(), other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N pow(double power)
|
||||
{
|
||||
return result(Math.pow(value(), power));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N square()
|
||||
{
|
||||
final double v = value();
|
||||
return result(v * v);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N cube()
|
||||
{
|
||||
final double v = value();
|
||||
return result(v * v * v);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N sqrt()
|
||||
{
|
||||
return result(Math.sqrt(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N cbrt()
|
||||
{
|
||||
return result(Math.cbrt(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N sin()
|
||||
{
|
||||
return result(Math.sin(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N cos()
|
||||
{
|
||||
return result(Math.cos(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N tan()
|
||||
{
|
||||
return result(Math.tan(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N asin()
|
||||
{
|
||||
return result(Math.asin(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N acos()
|
||||
{
|
||||
return result(Math.acos(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N atan()
|
||||
{
|
||||
return result(Math.atan(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N signum()
|
||||
{
|
||||
return result(Math.signum(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N average(double other)
|
||||
{
|
||||
return result((value() + other) / 2);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N round()
|
||||
{
|
||||
return result(Math.round(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N ceil()
|
||||
{
|
||||
return result(Math.ceil(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N floor()
|
||||
{
|
||||
return result(Math.floor(value()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N half()
|
||||
{
|
||||
return mul(0.5);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N add(final Num addend)
|
||||
{
|
||||
return add(eval(addend));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N sub(final Num subtrahend)
|
||||
{
|
||||
return sub(eval(subtrahend));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N mul(final Num factor)
|
||||
{
|
||||
return mul(eval(factor));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N div(final Num factor)
|
||||
{
|
||||
return div(eval(factor));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N perc(final Num percent)
|
||||
{
|
||||
return perc(eval(percent));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N max(final Num other)
|
||||
{
|
||||
return min(eval(other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N min(final Num other)
|
||||
{
|
||||
return min(eval(other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N pow(final Num power)
|
||||
{
|
||||
return pow(eval(power));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public N average(final Num other)
|
||||
{
|
||||
return average(eval(other));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.format("{%.1f}", value());
|
||||
}
|
||||
}
|
||||
@@ -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<NumMutable> {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<NumVal> {
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public static final NumVal ZERO = NumVal.make(0);
|
||||
@SuppressWarnings("hiding")
|
||||
public static final NumVal ONE = NumVal.make(1);
|
||||
|
||||
/**
|
||||
* Make a new constant
|
||||
*
|
||||
* @param value constant value
|
||||
* @return new constant with the value
|
||||
*/
|
||||
@FactoryMethod
|
||||
public static NumVal make(double value)
|
||||
{
|
||||
return new NumVal(value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make a new constant
|
||||
*
|
||||
* @param copied number whose value to use
|
||||
* @return new constant with the value
|
||||
*/
|
||||
@FactoryMethod
|
||||
public static NumVal make(Num copied)
|
||||
{
|
||||
return (copied == null ? ZERO : copied.copy());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make a new constant
|
||||
*
|
||||
* @param copied number whose value to use
|
||||
* @return new constant with the value
|
||||
*/
|
||||
@FactoryMethod
|
||||
public static NumVal make(NumBound copied)
|
||||
{
|
||||
return new NumVal(eval(copied));
|
||||
}
|
||||
|
||||
private final double value;
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<T extends Rect> extends AbstractRect {
|
||||
abstract class RectMath<R extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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<T extends Rect> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<RectView> {
|
||||
|
||||
@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());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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<R extends RectMathStatic<R>> extends RectMath<R> {
|
||||
|
||||
@Override
|
||||
public abstract VectVal origin();
|
||||
|
||||
|
||||
@Override
|
||||
public abstract VectVal size();
|
||||
|
||||
@Override
|
||||
public R move(Vect move)
|
||||
{
|
||||
return move(move.x(), move.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public R move(double x, double y) {
|
||||
return result(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();
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import mightypork.utils.math.vect.VectVal;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class RectMutable extends RectMath<RectMutable> {
|
||||
public abstract class RectMutable extends RectMathStatic<RectMutable> {
|
||||
|
||||
/**
|
||||
* Create at 0,0 with zero size
|
||||
|
||||
@@ -26,71 +26,6 @@ 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)
|
||||
{
|
||||
pos.add(left, top);
|
||||
size.sub(left + right, top + bottom).abs();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
pos.sub(left, top);
|
||||
size.add(left + right, top + bottom).abs();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Round coords
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
@Override
|
||||
public RectMutable round()
|
||||
{
|
||||
pos.round();
|
||||
size.round();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectVal origin()
|
||||
{
|
||||
@@ -105,6 +40,15 @@ class RectMutableImpl extends RectMutable {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected RectMutable result(Vect newOrigin, Vect newSize)
|
||||
{
|
||||
setOrigin(newOrigin);
|
||||
setSize(newSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RectMutable setOrigin(Vect origin)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,13 @@ import mightypork.utils.math.vect.VectVal;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class RectVal extends RectView {
|
||||
public class RectVal extends RectMathStatic<RectVal> {
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public static final RectVal ZERO = Rect.ZERO.copy();
|
||||
@SuppressWarnings("hiding")
|
||||
public static final RectVal ONE = Rect.ONE.copy();
|
||||
|
||||
|
||||
/**
|
||||
* Get a proxy at given rect
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<RectVal> {
|
||||
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<RectVal> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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.
|
||||
|
||||
@@ -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()
|
||||
public final VectView getVect()
|
||||
{
|
||||
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()
|
||||
{
|
||||
return x() == 0 && y() == 0 && z() == 0;
|
||||
return view();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,21 +92,102 @@ 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();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectAnimated result(double x, double y, double z)
|
||||
{
|
||||
setX(x);
|
||||
setY(y);
|
||||
setZ(z);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectMutable setX(double x)
|
||||
{
|
||||
this.x.animate(x, defaultDuration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectMutable setY(double y)
|
||||
{
|
||||
this.y.animate(y, defaultDuration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectMutable setZ(double z)
|
||||
{
|
||||
this.z.animate(z, defaultDuration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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), 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);
|
||||
this.y.animate(y, duration);
|
||||
this.z.animate(z, duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -130,40 +211,6 @@ public class VectAnimated extends VectMutable implements Pauseable, Updateable {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectAnimated result(double x, double y, double z)
|
||||
{
|
||||
this.x.animate(x, defaultDuration);
|
||||
this.y.animate(y, defaultDuration);
|
||||
this.z.animate(z, defaultDuration);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public VectAnimated add(Vect offset, double speed)
|
||||
{
|
||||
animate(view().add(offset), speed);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public VectAnimated animate(double x, double y, double z, double duration)
|
||||
{
|
||||
this.x.animate(x, duration);
|
||||
this.y.animate(y, duration);
|
||||
this.z.animate(z, duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public VectAnimated animate(Vect target, double duration)
|
||||
{
|
||||
animate(target.x(), target.y(), target.z(), duration);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(double delta)
|
||||
{
|
||||
|
||||
@@ -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 <V> Return type of methods
|
||||
* @param <V> return type of vector functions
|
||||
* @param <N> return type of numeric functions
|
||||
* @param <B> return type of boolean functions
|
||||
*/
|
||||
abstract class VectMath<V extends Vect> extends AbstractVect {
|
||||
abstract class VectMath<V extends Vect, N> extends AbstractVect {
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Some operation was performed and this result was obtained.
|
||||
* </p>
|
||||
* <p>
|
||||
* It's now up to implementing class what to do - mutable ones can alter
|
||||
* it's data values, immutable can return a new Vec.
|
||||
* </p>
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return the result Vec
|
||||
* @return X constraint
|
||||
*/
|
||||
public abstract V result(double x, double y, double z);
|
||||
public abstract Num xn();
|
||||
|
||||
|
||||
/**
|
||||
* @return Y constraint
|
||||
*/
|
||||
public abstract Num yn();
|
||||
|
||||
|
||||
/**
|
||||
* @return Z constraint
|
||||
*/
|
||||
public abstract Num zn();
|
||||
|
||||
|
||||
/**
|
||||
@@ -32,9 +39,9 @@ abstract class VectMath<V extends Vect> 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<V extends Vect> 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<V extends Vect> extends AbstractVect {
|
||||
* @param z z coordinate
|
||||
* @return result
|
||||
*/
|
||||
public V setZ(double z)
|
||||
public VectView withZ(double z)
|
||||
{
|
||||
return result(x(), y(), z);
|
||||
return withZ(NumVal.make(z));
|
||||
}
|
||||
|
||||
|
||||
public VectView withX(final Num x)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMath.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return x.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.z();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public VectView withY(final Num y)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMath.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return y.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public VectView withZ(final Num z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMath.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return z.value();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -67,10 +164,7 @@ abstract class VectMath<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> extends AbstractVect {
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
public V half()
|
||||
{
|
||||
return mul(0.5);
|
||||
}
|
||||
public abstract V half();
|
||||
|
||||
|
||||
/**
|
||||
@@ -130,10 +218,7 @@ abstract class VectMath<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> extends AbstractVect {
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
public V neg()
|
||||
{
|
||||
return result(-x(), -y(), -z());
|
||||
}
|
||||
public abstract V neg();
|
||||
|
||||
|
||||
/**
|
||||
@@ -266,14 +336,7 @@ abstract class VectMath<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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<V extends Vect> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<VectView, NumBound> {
|
||||
|
||||
private NumView size;
|
||||
private VectView neg;
|
||||
private VectView half;
|
||||
private VectView abs;
|
||||
private NumView xc;
|
||||
private NumView yc;
|
||||
private NumView zc;
|
||||
|
||||
|
||||
/**
|
||||
* @return X constraint
|
||||
*/
|
||||
@Override
|
||||
public final NumView xn()
|
||||
{
|
||||
if (xc == null) xc = new NumView() {
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
return x();
|
||||
}
|
||||
};
|
||||
|
||||
return xc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Y constraint
|
||||
*/
|
||||
@Override
|
||||
public final NumView yn()
|
||||
{
|
||||
if (yc == null) yc = new NumView() {
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
return y();
|
||||
}
|
||||
};
|
||||
|
||||
return yc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Z constraint
|
||||
*/
|
||||
@Override
|
||||
public final NumView zn()
|
||||
{
|
||||
if (zc == null) zc = new NumView() {
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
return z();
|
||||
}
|
||||
};
|
||||
|
||||
return zc;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView abs()
|
||||
{
|
||||
if (abs == null) abs = new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return Math.abs(t.x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return Math.abs(t.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return Math.abs(t.z());
|
||||
}
|
||||
};
|
||||
|
||||
return abs;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView add(Vect vec)
|
||||
{
|
||||
return add(vec.view().xn(), vec.view().yn(), vec.view().zn());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView add(double x, double y)
|
||||
{
|
||||
return add(x, y, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView add(final double x, final double y, final double z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x() + x;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y() + y;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z() + z;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView add(Num x, Num y)
|
||||
{
|
||||
return add(x, y, Num.ZERO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView add(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x() + x.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y() + y.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z() + z.value();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView half()
|
||||
{
|
||||
if (half == null) half = mul(0.5);
|
||||
return half;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(double d)
|
||||
{
|
||||
return mul(d, d, d);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(Vect vec)
|
||||
{
|
||||
return mul(vec.view().xn(), vec.view().yn(), vec.view().zn());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(double x, double y)
|
||||
{
|
||||
return mul(x, y, 1);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(final double x, final double y, final double z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x() * x;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y() * y;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z() * z;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(final Num d)
|
||||
{
|
||||
return mul(d, d, d);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(final Num x, final Num y)
|
||||
{
|
||||
return mul(x, y, Num.ONE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView mul(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x() * x.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y() * y.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z() * z.value();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView round()
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return Math.round(t.x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return Math.round(t.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return Math.round(t.z());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView floor()
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return Math.floor(t.x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return Math.floor(t.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return Math.floor(t.z());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView ceil()
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return Math.ceil(t.x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return Math.ceil(t.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return Math.ceil(t.z());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView sub(Vect vec)
|
||||
{
|
||||
return sub(vec.view().xn(), vec.view().yn(), vec.view().zn());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView sub(double x, double y)
|
||||
{
|
||||
return add(-x, -y, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView sub(double x, double y, double z)
|
||||
{
|
||||
return add(-x, -y, -z);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView sub(Num x, Num y)
|
||||
{
|
||||
return sub(x, y, Num.ZERO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView sub(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final Vect t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.x() - x.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.y() - y.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.z() - z.value();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView neg()
|
||||
{
|
||||
if (neg == null) neg = mul(-1);
|
||||
return neg;
|
||||
}
|
||||
|
||||
|
||||
public VectView norm(final Num size)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
final double tSize = t.size().value();
|
||||
final double nSize = size.value();
|
||||
|
||||
if (tSize == 0 || nSize == 0) return 0;
|
||||
|
||||
return x() / (nSize / tSize);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
final double tSize = t.size().value();
|
||||
final double nSize = size.value();
|
||||
|
||||
if (tSize == 0 || nSize == 0) return 0;
|
||||
|
||||
return y() / (nSize / tSize);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
final double tSize = t.size().value();
|
||||
final double nSize = size.value();
|
||||
|
||||
if (tSize == 0 || nSize == 0) return 0;
|
||||
|
||||
return z() / (nSize / tSize);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView norm(final double size)
|
||||
{
|
||||
return norm(NumVal.make(size));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumView distTo(final Vect point)
|
||||
{
|
||||
return new NumView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
final double dx = t.x() - point.x();
|
||||
final double dy = t.y() - point.y();
|
||||
final double dz = t.z() - point.z();
|
||||
|
||||
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final VectView midTo(final Vect point)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return (point.x() + t.x()) * 0.5;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return (point.y() + t.y()) * 0.5;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return (point.z() + t.z()) * 0.5;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final VectView vectTo(final Vect point)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return (point.x() - t.x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return (point.y() - t.y());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return (point.z() - t.z());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final VectView cross(final Vect vec)
|
||||
{
|
||||
return new VectView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double x()
|
||||
{
|
||||
return t.y() * vec.z() - t.z() * vec.y();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double y()
|
||||
{
|
||||
return t.z() * vec.x() - t.x() * vec.z();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double z()
|
||||
{
|
||||
return t.x() * vec.y() - t.y() * vec.x();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumView dot(final Vect vec)
|
||||
{
|
||||
return new NumView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
return t.x() * vec.x() + t.y() * vec.y() + t.z() * vec.z();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Num size()
|
||||
{
|
||||
if (size == null) size = new NumView() {
|
||||
|
||||
final VectMathDynamic t = VectMathDynamic.this;
|
||||
|
||||
|
||||
@Override
|
||||
public double value()
|
||||
{
|
||||
final double x = t.x(), y = t.y(), z = t.z();
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
};
|
||||
|
||||
return size;
|
||||
}
|
||||
}
|
||||
@@ -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 <V> Return type of methods
|
||||
*/
|
||||
public abstract class VectMathStatic<V extends VectMathStatic<V>> extends VectMath<V, NumVal> {
|
||||
|
||||
@Override
|
||||
public NumVal xn()
|
||||
{
|
||||
return NumVal.make(x());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumVal yn()
|
||||
{
|
||||
return NumVal.make(yn());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumVal zn()
|
||||
{
|
||||
return NumVal.make(z());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Some operation was performed and this result was obtained.
|
||||
* </p>
|
||||
* <p>
|
||||
* It's now up to implementing class what to do - mutable ones can alter
|
||||
* it's data values, immutable can return a new Vec.
|
||||
* </p>
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @return the result Vec
|
||||
*/
|
||||
public abstract V result(double x, double y, double z);
|
||||
|
||||
|
||||
@Override
|
||||
public V abs()
|
||||
{
|
||||
return result(Math.abs(x()), Math.abs(y()), Math.abs(z()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V add(Vect vec)
|
||||
{
|
||||
return add(vec.x(), vec.y(), vec.z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V add(double x, double y)
|
||||
{
|
||||
return add(x, y, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V add(double x, double y, double z)
|
||||
{
|
||||
return result(x() + x, y() + y, z() + z);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V add(Num x, Num y)
|
||||
{
|
||||
return add(x, y, Num.ZERO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V add(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return add(x.value(), y.value(), z.value());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V half()
|
||||
{
|
||||
return mul(0.5);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(double d)
|
||||
{
|
||||
return mul(d, d, d);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(Vect vec)
|
||||
{
|
||||
return mul(vec.x(), vec.y(), vec.z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(double x, double y)
|
||||
{
|
||||
return mul(x, y, 1);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(double x, double y, double z)
|
||||
{
|
||||
return result(x() * x, y() * y, z() * z);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(final Num d)
|
||||
{
|
||||
return mul(d, d, d);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(final Num x, final Num y)
|
||||
{
|
||||
return mul(x, y, Num.ONE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V mul(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return mul(x.value(), y.value(), z.value());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V round()
|
||||
{
|
||||
return result(Math.round(x()), Math.round(y()), Math.round(z()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V floor()
|
||||
{
|
||||
return result(Math.floor(x()), Math.floor(y()), Math.floor(z()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V ceil()
|
||||
{
|
||||
return result(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V sub(Vect vec)
|
||||
{
|
||||
return sub(vec.x(), vec.y(), vec.z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V sub(double x, double y)
|
||||
{
|
||||
return sub(x, y, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V sub(double x, double y, double z)
|
||||
{
|
||||
return result(x() - x, y() - y, z() - z);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V sub(Num x, Num y)
|
||||
{
|
||||
return sub(x, y, Num.ZERO);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V sub(final Num x, final Num y, final Num z)
|
||||
{
|
||||
return sub(x.value(), y.value(), z.value());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V neg()
|
||||
{
|
||||
return result(-x(), -y(), -z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V norm(double size)
|
||||
{
|
||||
if (isZero()) return result(x(), y(), z()); // can't norm zero vector
|
||||
|
||||
final NumVal k = size().mul(1 / size);
|
||||
|
||||
return mul(k);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumVal distTo(Vect point)
|
||||
{
|
||||
final double dx = x() - point.x();
|
||||
final double dy = y() - point.y();
|
||||
final double dz = z() - point.z();
|
||||
|
||||
return NumVal.make(Math.sqrt(dx * dx + dy * dy + dz * dz));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final V midTo(Vect point)
|
||||
{
|
||||
final double dx = (point.x() - x()) * 0.5;
|
||||
final double dy = (point.y() - y()) * 0.5;
|
||||
final double dz = (point.z() - z()) * 0.5;
|
||||
|
||||
return result(dx, dy, dz);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final V vectTo(Vect point)
|
||||
{
|
||||
return result(point.x() - x(), point.y() - y(), point.z() - z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final V cross(Vect vec)
|
||||
{
|
||||
//@formatter:off
|
||||
return result(
|
||||
y() * vec.z() - z() * vec.y(),
|
||||
z() * vec.x() - x() * vec.z(),
|
||||
x() * vec.y() - y() * vec.x());
|
||||
//@formatter:on
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumVal dot(Vect vec)
|
||||
{
|
||||
return NumVal.make(x() * vec.x() + y() * vec.y() + z() * vec.z());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NumVal size()
|
||||
{
|
||||
final double x = x(), y = y(), z = z();
|
||||
return NumVal.make(Math.sqrt(x * x + y * y + z * z));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
package mightypork.utils.math.vect;
|
||||
|
||||
|
||||
import mightypork.utils.annotations.FactoryMethod;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mutable coord
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class VectMutable extends VectMath<VectMutable> { // returns itself on edit
|
||||
public abstract class VectMutable extends VectMathStatic<VectMutable> { // returns itself on edit
|
||||
|
||||
/**
|
||||
* Get a variable initialized as zero (0,0,0)
|
||||
@@ -74,10 +73,15 @@ public abstract class VectMutable extends VectMath<VectMutable> { // 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<VectMutable> { // 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<VectMutable> { // 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<VectMutable> { // 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);
|
||||
}
|
||||
|
||||
+26
-2
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<VectVal> {
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public static final VectVal ZERO = new VectVal(0, 0, 0);
|
||||
@SuppressWarnings("hiding")
|
||||
public static final VectVal ONE = new VectVal(1, 1, 1);
|
||||
|
||||
|
||||
/**
|
||||
* Make a constant vector
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.<br>
|
||||
* To take a static copy, use the copy() method.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class VectView extends VectMath<VectVal> { // 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<VectVal> { // 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,27 +57,12 @@ public abstract class VectView extends VectMath<VectVal> { // 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)
|
||||
{
|
||||
return VectVal.make(x, y, z);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public VectView view()
|
||||
{
|
||||
// must NOT call VectView.copy, it'd cause infinite recursion.
|
||||
return this; // already a view
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@DefaultImpl
|
||||
public double z()
|
||||
@@ -78,4 +70,15 @@ public abstract class VectView extends VectMath<VectVal> { // returns constant v
|
||||
return 0; // implemented for ease with 2D anonymous subtypes
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprectaed No point in taking view of a view.
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public VectView view()
|
||||
{
|
||||
return super.view();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user