package org.djutils.draw.point; import java.awt.geom.Point2D; import java.util.Arrays; import java.util.Iterator; import java.util.Locale; import org.djutils.base.AngleUtil; import org.djutils.draw.DrawRuntimeException; import org.djutils.draw.Oriented2d; import org.djutils.exceptions.Throw; /** * The OrientedPoint2d is a point in a 2-dimensional space with an orientation vector, which is specified in terms of its * counter-clockwise rotation around the point in radians. *
* Copyright (c) 2020-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See DJUTILS License.
*
fraction
is 0;
* this
Point is returned; if fraction
is 1, the other point
is returned
* @return OrientedPoint2d; a new OrientedPoint2d at the requested fraction
* @throws NullPointerException when otherPoint is null
* @throws IllegalArgumentException when fraction is NaN
*/
public OrientedPoint2d interpolate(final OrientedPoint2d otherPoint, final double fraction)
throws NullPointerException, IllegalArgumentException
{
Throw.whenNull(otherPoint, "point cannot be null");
Throw.when(Double.isNaN(fraction), IllegalArgumentException.class, "fraction must be a number (not NaN)");
return new OrientedPoint2d((1.0 - fraction) * getX() + fraction * otherPoint.x,
(1.0 - fraction) * getY() + fraction * otherPoint.y,
AngleUtil.interpolateShortest(getDirZ(), otherPoint.getDirZ(), fraction));
}
/**
* Return a new OrientedPoint2d with an in-place rotation around the z-axis by the provided delta. The resulting rotation is
* normalized between -π and π.
* @param rotateZ double; the rotation around the z-axis
* @return OrientedPoint; a new point with the same coordinates and applied rotation
* @throws IllegalArgumentException when deltaRotZ is NaN
*/
public OrientedPoint2d rotate(final double rotateZ) throws IllegalArgumentException
{
Throw.when(Double.isNaN(rotateZ), IllegalArgumentException.class, "deltaDirZ must be a number (not NaN)");
return new OrientedPoint2d(getX(), getY(), AngleUtil.normalizeAroundZero(getDirZ() + rotateZ));
}
/** {@inheritDoc} */
@Override
public double getDirZ()
{
return this.dirZ;
}
/** {@inheritDoc} */
@Override
public Iterator extends OrientedPoint2d> getPoints()
{
return Arrays.stream(new OrientedPoint2d[] { this }).iterator();
}
/** {@inheritDoc} */
@Override
public String toString()
{
return toString("%f", false);
}
/** {@inheritDoc} */
@Override
public String toString(final String doubleFormat, final boolean doNotIncludeClassName)
{
String format =
String.format("%1$s[x=%2$s, y=%2$s, rot=%2$s]", doNotIncludeClassName ? "" : "OrientedPoint2d ", doubleFormat);
return String.format(Locale.US, format, this.x, this.y, this.dirZ);
}
/** {@inheritDoc} */
@Override
public boolean epsilonEquals(final OrientedPoint2d other, final double epsilonCoordinate, final double epsilonRotation)
throws NullPointerException, IllegalArgumentException
{
Throw.whenNull(other, "other point cannot be null");
if (Math.abs(this.x - other.x) > epsilonCoordinate)
{
return false;
}
if (Math.abs(this.y - other.y) > epsilonCoordinate)
{
return false;
}
if (Math.abs(AngleUtil.normalizeAroundZero(this.dirZ - other.dirZ)) > epsilonRotation)
{
return false;
}
return true;
}
/** {@inheritDoc} */
@Override
public int hashCode()
{
final int prime = 31;
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(this.dirZ);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("checkstyle:needbraces")
public boolean equals(final Object obj)
{
if (this == obj)
return true;
if (!super.equals(obj))
return false;
OrientedPoint2d other = (OrientedPoint2d) obj;
if (Double.doubleToLongBits(this.dirZ) != Double.doubleToLongBits(other.dirZ))
return false;
return true;
}
}