Skip to content

Commit

Permalink
Removed hard dep on coding lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGuardsman committed Feb 9, 2018
1 parent d1f3cb2 commit 167bf35
Show file tree
Hide file tree
Showing 23 changed files with 857 additions and 1,580 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.builtbroken.jlib.data.network;

import io.netty.buffer.ByteBuf;

/** Applied to objects that can read data from a byte buf to
* update there own contents.
* Created by robert on 1/11/2015.
*/
public interface IByteBufReader
{
/** Called to read an object from byte buf
* @param buf a {@link ByteBuf} to read from
* @return instance of the object
*/
Object readBytes(ByteBuf buf);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.builtbroken.jlib.data.network;

import io.netty.buffer.ByteBuf;

/** Applied to objects that can write there own
* data to the ByteBuf stream. If used with the
* packet handler you need to have a constructor
* to create the object on the other end.
*
* Created by robert on 1/11/2015.
*/
public interface IByteBufWriter
{
/**
* @param buf a {@link ByteBuf} to write to.
*/
ByteBuf writeBytes(ByteBuf buf);
}
33 changes: 33 additions & 0 deletions src/main/java/com/builtbroken/jlib/data/vector/IPos2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.builtbroken.jlib.data.vector;

/**
* Useful interface to define that an object has a 2D location.
*
* @author DarkGuardsman
*/
public interface IPos2D
{
double x();

double y();

default int xi()
{
return (int) Math.floor(x());
}

default int yi()
{
return (int) Math.floor(y());
}

default float xf()
{
return (float) x();
}

default float yf()
{
return (float) y();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/builtbroken/jlib/data/vector/IPos3D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.builtbroken.jlib.data.vector;

/**
* Useful interface to define that an object has a 3D location.
*
* @author DarkGuardsman
*/
public interface IPos3D extends IPos2D
{
double z();

default float zf()
{
return (float) z();
}

default int zi()
{
return (int) Math.floor(z());
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/builtbroken/jlib/data/vector/ITransform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.builtbroken.jlib.data.vector;

/**
* Applied to objects that can transform vectors
*
* @Calclavia
*/
public interface ITransform
{
IPos3D transform(IPos3D vector);
}
197 changes: 197 additions & 0 deletions src/main/java/com/builtbroken/jlib/data/vector/Pos2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package com.builtbroken.jlib.data.vector;

/** Basic implementation of a 2D position with math operators
*
* Created by robert on 1/11/2015.
*/
public abstract class Pos2D<R extends Pos2D> extends Pos2DBean
{
public Pos2D(double x, double y)
{
super(x, y);
}

public Pos2D()
{
this(0, 0);
}

public R add(double x, double y)
{
return newPos(x + x(), y + y());
}

public R add(IPos2D other)
{
return add(other.x(), other.y());
}

public R add(double a)
{
return add(a, a);
}

public R sub(double x, double y)
{
return add(-x, -y);
}

public R sub(IPos2D other)
{
return sub(other.x(), other.y());
}

public R sub(double a)
{
return sub(a, a);
}


public R multiply(IPos2D pos)
{
return newPos(pos.x() * x(), pos.y() * y());
}

public R multiply(double x, double y)
{
return newPos(x * x(), y * y());
}

public R multiply(double a)
{
return multiply(a, a);
}


public R divide(IPos2D pos)
{
return newPos( x() / pos.x(), y() / pos.y());
}

public R divide(double x, double y)
{
return newPos(x() / x, y() / y);
}

public R divide(double a)
{
return divide(a, a);
}

public R rotate(double angle)
{
return newPos(x() * Math.cos(angle) - y() * Math.sin(angle), x() * Math.sin(angle) + y() * Math.cos(angle));
}

public double dotProduct(IPos2D other)
{
return x() * other.x() + y() * other.y();
}

public double magnitudeSquared()
{
return x() * x() + y() * y();
}

public double magnitude()
{
return Math.sqrt(magnitudeSquared());
}

public R normalize() { return this.divide(magnitude()); }

public double distance(IPos2D other)
{
return (this.sub(other)).magnitude();
}

public R midpoint(IPos2D other)
{
return (R)add(other).divide(2);
}

public boolean isZero()
{
return x() == 0 && y() == 0;
}

/**
* Gets the slow or ratio of change between
* two points
*
* @param other - point to use
* @return slope
*/
public double slope(IPos2D other)
{
return (y() - other.y()) / (x() - other.x());
}

/**
* Rounds down
*
* @return new Pos
*/
public R round()
{
return newPos(Math.round(x()), Math.round(y()));
}

/**
* Rounds up
*
* @return new Pos
*/
public R ceil()
{
return newPos(Math.ceil(x()), Math.ceil(y()));
}

/**
* Rounds down
*
* @return new Pos
*/
public R floor()
{
return newPos(Math.floor(x()), Math.floor(y()));
}

/**
* Creates a new point that represents the max between the two points
*
* @return new Pos
*/
public R max(IPos2D other)
{
return newPos(Math.max(x(), other.x()), Math.max(y(), other.y()));
}

/**
* Creates a new point that represents the min between the two points
*
* @return new Pos
*/
public R min(IPos2D other)
{
return newPos(Math.min(x(), other.x()), Math.min(y(), other.y()));
}

/**
* Gets the reciprocal or the value of 1 over (x, y)
*
* @return new Pos
*/
public R reciprocal()
{
return newPos(1 / x(), 1 / y());
}

@Override
public R clone()
{
return newPos(this.x(), this.y());
}

public abstract R newPos(double x, double y);
}
Loading

0 comments on commit 167bf35

Please sign in to comment.