public class VonaMath
extends java.lang.Object
Vona's utils: math misc.
Copyright (C) 2004 Marsette A. Vona, III
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
cvsid
CVS id.
|
static double |
DP
similar to
FP |
static double |
DP2R
square root of
DP |
static double |
DP3R
cube root of
DP |
static double |
DP4R
4th root of
DP |
static double |
EPSILON
A default epsilon.
|
static double |
EPSILON2
Default epsilon squared.
|
static float |
FP
32-bit IEEE floating point precision, defined as the largest value for
which
(1.0f+FP)-1.0f == 0.0f . |
static float |
FP2R
square root of
FP |
static float |
FP3R
cube root of
FP |
static float |
FP4R
4th root of
FP |
static float |
FTWO_PI
TWO_PI as a float |
static double |
SQRT_2
sqrt(2.0). |
static double |
TWO_PI
2*pi
|
Constructor and Description |
---|
VonaMath() |
Modifier and Type | Method and Description |
---|---|
static double[] |
add2(double[] v1,
double[] v2)
Compute the sum of two two-dimensional vectors.
|
static int |
addMod(int a0,
int a1,
int m)
Compute
(a0 + a1) mod m . |
static double |
angle(double[] v)
Return the CCW angle from the positive X axis to the 2D vector
v . |
static double |
angle(double x,
double y)
Return the CCW angle from the positive X axis to the vector
(x,
y) , in the range [0, 2*PI). |
static double |
canonicalizeAngle(double t)
Return the canonical angle in the range (-PI, PI]
corresponding to any angle
t . |
static double |
canonicalizeAngle180(double t)
Same as
canonicalizeAngle(double) but operates on degrees. |
static double |
canonicalizeAngle360(double t)
Same as
canonicalizeAngle180(double) but returns an angle in the range
(0, 360]. |
static double |
clamp(double value,
double min,
double max)
Clamp
value to the range [min ,
max ]. |
static boolean |
closeToZero(double d)
Check whether
d epsilonEquals(double, double)
0.0. |
static boolean |
closeToZero(double x,
double y)
Check whether the 2D vector
(x, y) is nearly (0.0,
0.0). |
static boolean |
closeToZero2(double[] v)
Check whether the 2D vector
v is nearly (0.0, 0.0). |
static double[] |
copy2(double[] dest,
double[] v)
Copy a two-dimensional vector.
|
static double[] |
cross3(double[] ret,
double[] v1,
double[] v2)
Compute the cross product of two 3D vectors.
|
static double |
crossMagnitudeSquared3(double[] v1,
double[] v2)
Compute the square of the magnitude of the cross product of two 3D
vectors.
|
static double |
crossZ(double[] v1,
double[] v2)
Compute the Z component of the cross product of the two 2D vectors
v1 and v2 . |
static double |
crossZ(double[] s1,
double[] e1,
double[] s2,
double[] e2)
Compute the Z component of the cross product of the two 2D vectors
s1->e1 and s2->e2 . |
static double |
crossZ(double v1x,
double v1y,
double v2x,
double v2y)
Compute the Z component of the cross product of the two 2D vectors
v1 and v2 . |
static double |
crossZ(double s1x,
double s1y,
double e1x,
double e1y,
double s2x,
double s2y,
double e2x,
double e2y)
Compute the Z component of the cross product of the two 2D vectors
s1->e1 and s2->e2 . |
static int |
crossZSign(double v1x,
double v1y,
double v2x,
double v2y)
Compute the sign of the cross product of two 2D vectors.
|
static double |
distance(double p1x,
double p1y,
double p2x,
double p2y)
Compute the distance between the 2D points
(p1x,
p1y) and (p2x, p2y) . |
static double |
distance2(double[] start,
double[] end)
Compute the distance between the 2D points
start and
end . |
static double |
distanceSquared(double p1x,
double p1y,
double p2x,
double p2y)
Compute the square of the distance between the 2D points
(p1x,
p1y) and (p2x, p2y) . |
static double |
distanceSquared2(double[] start,
double[] end)
Compute the square of the distance between the 2D points
start and end . |
static double |
dot(double v1x,
double v1y,
double v2x,
double v2y)
Compute the dot product of the 2D vectors
v1 and
v2 . |
static double |
dot2(double[] v1,
double[] v2)
Compute the dot product of the 2D vectors
v1 and
v2 . |
static double |
dotAny(double[] v1,
double[] v2)
Compute the dot product of two arbitrary-dimensional vectors
v1 and v2 . |
static double[] |
dup2(double[] v)
Duplicate a two-dimensional vector.
|
static boolean |
epsilonEquals(double a,
double b)
Same as
epsilonEquals(double, double, double) but always uses
EPSILON . |
static boolean |
epsilonEquals(double a,
double b,
double eps)
Check whether the difference between
a and b
is at most eps . |
static boolean |
epsilonEquals(double v1x,
double v1y,
double v2x,
double v2y)
Check whether the the 2D vectors
v1 and
v2 are approximately equal. |
static boolean |
epsilonEquals2(double[] v1,
double[] v2)
Check whether the the 2D vectors
v1 and
v2 are approximately equal. |
static int |
findClosest(double theta,
double[] angle)
Find the closest angle to
theta in an array of angles. |
static double |
getPolyWidth(float[] x,
float[] y,
double[] angle,
double theta)
Compute the parallel-jaws calipers width of a convex polygon.
|
static boolean |
inAngleRange(double from,
double to,
double where)
Check whether angle
where is in the angle range
[from, to] . |
static double[] |
interpolateAny(double[] result,
double[] v1,
double[] v2,
double r)
Interpolate between two arbitrary-dimensional vectors.
|
static double[] |
intersect(double[] where,
double sx,
double sy,
double dx,
double dy,
double cx,
double cy,
double r)
2D segment intersect circle.
|
static float |
intersect(float[] s,
float[] d,
float[] v0,
float[] v1,
float[] v2,
boolean cullBackface,
boolean cullFrontFace)
Intersect the 3D segment from s extending by vector d
with the 3D triangle v0, v1, v2.
|
static double[] |
intersect2(double[] where,
double[] s,
double[] d,
double extent,
double[] corner,
double[] diag)
2D segment intersect rectangle.
|
static double[] |
intersect2(double[] where,
double[] s0,
double[] d0,
double extent0,
double[] s1,
double[] d1,
double extent1)
2D segment intersection.
|
static float |
intersect3(float[] where,
float[] s,
float[] d,
float[] c,
float r,
boolean checkOnly)
3D segment intersect sphere.
|
static double |
magnitude(double x,
double y)
Compute the magnitude of the 2D vector
(x, y) . |
static double |
magnitude2(double[] v)
Compute the magnitude of the 2D vector
v . |
static double |
magnitudeAny(double[] v)
Compute the magnitude of the arbitrary-dimensional vector
v . |
static double |
magnitudeSquared(double x,
double y)
Compute the square of the magnitude of the 2D vector
(x,
y) . |
static double |
magnitudeSquared2(double[] v)
Compute the square of the magnitude of the 2D vector
v . |
static double |
magnitudeSquaredAny(double[] v)
Compute the square of the magnitude of the arbitrary-dimensional vector
v . |
static double[] |
minvert44rht(double[] mi,
double[] m)
mi = (m)^{-1} for m 4x4 rigid homogenous transform, mi != m
|
static float[] |
minvert44rht(float[] mi,
float[] m)
mi = (m)^{-1} for m 4x4 rigid homogenous transform, mi != m
|
static double[] |
mmult44(double[] m,
double[] mb,
double[] ma)
4x4 m = (mb)(ma) for m != mb and m != ma
|
static float[] |
mmult44(float[] m,
float[] mb,
float[] ma)
4x4 m = (mb)(ma) for m != mb and m != ma
|
static double[] |
mscale44ht(double[] m,
double s)
append a scaling matrix to 4x4 row-major homogenous transform m
|
static float[] |
mscale44ht(float[] m,
float s)
append a scaling matrix to 4x4 row-major homogenous transform m
|
static float[] |
mutualPerp(float[] sa,
float[] a,
float[] sb,
float[] b,
float[] u)
Covers
mutualPerp(float[], float[], float[], float[], float[],
float) , uses Float.NaN when parallel. |
static float[] |
mutualPerp(float[] sa,
float[] a,
float[] sb,
float[] b,
float[] u,
float parallel)
Find the mutual perpendicular between two line segments in 3D.
|
static double[] |
normalizeAny(double[] v)
Normalize an arbitrary-dimensional vector.
|
static int |
percent(double value)
Convert a double value in the range [0.0, 1.0] to an integer percent in
the range [0, 100].
|
static double |
project(double sx,
double sy,
double dx,
double dy,
double px,
double py)
Project 2D point
(px, py) onto the segment starting at
(sx, sy) and extending by the vector (dx,
dy) . |
static double |
project(double sx,
double sy,
double sz,
double dx,
double dy,
double dz,
double px,
double py,
double pz)
Same as
project(double, double, double, double, double,
double) but in 3D. |
static float |
project(float sx,
float sy,
float sz,
float dx,
float dy,
float dz,
float px,
float py,
float pz)
|
static float |
project3(float[] s,
float[] d,
float[] p)
|
static double |
random(double from,
double to)
Return a random double in the range
[from, to) if from
\leq to, else (to, from] . |
static float[] |
randomDirection(float[] v,
float length)
make v a random direction vector with the given length
|
static double[] |
reflectAny(double[] r,
double[] i,
double[] n)
Arbitrary-dimensional mirror reflection.
|
static boolean |
relativeEquals(double a,
double b)
Covers
relativeEquals(double, double, double) , uses EPSILON . |
static boolean |
relativeEquals(double a,
double b,
double precision)
Check whether the relative difference between a and
b is less than precision.
|
static double[] |
rotY(double[] r,
double[] v,
double angle)
Rotate 3D vector
v CCW by angle about the Y
axis, return in r . |
static double[] |
scale2(double[] v,
double s)
Scale a two-dimensional vector.
|
static double |
signum(double value)
Mathematical signum.
|
static double[] |
sub2(double[] v1,
double[] v2)
Compute the difference of two two-dimensional vectors.
|
private static final java.lang.String cvsid
CVS id.
public static float FP
32-bit IEEE floating point precision, defined as the largest value for
which (1.0f+FP)-1.0f == 0.0f
.
See TimeMath
for how this is computed.
public static float FP2R
FP
public static float FP3R
FP
public static float FP4R
FP
public static double DP
FP
public static double DP2R
DP
public static double DP3R
DP
public static double DP4R
DP
public static double EPSILON
A default epsilon.
public static double EPSILON2
Default epsilon squared.
public static double SQRT_2
sqrt(2.0).
public static double TWO_PI
public static float FTWO_PI
TWO_PI
as a floatpublic static boolean epsilonEquals(double a, double b, double eps)
Check whether the difference between a
and b
is at most eps
.
a
- one number to checkb
- the other number to checkeps
- the epsilona
and b
is at most eps
public static boolean epsilonEquals(double a, double b)
Same as epsilonEquals(double, double, double)
but always uses
EPSILON
.
public static boolean closeToZero(double d)
Check whether d
epsilonEquals(double, double)
0.0.
d
- the double value to checkd
epsilonEquals(double, double)
0.0public static boolean relativeEquals(double a, double b, double precision)
Check whether the relative difference between a and b is less than precision.
This compares floating point values for equality in a way which respects their diminishing precision with respect to magnitude.
References:
public static boolean relativeEquals(double a, double b)
Covers relativeEquals(double, double, double)
, uses EPSILON
.
public static double random(double from, double to)
Return a random double in the range [from, to)
if from
\leq to, else (to, from]
.
from
- the start of the rangeto
- the end of the range[from, to)
if from \leq
to, else (to, from]
public static float[] randomDirection(float[] v, float length)
public static int addMod(int a0, int a1, int m)
Compute (a0 + a1) mod m
.
a0
- the first addenda1
- the second addendm
- the modulus(a0 + a1) mod m
public static int percent(double value)
Convert a double value in the range [0.0, 1.0] to an integer percent in the range [0, 100].
value
- the value to convertpublic static double signum(double value)
Mathematical signum.
value
- the value for which to compute the signpublic static double clamp(double value, double min, double max)
Clamp value
to the range [min
,
max
].
value
- the value to clampmin
- the bottom of the rangemax
- the top of the rangevalue
clamped to the range [min
,
max
]public static double distance(double p1x, double p1y, double p2x, double p2y)
Compute the distance between the 2D points (p1x,
p1y)
and (p2x, p2y)
.
p1x
- the first point x coordinatep1y
- the first point y coordinatep2x
- the second point x coordinatep2y
- the second point y coordinatepublic static double distance2(double[] start, double[] end)
Compute the distance between the 2D points start
and
end
.
start
- the first point, not nullend
- the second point, not nullpublic static double distanceSquared(double p1x, double p1y, double p2x, double p2y)
Compute the square of the distance between the 2D points (p1x,
p1y)
and (p2x, p2y)
.
p1x
- the first point x coordinatep1y
- the first point y coordinatep2x
- the second point x coordinatep2y
- the second point y coordinatepublic static double distanceSquared2(double[] start, double[] end)
Compute the square of the distance between the 2D points
start
and end
.
start
- the first point, not nullend
- the second point, not nullpublic static boolean epsilonEquals(double v1x, double v1y, double v2x, double v2y)
Check whether the the 2D vectors v1
and
v2
are approximately equal.
v1x
- the first vector x coordv1y
- the first vector y coordv2x
- the second vector x coordv2y
- the second vector y coordv1
and v2
are epsilonEquals(double, double)
coordinate-wisepublic static boolean epsilonEquals2(double[] v1, double[] v2)
Check whether the the 2D vectors v1
and
v2
are approximately equal.
v1
- the first point, not nullv2
- the second point, not nullv1
and v2
are epsilonEquals(double, double)
coordinate-wisepublic static boolean closeToZero(double x, double y)
Check whether the 2D vector (x, y)
is nearly (0.0,
0.0).
x
- the x-coord of the vector to checky
- the y-coord of the vector to check(x, y)
is epsilonEquals(double,
double)
(0.0, 0.0) coordinate-wisepublic static boolean closeToZero2(double[] v)
Check whether the 2D vector v
is nearly (0.0, 0.0).
v
- the vector to check, not nullv
is epsilonEquals(double,
double)
(0.0, 0.0) coordinate-wisepublic static double magnitudeSquared(double x, double y)
Compute the square of the magnitude of the 2D vector (x,
y)
.
x
- the x-coord of the vectory
- the y-coord of the vector(x, y)
public static double magnitudeSquared2(double[] v)
Compute the square of the magnitude of the 2D vector
v
.
v
- the vector, not nullv
public static double magnitudeSquaredAny(double[] v)
Compute the square of the magnitude of the arbitrary-dimensional vector
v
.
v
- the vector, not nullv
public static double magnitude(double x, double y)
Compute the magnitude of the 2D vector (x, y)
.
x
- the x-coord of the vectory
- the y-coord of the vector(x, y)
public static double magnitude2(double[] v)
Compute the magnitude of the 2D vector v
.
v
- the vectorv
public static double magnitudeAny(double[] v)
Compute the magnitude of the arbitrary-dimensional vector
v
.
v
- the vector, not nullv
public static double dot(double v1x, double v1y, double v2x, double v2y)
Compute the dot product of the 2D vectors v1
and
v2
.
v1x
- the first vector x coordv1y
- the first vector y coordv2x
- the second vector x coordv2y
- the second vector y coordv1
and v2
public static double dot2(double[] v1, double[] v2)
Compute the dot product of the 2D vectors v1
and
v2
.
v1
- the first vectorv2
- the second vectorv1
and v2
public static double dotAny(double[] v1, double[] v2)
Compute the dot product of two arbitrary-dimensional vectors
v1
and v2
.
If v1 and v2 are not the same length then the shorter length is used.
v1
- the first vectorv2
- the second vectorv1
and v2
public static double crossZ(double v1x, double v1y, double v2x, double v2y)
Compute the Z component of the cross product of the two 2D vectors
v1
and v2
.
v1x
- the first vector x coordv1y
- the first vector y coordv2x
- the second vector x coordv2y
- the second vector y coordv1
and
v2
public static double crossZ(double[] v1, double[] v2)
Compute the Z component of the cross product of the two 2D vectors
v1
and v2
.
v1
- the first vectorv2
- the second vectorv1
and
v2
public static double crossZ(double[] s1, double[] e1, double[] s2, double[] e2)
Compute the Z component of the cross product of the two 2D vectors
s1->e1
and s2->e2
.
s1
- the first vector start pointe1
- the first vector end points2
- the second vector start pointe2
- the second vector end points1->e1
and
s2->e2
public static double crossZ(double s1x, double s1y, double e1x, double e1y, double s2x, double s2y, double e2x, double e2y)
Compute the Z component of the cross product of the two 2D vectors
s1->e1
and s2->e2
.
s1x
- the first vector start point x coords1y
- the first vector start point y coorde1x
- the first vector end point x coorde1y
- the first vector end point y coords2x
- the second vector start point x coords2y
- the second vector start point y coorde2x
- the second vector end point x coorde2y
- the second vector end point y coords1->e1
and
s2->e2
public static double[] cross3(double[] ret, double[] v1, double[] v2)
Compute the cross product of two 3D vectors.
v1
- the first vector, not nullv2
- the second vector, not nullret
- the return vector, not null. On return ret = v1 x
v2
.ret
public static double crossMagnitudeSquared3(double[] v1, double[] v2)
Compute the square of the magnitude of the cross product of two 3D vectors.
v1
- the first vector, not nullv2
- the second vector, not nullv1 x v2
public static int crossZSign(double v1x, double v1y, double v2x, double v2y)
Compute the sign of the cross product of two 2D vectors.
v1x
- the first vector x coordv1y
- the first vector y coordv2x
- the second vector x coordv2y
- the second vector y coordv1 x v2
public static double[] normalizeAny(double[] v)
Normalize an arbitrary-dimensional vector.
v
- the vector to normalize, not null, normalized in-placepublic static double[] add2(double[] v1, double[] v2)
Compute the sum of two two-dimensional vectors.
v1
- the first addend, not null, sum is written here on returnv2
- the second addend, not nullv1
, which is overwritten with the sumpublic static double[] sub2(double[] v1, double[] v2)
Compute the difference of two two-dimensional vectors.
v1
- the minuend, not null, v1-v2
is written here on
returnv2
- the subtrahend, not nullv1
, which is overwritten with the
differencepublic static double[] dup2(double[] v)
Duplicate a two-dimensional vector.
v
- the vector to duplicate, not nullv
public static double[] copy2(double[] dest, double[] v)
Copy a two-dimensional vector.
dest
- the destination of the copy, not nullv
- the vector to copy, not nulldest
public static double[] scale2(double[] v, double s)
Scale a two-dimensional vector.
v
- the vector to scale, not null, scaled in-places
- the scale factorv
, as scaled by s
public static double project(double sx, double sy, double dx, double dy, double px, double py)
Project 2D point (px, py)
onto the segment starting at
(sx, sy)
and extending by the vector (dx,
dy)
.
sx
- the segment start point x coordinatesy
- the segment start point y coordinatedx
- the segment extent xdy
- the segment extent ypx
- the x coordinate of the point to projectpy
- the y coordinate of the point to project(px, py)
on the
segment given relative to the length of the segment. If the return value
is where then the projection point is (sx, sy) +
where*(dx, dy)
. If the segment length is 0.0 then the
return value is 0.0.public static double project(double sx, double sy, double sz, double dx, double dy, double dz, double px, double py, double pz)
Same as project(double, double, double, double, double,
double)
but in 3D.
public static float project(float sx, float sy, float sz, float dx, float dy, float dz, float px, float py, float pz)
public static float project3(float[] s, float[] d, float[] p)
public static float[] mutualPerp(float[] sa, float[] a, float[] sb, float[] b, float[] u, float parallel)
Find the mutual perpendicular between two line segments in 3D.
Let d be the sb-sa vector, and t be the vector from the intersection of the perpendicular on segment a to the intersection point on segment b. Then solve the following two-equation linear system for ua and ub: t*a=0, t*b=0.
Handles both the case that either or both segments are degenerate and the case that the segments are parallel.
sa
- the start coordinates of segment aa
- the extent of segment asb
- the start coordinates of segment bb
- the extent of segment bu
- if non-null then this must be of length at least two and the
return values are filled in here, null to consparallel
- this value is returned when the lines are parallelpublic static float[] mutualPerp(float[] sa, float[] a, float[] sb, float[] b, float[] u)
Covers mutualPerp(float[], float[], float[], float[], float[],
float)
, uses Float.NaN when parallel.
public static float intersect(float[] s, float[] d, float[] v0, float[] v1, float[] v2, boolean cullBackface, boolean cullFrontFace)
Intersect the 3D segment from s extending by vector d with the 3D triangle v0, v1, v2.
The triangle vertices are taken in CCW order when viewing the front face.
The implementation follows [Moller, Trumbore, 1997. Fast, Minimum Storage Ray/Triangle Intersection]. See also [Moller, Haines 2002. Real-Time Rendering].
s
- the segment start pointd
- the segment extentv0
- triangle vertex 0v1
- triangle vertex 1v2
- triangle vertex 2cullBackface
- whether to always fail intersect for back-facing
trianglescullFrontFace
- whether to always fail intersect for front-facing
trianglespublic static float intersect3(float[] where, float[] s, float[] d, float[] c, float r, boolean checkOnly)
3D segment intersect sphere.
Intersects the line through the segment starting at s and extending by d with the radius r circle centered at c. There are three possible cases: either there is no intrersection and the return where values are NaN; the line is tangent to the circle and both return where values are equal; or there are two intersections and the return where values are symmetric about the projection of the circle center onto the line.
The implementation follows that of intersect(double[], double,
double, double, double, double, double, double)
.
where
- if non-null, the intersection points will be written here on
return, as "where" values relative to the length of the segment. If there
was no intersection the where values will be NaN. Otherwise the
coordinates of the intersection points are given by s+where[i]*d.
Note that the intersections are computed between the circle and the
line through the segment, so it is possible that the return where
values are less than 0.0 or greater than 1.0. If you are only interested
in intersections within the segment then just disregard where values
outside this range.s
- the segment start pointd
- the segment extentc
- the circle centerr
- the circle radiuscheckOnly
- if set then the return where will be NaN if no
intersection or non-NaN, but otherwise unspecified, if intersectingpublic static double angle(double x, double y)
Return the CCW angle from the positive X axis to the vector (x,
y)
, in the range [0, 2*PI).
x
- the x coordinate of the vectory
- the y coordinate of the vector(x, y)
, in the range [0, 2*PI), unless (x,
y)
is closeToZero(double, double)
, in which case 0.0 is
returnedpublic static double angle(double[] v)
Return the CCW angle from the positive X axis to the 2D vector
v
.
v
- the vector, not nullv
,
unless v
is closeToZero2(double[])
, in
which case 0.0 is returnedpublic static double canonicalizeAngle(double t)
Return the canonical angle in the range (-PI, PI]
corresponding to any angle t
.
t
- the angle to canonicalize, in radianst
public static double canonicalizeAngle180(double t)
Same as canonicalizeAngle(double)
but operates on degrees.
public static double canonicalizeAngle360(double t)
Same as canonicalizeAngle180(double)
but returns an angle in the range
(0, 360].
public static boolean inAngleRange(double from, double to, double where)
Check whether angle where
is in the angle range
[from, to]
.
from
- the start angle, radians, need not be normalizedto
- the end angle, radians, need not be normalizedwhere
- the angle to check, radians, need not be normalizedwhere
is in the angle range [from,
to]
public static int findClosest(double theta, double[] angle)
Find the closest angle to theta
in an array of angles.
theta
- the angle to look for, radians, must be canonicalized (see
canonicalizeAngle(double)
)angle
- the array of angles in which to look, not null, must all be
canonicalized and must be sorted smallest to largesttheta
public static double[] intersect(double[] where, double sx, double sy, double dx, double dy, double cx, double cy, double r)
2D segment intersect circle.
Intersects the line through the segment starting at (sx,
sy)
and extending by the vector (dx, dy)
with the
radius r
circle centered at (cx, cy)
. There are
three possible cases: either there is no intrersection and the return
where
values are NaN
; the line is tangent to the
circle and both return where
values are equal; or there are
two intersections and the return where
values are symmetric
about the projection of the circle center onto the line.
where
- the intersection points will be written here on return, as
"where" values relative to the length of the segment. If there was no
intersection the where values will be NaN. Otherwise the coordinates of
the intersection points are given by (sx, sy) + where[i]*(dx,
dy)
. Note that the intersections are computed between the circle
and the line through the segment, so it is possible that the return
where values are less than 0.0 or greater than 1.0. If you are only
interested in intersections within the segment then just disregard where
values outside this range.sx
- the segment start point x coordinatesy
- the segment start point y coordinatedx
- the segment extent x coordinatedy
- the segment extent y coordinatecx
- circle center x coordinatecy
- circle center y coordinater
- the circle radiuswhere
public static double[] intersect2(double[] where, double[] s0, double[] d0, double extent0, double[] s1, double[] d1, double extent1)
2D segment intersection.
EPSILON
is used for degeneracy tests.
where
- the locations of the intersection on each segment, as
multiples of the respective d
vectors, are written here on
return. If the segments were parallel then Double.POSITIVE_INFINITY is
returned, unless they are coincident, in which case the location of the
first shared point (in either direction) is returned. Note that the
segments do not actually intersect unless the components of this return
vector are in the range [0.0, extent].s0
- the start point of the first segmentd0
- the direction of the first segment (need not be normalized)extent0
- the extent of the first segment in multiples of
d0
. Must be non-negative, may be Double.POSITIVE_INFINITY.s1
- the start point of the second segmentd1
- the direction of the second segment (need not be normalized)extent1
- the extent of the second segment in multiples of
d1
. Must be non-negative, may be Double.POSITIVE_INFINITY.where
public static double[] intersect2(double[] where, double[] s, double[] d, double extent, double[] corner, double[] diag)
2D segment intersect rectangle.
Intersects the segment s->extent*d
with the rectangle
from corner
to corner+diag
.
There are fivew possible cases:
where
values are NaN
where
values are 0.0 and extent
where
values are equalwhere
- the endpoints of the intersected segment are written here on
return, as "where" values relative to the magnitude of d
. If
there was no intersection the where values will be NaN. Otherwise the
coordinates of the intersection points are given by
(s[0]+d[0]*where[0], s[1]+d[1]*where[0]), (s[0]+d[0]*where[1],
s[1]+d[1]*where[1])
.s
- the segment start pointd
- the segment direction vector, need not be normalizedextent
- the segment extent in multiples of d
corner
- a corner of the rectanglediag
- the diagonal from corner
to the opposite cornerwhere
public static double[] interpolateAny(double[] result, double[] v1, double[] v2, double r)
Interpolate between two arbitrary-dimensional vectors.
If result
, v1
, and v2
are not
the same length then the shortest length is used.
result
- not null, on return result = (1-r)*v1 + r*v2
v1
- the first vectorv2
- the second vectorr
- the interpolation factorpublic static double[] reflectAny(double[] r, double[] i, double[] n)
Arbitrary-dimensional mirror reflection.
If r
, i
, and n
are not the same
length then the shortest length is used.
r
- the reflection direction is written here on return, not nulli
- the incident direction, not nulln
- the surface normal direction, not nullr
public static double[] rotY(double[] r, double[] v, double angle)
Rotate 3D vector v
CCW by angle
about the Y
axis, return in r
.
r
- the return vector, not nullv
- the vector to rotate, not nullangle
- the CCW angle to rotate v
about the Y axisr
public static double getPolyWidth(float[] x, float[] y, double[] angle, double theta)
Compute the parallel-jaws calipers width of a convex polygon.
x
- the polygon vertex x coords in CCW order, not nully
- the polygon vertex y coords in CCW order, not null, same length
as x
angle
- the canonical (see canonicalizeAngle(double)
) polygon vertex
angles in CCW order, same length as the coordinate vectors. If the first
element is NaN then the angles are computed and written here. If this
parameter is null then the angles are computed internally.theta
- the angle at which to compute the widththeta
public static double[] mmult44(double[] m, double[] mb, double[] ma)
public static float[] mmult44(float[] m, float[] mb, float[] ma)
public static double[] minvert44rht(double[] mi, double[] m)
public static float[] minvert44rht(float[] mi, float[] m)
public static double[] mscale44ht(double[] m, double s)
public static float[] mscale44ht(float[] m, float s)