public class Vec2D
extends java.awt.geom.Point2D.Double
implements java.lang.Cloneable
Modifier and Type | Field and Description |
---|---|
private static double |
toStringRoundFactor |
static double |
TwoPI |
Constructor and Description |
---|
Vec2D() |
Vec2D(double x,
double y) |
Vec2D(java.awt.geom.Point2D p) |
Vec2D(java.awt.geom.Point2D from,
java.awt.geom.Point2D to) |
Modifier and Type | Method and Description |
---|---|
Vec2D |
add(double dx,
double dy) |
Vec2D |
add(java.awt.geom.Point2D p) |
Vec2D |
addScaled(java.awt.geom.Point2D p,
double scale) |
double |
angleBetween(Vec2D v2)
Gets the smallest angle between two vectors.
|
double |
angleTo(Vec2D v2)
Gets the full angle from this vector to v2, sweeping counter-clockwise.
|
double |
arcAngleTo(Vec2D v2,
boolean reverse)
This calculates the angle from one vector to another, similar to
angleTo(Vec2D) except that the
output is intended for applications in which the direction of travel is indicated by the sign of the result
and the extent of travel is indicated by its absolute value. |
Vec2D |
clone() |
static double |
distance(java.awt.geom.Point2D p1,
java.awt.geom.Point2D p2)
Returns the distance between two points.
|
static double |
distanceSq(java.awt.geom.Point2D p1,
java.awt.geom.Point2D p2)
Returns the square of the distance between two points, i.e.
|
double |
dot(java.awt.geom.Point2D endPoint,
java.awt.geom.Point2D referencePoint) |
double |
dot(Vec2D v) |
static Vec2D |
fromAngle(double theta) |
Vec2D |
getAdded(java.awt.geom.Point2D p) |
double |
getAngle() |
Vec2D |
getMidpoint(java.awt.geom.Point2D other) |
static Vec2D |
getMidpoint(java.awt.geom.Point2D start,
java.awt.geom.Point2D end) |
Vec2D |
getMultiplied(java.awt.geom.Point2D p) |
Vec2D |
getNegated() |
Vec2D |
getNormalized() |
Vec2D |
getPointAlong(java.awt.geom.Point2D startPoint,
double lengthAlongVector)
Returns the location of result to be the point that would be reached by starting
at startPoint and traveling along the direction of this vector the distance specified by lengthAlongVector.
|
Vec2D |
getPointAlongScaled(java.awt.geom.Point2D startPoint,
double lengthMultiple)
Sets the location of result to be the point that would be reached by starting
at startPoint and traveling along the direction of this vector the specified multiple of this vector's length.
|
static Vec2D |
getPointBetween(java.awt.geom.Point2D start,
java.awt.geom.Point2D end,
double fractionOfDistance) |
Vec2D |
getRotated(double theta)
Get a copy of this vector rotated counter-clockwise by the specified angle (in radians)
|
Vec2D |
getRotated90()
Get a copy of this vector rotated counter-clockwise by 90 degrees
|
Vec2D |
getScaled(double scale) |
Vec2D |
getScaled(double scaleX,
double scaleY) |
Vec2D |
getSubtr(java.awt.geom.Point2D p) |
Vec2D |
getTransformed(java.awt.geom.AffineTransform tr) |
double |
length()
The magnitude (aka Euclidean length) of the vector.
|
double |
lengthSqr() |
static double |
mod2PI(double angle)
Given any angle in radians, this returns an equivalent angle in the range [0, 2*PI )
Note that 2*PI is an exclusive upper limit (unlike wrapTo2PI).
|
Vec2D |
multiply(java.awt.geom.Point2D p) |
Vec2D |
negate() |
Vec2D |
normalize() |
Vec2D |
normalize(double x_if_Zero,
double y_if_Zero) |
Vec2D |
projectedOn(Vec2D direction)
Calculates the projection of this vector onto the vector specified by direction.
|
static Vec2D |
reflect(java.awt.geom.Point2D pt,
Vec2D reflectionVector,
java.awt.geom.Point2D pointOnReflectionLine) |
Vec2D |
rotate(double theta) |
Vec2D |
rotate90()
Rotates this vector 90 degrees counter-clockwise.
|
Vec2D |
scale(double scale) |
Vec2D |
scale(double scaleX,
double scaleY) |
Vec2D |
setLength(double newLength)
Sets the length (magnitude) of this vector.
|
Vec2D |
setTo(double newX,
double newY)
Alias of
Point2D.Double.setLocation(double, double) |
Vec2D |
setTo(java.awt.geom.Point2D p)
Alias of
Point2D.setLocation(Point2D) |
Vec2D |
setTo(Vec2D p) |
Vec2D |
subtr(java.awt.geom.Point2D p) |
java.awt.geom.Line2D |
toLine()
Returns a line starting at (0,0) and ending at the tip of the vector -- (x, y).
|
java.awt.geom.Line2D |
toLine(java.awt.geom.Point2D start)
Returns a line starting at the given point and ending at the starting point + the vector -- i.e.
|
java.awt.geom.Line2D |
toLine(java.awt.geom.Point2D start,
double distance)
Returns a line starting at the given point, in the direction of the vector, and with the specified distance.
|
java.awt.Point |
toPoint() |
java.awt.geom.Point2D.Float |
toPointF() |
java.lang.String |
toString() |
Vec2D |
transform(java.awt.geom.AffineTransform tr) |
static double |
wrapTo2PI(double angle)
Given any angle in radians, this returns an equivalent angle in the range [0, 2*PI]
In general, multiples of 2*PI map to 0 if they are negative and to 2*PI if they are positive.
|
static double |
wrapToPI(double angle)
Given any angle in radians, this returns an equivalent angle in the range [-PI, PI]
In general, odd multiples of PI map to -PI if they are negative and to PI if they are positive.
|
private static final double toStringRoundFactor
public static final double TwoPI
public Vec2D()
public Vec2D(double x, double y)
public Vec2D(java.awt.geom.Point2D p)
public Vec2D(java.awt.geom.Point2D from, java.awt.geom.Point2D to)
public Vec2D add(double dx, double dy)
public Vec2D add(java.awt.geom.Point2D p)
public Vec2D addScaled(java.awt.geom.Point2D p, double scale)
public double angleBetween(Vec2D v2)
A · B = |A| * |B| * cos(θ)
public double angleTo(Vec2D v2)
v1.angleTo(v2) == -v2.angleTo(v1)
public double arcAngleTo(Vec2D v2, boolean reverse)
angleTo(Vec2D)
except that the
output is intended for applications in which the direction of travel is indicated by the sign of the result
and the extent of travel is indicated by its absolute value. Thus instead of returning a value from -PI to PI, this
function returns a value from -2PI to 2PI.
For example, want to draw an arc from 0 degrees to 270 degrees counter-clockwise. (I.e. the arc should span 3 quadrants.)
So v1 = (1, 0) and v2 = (0, -1)
But if angleTo(Vec2D)
were used, it would return -90 degrees, which would result in the arc being drawn CLOCKWISE through only the bottom-right quadrant.
This function would return 270 degrees (reverse = false) or -90 (reverse = true);
As another example, assume v1 points to (-1, 0) and v2 points to (1, 0). This function would return 180 (reverse = false) or -180 (reverse = true);
As another example, assume v1 points to (1, 1) and v2 points to (-1, 1). This function would return 90 (reverse = false) or -270 (reverse = true);v2
- the other vectorreverse
- whether the arc should be drawn counter-clockwise (reverse=false) or clockwise (reverse=true) from v1 to v2.
Alternatively reverse=true could be interpreted as "draw the arc counter-clockwise from v2 to v1 instead of from v1 to v2"
If reverse is true, the output will be in the range (-360, 0]
If reverse is false, the output will be in the range [0, 360)public Vec2D clone()
clone
in class java.awt.geom.Point2D
public static double distance(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
p1
- one of the pointsp2
- one of the pointspublic static double distanceSq(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
(p1.x - p2.x2)^2 + (p1.y - p2.y)^2
p1
- one of the pointsp2
- one of the pointspublic double dot(java.awt.geom.Point2D endPoint, java.awt.geom.Point2D referencePoint)
public double dot(Vec2D v)
public static Vec2D fromAngle(double theta)
public Vec2D getAdded(java.awt.geom.Point2D p)
public double getAngle()
public Vec2D getMidpoint(java.awt.geom.Point2D other)
public static Vec2D getMidpoint(java.awt.geom.Point2D start, java.awt.geom.Point2D end)
public Vec2D getMultiplied(java.awt.geom.Point2D p)
public Vec2D getNegated()
public Vec2D getNormalized()
public Vec2D getPointAlong(java.awt.geom.Point2D startPoint, double lengthAlongVector)
this.clone().setLength(lengthAlongVector).add(startPoint)
public Vec2D getPointAlongScaled(java.awt.geom.Point2D startPoint, double lengthMultiple)
this.clone().scale(lengthMultiple).add(startPoint)
For example @{code new Vec2D(p1, p2).setPointAlongScaled(p1, 0.5, p3) } would set the
point p3 to be the midpoint between p1 and p2.public static Vec2D getPointBetween(java.awt.geom.Point2D start, java.awt.geom.Point2D end, double fractionOfDistance)
public Vec2D getRotated(double theta)
public Vec2D getRotated90()
public Vec2D getScaled(double scale)
public Vec2D getScaled(double scaleX, double scaleY)
public Vec2D getSubtr(java.awt.geom.Point2D p)
public Vec2D getTransformed(java.awt.geom.AffineTransform tr)
public double length()
public double lengthSqr()
public static double mod2PI(double angle)
public Vec2D multiply(java.awt.geom.Point2D p)
public Vec2D negate()
public Vec2D normalize()
public Vec2D normalize(double x_if_Zero, double y_if_Zero)
public Vec2D projectedOn(Vec2D direction)
direction * (this·direction)
More generally, this is equal to direction.normalize().scale(this·direction/direction.length())
public static Vec2D reflect(java.awt.geom.Point2D pt, Vec2D reflectionVector, java.awt.geom.Point2D pointOnReflectionLine)
public Vec2D rotate(double theta)
public Vec2D rotate90()
public Vec2D scale(double scale)
public Vec2D scale(double scaleX, double scaleY)
public Vec2D setLength(double newLength)
normalize()
followed by scale(double)
public Vec2D setTo(double newX, double newY)
Point2D.Double.setLocation(double, double)
public Vec2D setTo(java.awt.geom.Point2D p)
Point2D.setLocation(Point2D)
public Vec2D subtr(java.awt.geom.Point2D p)
public java.awt.geom.Line2D toLine()
public java.awt.geom.Line2D toLine(java.awt.geom.Point2D start)
public java.awt.geom.Line2D toLine(java.awt.geom.Point2D start, double distance)
public java.awt.Point toPoint()
public java.awt.geom.Point2D.Float toPointF()
public java.lang.String toString()
toString
in class java.awt.geom.Point2D.Double
public Vec2D transform(java.awt.geom.AffineTransform tr)
public static double wrapTo2PI(double angle)
public static double wrapToPI(double angle)