Full Documentation

Fully Documented
Also added option for adjacent and opposite sides of the triangle based on our finite point P(r,Theta) and/or P(r,Theta,Phi)
This commit is contained in:
Paul Reilly 2023-04-17 16:32:15 -05:00
parent 043fbfeeb6
commit 913832557e
12 changed files with 327 additions and 48 deletions

View File

@ -90,11 +90,31 @@ public interface IQuaternion {
*/
double getMagnitude();
/**
* Returns the angular rotation of this quaternion.
*
* @return the angular rotation of this quaternion
*/
double getW();
/**
* Returns the x component of this quaternion.
*
* @return the x component of this quaternion
*/
double getX();
/**
* Returns the y component of this quaternion.
*
* @return the y component of this quaternion
*/
double getY();
/**
* Returns the z component of this quaternion.
*
* @return the z component of this quaternion
*/
double getZ();
}

View File

@ -109,10 +109,9 @@ public interface IVector {
* vectors. The distance is calculated from the X Y Z mods as
* sqrt(distanceSquared(vector));
*
* @see #distanceSquared(IVector)
*
* @param vector The vector to get the distance between.
* @return The distance between this vector and the vector passed in.
* @see #distanceSquared(IVector)
*/
double distance(@NotNull IVector vector);

View File

@ -12,8 +12,7 @@ package io.github.simplexdev.polarize.api.units;
* @see <a href="https://en.wikipedia.org/wiki/Azimuth">Azimuth</a>
*/
@FunctionalInterface
public interface Phi
{
public interface Phi {
/**
* This method returns a double value representing the azimuth.

View File

@ -12,8 +12,7 @@ package io.github.simplexdev.polarize.api.units;
* @see <a href="https://en.wikipedia.org/wiki/Radius">Radius</a>
*/
@FunctionalInterface
public interface Radius
{
public interface Radius {
/**
* This method returns a double value representing the length of the radius.
*

View File

@ -1,6 +1,14 @@
package io.github.simplexdev.polarize.api.units;
/**
* This is a functional interface representing a mathematical angle Theta, which returns the zenith value.
*/
@FunctionalInterface
public interface Theta {
/**
* This method returns a double value representing the zenith angle.
*
* @return the zenith angle as a double.
*/
double getZenith();
}

View File

@ -3,7 +3,10 @@ package io.github.simplexdev.polarize.math;
import io.github.simplexdev.polarize.api.IQuaternion;
public class Quaternion implements IQuaternion {
private double w, x, y, z;
private final double w;
private final double x;
private final double y;
private final double z;
public Quaternion(double w, double x, double y, double z) {
this.w = w;

View File

@ -3,27 +3,63 @@ package io.github.simplexdev.polarize.polar;
import io.github.simplexdev.polarize.api.units.Phi;
import io.github.simplexdev.polarize.api.units.Theta;
/**
* Represents a modifier that can be used in rotations of polar and spherical units.
* <p>
* Typically, these are used in full rotations along the unit circle / unit sphere,
* but can be used in any degree of rotation.
*/
public class Delta {
private final Theta theta;
private final Phi phi;
/**
* Creates a new Delta with the given theta and phi modifiers.
*
* @param theta the theta modifier.
* @param phi the phi modifier.
*/
public Delta(double theta, double phi) {
this.theta = () -> theta;
this.phi = () -> phi;
}
/**
* Returns an object that represents the theta modifier.
*
* @return an object that represents the theta modifier.
* @see Theta
*/
public Theta getTheta() {
return theta;
}
/**
* Returns an object that represents the phi modifier.
*
* @return an object that represents the phi modifier.
* @see Phi
*/
public Phi getPhi() {
return phi;
}
/**
* Returns the theta modifier.
* This is a double value for easier access.
*
* @return the theta modifier.
*/
public double theta() {
return theta.getZenith();
}
/**
* Returns the phi modifier.
* This is a double value for easier access.
*
* @return the phi modifier.
*/
public double phi() {
return phi.getAzimuth();
}

View File

@ -3,28 +3,87 @@ package io.github.simplexdev.polarize.polar;
import io.github.simplexdev.polarize.api.units.Radius;
import io.github.simplexdev.polarize.api.units.Theta;
/**
* Represents a unit in polar coordinates.
*/
public class PolarUnit {
private final Radius radius;
private final Theta theta;
/**
* Creates a new PolarUnit with the given radius and angle theta.
*
* @param radius the radius of the unit.
* @param theta the angle theta of the unit.
*/
public PolarUnit(double radius, double theta) {
this.radius = () -> radius;
this.theta = () -> theta;
}
/**
* Returns the radius object of this unit.
* <p>
* This is an object which represents the variable r, which
* represents the radius of the unit circle in 2d space.
*
* @return the radius object of this unit.
*/
public Radius getRadius() {
return radius;
}
/**
* Returns the angle object theta of this unit.
* <p>
* This is an object which represents the variable theta, which
* represents an angle on the unit circle in 2d space.
*
* @return the angle object theta of this unit.
*/
public Theta getTheta() {
return theta;
}
/**
* Returns the radius of this unit.
* This is formatted as a double for quick access.
*
* @return the radius of this unit.
*/
public double radius() {
return radius.length();
}
/**
* Returns the angle theta of this unit.
* This is formatted as a double for quick access.
*
* @return the angle theta of this unit.
*/
public double theta() {
return theta.getZenith();
}
/**
* Returns the adjacent side of the triangle formed by this unit in 2d space.
* <p>
* The adjacent side is the side adjacent the angle theta.
*
* @return the adjacent side of the triangle formed by this unit in 2d space.
*/
public double adjacent() {
return radius() * Math.cos(theta());
}
/**
* Returns the opposite side of the triangle formed by this unit in 2d space.
* <p>
* The opposite side is the side opposite the angle theta.
*
* @return the opposite side of the triangle formed by this unit in 2d space.
*/
public double opposite() {
return radius() * Math.sin(theta());
}
}

View File

@ -3,39 +3,122 @@ package io.github.simplexdev.polarize.polar;
import io.github.simplexdev.polarize.api.units.Phi;
import io.github.simplexdev.polarize.api.units.Radius;
import io.github.simplexdev.polarize.api.units.Theta;
import io.github.simplexdev.polarize.cartesian.CartesianUnit;
import io.github.simplexdev.polarize.util.Polarizer;
/**
* A class that represents a spherical unit.
*/
public class SphericalUnit {
private final Radius radius;
private final Theta theta;
private final Phi phi;
/**
* Creates a new SphericalUnit with the given radius, theta, and phi.
*
* @param radius The radius of the unit.
* @param theta The theta of the unit.
* @param phi The phi of the unit.
*/
public SphericalUnit(double radius, double theta, double phi) {
this.radius = () -> radius;
this.theta = () -> theta;
this.phi = () -> phi;
}
/**
* Returns the radius object of the unit.
* <p>
* This is an object which represents the variable r, which
* represents the radius of the unit sphere in 3d space.
*
* @return The radius object of the unit.
* @see Radius
*/
public Radius getRadius() {
return this.radius;
}
/**
* Returns the theta object of the unit.
* <p>
* This is an object which represents the variable theta, which
* represents the angle of the unit sphere in 3d space.
*
* @return The theta object of the unit.
* @see Theta
*/
public Theta getTheta() {
return this.theta;
}
/**
* Returns the phi object of the unit.
* <p>
* This is an object which represents the variable phi, which
* represents the angle of the unit sphere in 3d space.
*
* @return The phi object of the unit.
* @see Phi
*/
public Phi getPhi() {
return this.phi;
}
/**
* Returns the radius of the unit.
* This is formatted as a double for quick access.
*
* @return The radius of the unit.
*/
public double radius() {
return this.radius.length();
}
/**
* Returns the theta of the unit.
* This is formatted as a double for quick access.
*
* @return The theta of the unit.
*/
public double theta() {
return this.theta.getZenith();
}
/**
* Returns the phi of the unit.
* This is formatted as a double for quick access.
*
* @return The phi of the unit.
*/
public double phi() {
return this.phi.getAzimuth();
}
/**
* Returns the adjacent side of the unit.
* <p>
* This is calculated from a translation to Cartesian units,
* and returning the X value.
*
* @return The adjacent side of the unit.
*/
public double adjacent() {
CartesianUnit unit = Polarizer.toCartesianUnit(this);
return unit.getPoint3D().getX();
}
/**
* Returns the opposite side of the unit.
* <p>
* This is calculated from a translation to Cartesian units,
* and returning the Z value.
*
* @return The opposite side of the unit.
*/
public double opposite() {
CartesianUnit unit = Polarizer.toCartesianUnit(this);
return unit.getPoint3D().getZ();
}
}

View File

@ -14,8 +14,7 @@ import io.github.simplexdev.polarize.polar.SphericalUnit;
* This class provides static methods for converting between different polar coordinate systems and their Cartesian equivalents.
* It includes methods for converting to and from polar coordinates in 2D and 3D, as well as to and from spherical coordinates.
*/
public class Polarizer
{
public class Polarizer {
private Polarizer() {
throw new AssertionError();
}
@ -31,8 +30,7 @@ public class Polarizer
* @see <a href="https://en.wikipedia.org/wiki/Polar_coordinate_system">Polar coordinate system</a>
* @see <a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian coordinate system</a>
*/
public static CartesianUnit toCartesianUnit(PolarUnit unit)
{
public static CartesianUnit toCartesianUnit(PolarUnit unit) {
double x = unit.radius() * Math.sin(unit.theta());
double z = unit.radius() * Math.cos(unit.theta());
return new CartesianUnit(x, 0, z);
@ -50,8 +48,7 @@ public class Polarizer
* @see <a href="https://en.wikipedia.org/wiki/Spherical_coordinate_system">Spherical coordinate system</a>
* @see <a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian coordinate system</a>
*/
public static CartesianUnit toCartesianUnit(IScalar scalar, Theta theta)
{
public static CartesianUnit toCartesianUnit(IScalar scalar, Theta theta) {
double x = scalar.getMagnitude() * Math.sin(theta.getZenith());
double z = scalar.getMagnitude() * Math.cos(theta.getZenith());
return new CartesianUnit(x, 0, z);
@ -69,8 +66,7 @@ public class Polarizer
* @see <a href="https://en.wikipedia.org/wiki/Spherical_coordinate_system">Spherical coordinate system</a>
* @see <a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian coordinate system</a>
*/
public static CartesianUnit toCartesianUnit(double radius, double theta)
{
public static CartesianUnit toCartesianUnit(double radius, double theta) {
double x = radius * Math.sin(theta);
double z = radius * Math.cos(theta);
return new CartesianUnit(x, 0, z);
@ -87,8 +83,7 @@ public class Polarizer
* @see <a href="https://en.wikipedia.org/wiki/Spherical_coordinate_system">Spherical coordinate system</a>
* @see <a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian coordinate system</a>
*/
public static CartesianUnit toCartesianUnit(SphericalUnit unit)
{
public static CartesianUnit toCartesianUnit(SphericalUnit unit) {
double x = unit.radius() * Math.sin(unit.theta()) * Math.cos(unit.phi());
double y = unit.radius() * Math.cos(unit.theta());
double z = unit.radius() * Math.sin(unit.theta()) * Math.sin(unit.phi());
@ -103,8 +98,7 @@ public class Polarizer
* @param phi the phi coordinate of the vector
* @return the CartesianUnit representation of the vector
*/
public static CartesianUnit toCartesianUnit(IScalar scalar, Theta theta, Phi phi)
{
public static CartesianUnit toCartesianUnit(IScalar scalar, Theta theta, Phi phi) {
double x = scalar.getMagnitude() * Math.sin(theta.getZenith()) * Math.cos(phi.getAzimuth());
double y = scalar.getMagnitude() * Math.cos(theta.getZenith());
double z = scalar.getMagnitude() * Math.sin(theta.getZenith()) * Math.sin(phi.getAzimuth());
@ -119,8 +113,7 @@ public class Polarizer
* @param phi the phi angle in radians of the spherical coordinate
* @return the corresponding CartesianUnit
*/
public static CartesianUnit toCartesianUnit(double radius, double theta, double phi)
{
public static CartesianUnit toCartesianUnit(double radius, double theta, double phi) {
double x = radius * Math.sin(theta) * Math.cos(phi);
double y = radius * Math.cos(theta);
double z = radius * Math.sin(theta) * Math.sin(phi);
@ -133,8 +126,7 @@ public class Polarizer
* @param unit the CartesianUnit to be converted
* @return a PolarUnit representing the same point as the input CartesianUnit
*/
public static PolarUnit toPolarUnit(CartesianUnit unit)
{
public static PolarUnit toPolarUnit(CartesianUnit unit) {
double radius = Math.sqrt(
unit.getPoint2D().getX() * unit.getPoint2D().getX()
+ unit.getPoint2D().getZ() * unit.getPoint2D().getZ());
@ -150,8 +142,7 @@ public class Polarizer
* @param vector the {@link IVector} representing the radius of the resulting {@link PolarUnit}
* @return a {@link PolarUnit} representing the same point as the given {@link CartesianUnit}
*/
public static PolarUnit toPolarUnit(CartesianUnit unit, IVector vector)
{
public static PolarUnit toPolarUnit(CartesianUnit unit, IVector vector) {
double radius = vector.length();
double theta = Math.atan2(unit.getPoint2D().getX(), unit.getPoint2D().getZ());
return new PolarUnit(radius, theta);
@ -164,8 +155,7 @@ public class Polarizer
* @param vector the vector used as a reference for the polar coordinates
* @return a PolarUnit representing the polar coordinates of the given point
*/
public static PolarUnit toPolarUnit(IPoint2D point, IVector vector)
{
public static PolarUnit toPolarUnit(IPoint2D point, IVector vector) {
double radius = vector.length();
double theta = Math.atan2(point.getX(), point.getZ());
return new PolarUnit(radius, theta);
@ -178,8 +168,7 @@ public class Polarizer
* @param z the z-coordinate of the point
* @return a {@code PolarUnit} representing the polar coordinates of the point
*/
public static PolarUnit toPolarUnit(double x, double z)
{
public static PolarUnit toPolarUnit(double x, double z) {
double radius = Math.sqrt(x * x + z * z);
double theta = Math.atan2(x, z);
return new PolarUnit(radius, theta);
@ -191,8 +180,7 @@ public class Polarizer
* @param unit the CartesianUnit to be converted.
* @return the SphericalUnit representing the same point as the input CartesianUnit.
*/
public static SphericalUnit toSphericalUnit(CartesianUnit unit)
{
public static SphericalUnit toSphericalUnit(CartesianUnit unit) {
double radius = Math.sqrt(
unit.getPoint3D().getX() * unit.getPoint3D().getX()
+ unit.getPoint3D().getY() * unit.getPoint3D().getY()
@ -209,8 +197,7 @@ public class Polarizer
* @param vector the vector to use for the conversion
* @return a new {@link SphericalUnit} representing the point in spherical coordinates
*/
public static SphericalUnit toSphericalUnit(IPoint3D point, IVector vector)
{
public static SphericalUnit toSphericalUnit(IPoint3D point, IVector vector) {
double radius = vector.length();
double theta = Math.acos(point.getY() / radius);
double phi = Math.atan2(point.getX(), point.getZ());
@ -225,8 +212,7 @@ public class Polarizer
* @param z the z-coordinate
* @return a new {@code SphericalUnit} representing the converted spherical coordinates
*/
public static SphericalUnit toSphericalUnit(double x, double y, double z)
{
public static SphericalUnit toSphericalUnit(double x, double y, double z) {
double radius = Math.sqrt(x * x + y * y + z * z);
double theta = Math.acos(y / radius);
double phi = Math.atan2(x, z);

View File

@ -7,13 +7,33 @@ import io.github.simplexdev.polarize.math.Point2D;
import io.github.simplexdev.polarize.math.Point3D;
import io.github.simplexdev.polarize.math.Quaternion;
import io.github.simplexdev.polarize.polar.Delta;
import io.github.simplexdev.polarize.polar.PolarUnit;
import io.github.simplexdev.polarize.polar.SphericalUnit;
/**
* A utility class for rotating points in 2d and 3d space.
* This class supports rotations for Cartesian, Spherical, and Polar coordinates.
* <p>
* Typically, rotations in polar / spherical units are done with delta values.
* The rotations in Cartesian units are done with a quaternion.
*/
public class Rotator {
/**
* This class should not be instantiated.
*/
private Rotator() {
throw new AssertionError();
}
/**
* Rotates a point in 3d space using spherical units.
* The returned result is a point in 3d space represented by {@link IPoint3D}.
* This will rotate the point around the x-axis.
*
* @param point the point to rotate.
* @param unit the spherical unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint3D rotateX(IPoint3D point, SphericalUnit unit) {
double x = point.getX();
double y = point.getY() * Math.cos(unit.theta()) - point.getZ() * Math.sin(unit.theta());
@ -22,6 +42,15 @@ public class Rotator {
return new Point3D(x, y, z);
}
/**
* Rotates a point in 3d space using spherical units.
* The returned result is a point in 3d space represented by {@link IPoint3D}.
* This will rotate the point around the y-axis.
*
* @param point the point to rotate.
* @param unit the spherical unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint3D rotateY(IPoint3D point, SphericalUnit unit) {
double x = point.getX() * Math.cos(unit.phi()) - point.getZ() * Math.sin(unit.phi());
double y = point.getY();
@ -30,6 +59,15 @@ public class Rotator {
return new Point3D(x, y, z);
}
/**
* Rotates a point in 3d space using spherical units.
* The returned result is a point in 3d space represented by {@link IPoint3D}.
* This will rotate the point around the z-axis.
*
* @param point the point to rotate.
* @param unit the spherical unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint3D rotateZ(IPoint3D point, SphericalUnit unit) {
double x = point.getX() * Math.cos(unit.theta()) - point.getY() * Math.sin(unit.theta());
double y = point.getX() * Math.sin(unit.theta()) + point.getY() * Math.cos(unit.theta());
@ -38,6 +76,16 @@ public class Rotator {
return new Point3D(x, y, z);
}
/**
* Rotates a point in 3d space using spherical units.
* The returned result is a point in 3d space represented by {@link IPoint3D}.
* This will rotate the point around the x-axis, y-axis, and z-axis.
*
* @param point the point to rotate.
* @param delta the delta values to rotate the point with.
* @param unit the spherical unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint3D fullRotation(IPoint3D point, Delta delta, SphericalUnit unit) {
double r = unit.radius() * Math.cos(unit.theta() + delta.theta()) * Math.cos(unit.phi() + delta.phi());
double theta = Math.atan2(point.getX(), point.getZ()) + delta.theta();
@ -50,24 +98,59 @@ public class Rotator {
return new Point3D(xRot, yRot, zRot);
}
public static IPoint2D rotateX(IPoint2D point, SphericalUnit unit) {
/**
* Rotates a point in 2d space using spherical units.
* The returned result is a point in 2d space represented by {@link IPoint2D}.
* This will rotate the point around the x-axis.
*
* @param point the point to rotate.
* @param unit the polar unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint2D rotateX(IPoint2D point, PolarUnit unit) {
double x = point.getZ() * Math.cos(unit.theta()) - point.getX() * Math.sin(unit.theta());
double z = point.getZ() * Math.sin(unit.theta()) + point.getX() * Math.cos(unit.theta());
return new Point2D(x, z);
}
public static IPoint2D rotateZ(IPoint2D point, SphericalUnit unit) {
/**
* Rotates a point in 2d space using spherical units.
* The returned result is a point in 2d space represented by {@link IPoint2D}.
* This will rotate the point around the y-axis.
*
* @param point the point to rotate.
* @param unit the polar unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint2D rotateZ(IPoint2D point, PolarUnit unit) {
double x = point.getX() * Math.cos(unit.theta()) - point.getZ() * Math.sin(unit.theta());
double z = point.getX() * Math.sin(unit.theta()) + point.getZ() * Math.cos(unit.theta());
return new Point2D(x, z);
}
/**
* Rotates a point in 2d space using spherical units.
* The returned result is a point in 2d space represented by {@link IPoint2D}.
* This will rotate the point around the x-axis and z-axis.
*
* @param point the point to rotate.
* @param unit the polar unit to rotate the point with.
* @return the rotated point.
*/
public static IPoint2D fullRotation(IPoint2D point, SphericalUnit unit) {
double x = point.getX() * Math.cos(unit.theta()) - point.getZ() * Math.sin(unit.theta());
double z = point.getX() * Math.sin(unit.theta()) + point.getZ() * Math.cos(unit.theta());
return new Point2D(x, z);
}
/**
* Rotates a point in 3d space using a quaternion.
* The returned result is a point in 3d space represented by {@link IPoint3D}.
*
* @param point the point to rotate.
* @param quaternion the quaternion to rotate the point with.
* @return the rotated point.
*/
public static IPoint3D rotate(IPoint3D point, IQuaternion quaternion) {
IQuaternion pQuat = new Quaternion(0.0, point.getX(), point.getY(), point.getZ());
@ -78,6 +161,14 @@ public class Rotator {
return new Point3D(w.getX(), w.getY(), w.getZ());
}
/**
* Rotates a point in 2d space using a quaternion.
* The returned result is a point in 2d space represented by {@link IPoint2D}.
*
* @param point the point to rotate.
* @param quaternion the quaternion to rotate the point with.
* @return the rotated point.
*/
public static IPoint2D rotate(IPoint2D point, IQuaternion quaternion) {
IQuaternion pQuat = new Quaternion(0.0, point.getX(), 0.0, point.getZ());

View File

@ -1,11 +1,6 @@
package io.github.simplexdev.polarize.util;
public class Utilities
{
private Utilities() {
throw new AssertionError();
}
public class Utilities {
/**
* The value of pi divided by 4.
* This represents 45 degrees in radians.
@ -31,6 +26,9 @@ public class Utilities
* This represents 360 degrees in radians.
*/
public static final double RADIAN_360 = Math.PI * 2;
private Utilities() {
throw new AssertionError();
}
/**
* Calculates the magnitude of a vector in 2D Cartesian coordinate system.
@ -39,8 +37,7 @@ public class Utilities
* @param z the z-coordinate of the vector
* @return the magnitude of the vector
*/
public static double magnitudeOf(double x, double z)
{
public static double magnitudeOf(double x, double z) {
return Math.sqrt(x * x + z * z);
}
@ -52,8 +49,7 @@ public class Utilities
* @param z the z-coordinate of the vector
* @return the magnitude of the vector
*/
public static double magnitudeOf(double x, double y, double z)
{
public static double magnitudeOf(double x, double y, double z) {
return Math.sqrt(x * x + y * y + z * z);
}
}