Use FloatMath for our float math.

This commit is contained in:
Michael Bayne
2011-07-05 09:06:32 -07:00
parent b1c9545f04
commit 339ea761d1
9 changed files with 45 additions and 45 deletions
+17 -17
View File
@@ -19,9 +19,9 @@ public abstract class AbstractArc extends RectangularShape implements IArc
@Override // from interface IArc @Override // from interface IArc
public Point getStartPoint (Point target) { public Point getStartPoint (Point target) {
float a = (float)Math.toRadians(getAngleStart()); float a = FloatMath.toRadians(getAngleStart());
target.setLocation(getX() + (1f + (float)Math.cos(a)) * getWidth() / 2f, target.setLocation(getX() + (1f + FloatMath.cos(a)) * getWidth() / 2f,
getY() + (1f - (float)Math.sin(a)) * getHeight() / 2f); getY() + (1f - FloatMath.sin(a)) * getHeight() / 2f);
return target; return target;
} }
@@ -32,9 +32,9 @@ public abstract class AbstractArc extends RectangularShape implements IArc
@Override // from interface IArc @Override // from interface IArc
public Point getEndPoint (Point target) { public Point getEndPoint (Point target) {
float a = (float)Math.toRadians(getAngleStart() + getAngleExtent()); float a = FloatMath.toRadians(getAngleStart() + getAngleExtent());
target.setLocation(getX() + (1f + (float)Math.cos(a)) * getWidth() / 2f, target.setLocation(getX() + (1f + FloatMath.cos(a)) * getWidth() / 2f,
getY() + (1f - (float)Math.sin(a)) * getHeight() / 2f); getY() + (1f - FloatMath.sin(a)) * getHeight() / 2f);
return target; return target;
} }
@@ -82,7 +82,7 @@ public abstract class AbstractArc extends RectangularShape implements IArc
return true; return true;
} }
boolean containsAngle = containsAngle((float)Math.toDegrees(-Math.atan2(ny, nx))); boolean containsAngle = containsAngle(FloatMath.toDegrees(-FloatMath.atan2(ny, nx)));
if (getArcType() == PIE) { if (getArcType() == PIE) {
return containsAngle; return containsAngle;
} }
@@ -195,7 +195,7 @@ public abstract class AbstractArc extends RectangularShape implements IArc
/** Returns a normalized angle (bound between 0 and 360 degrees). */ /** Returns a normalized angle (bound between 0 and 360 degrees). */
protected float getNormAngle (float angle) { protected float getNormAngle (float angle) {
return angle - (float)Math.floor(angle / 360f) * 360f; return angle - FloatMath.floor(angle / 360f) * 360f;
} }
/** An iterator over an {@link IArc}. */ /** An iterator over an {@link IArc}. */
@@ -264,7 +264,7 @@ public abstract class AbstractArc extends RectangularShape implements IArc
this.height = a.getHeight() / 2f; this.height = a.getHeight() / 2f;
this.x = a.getX() + width; this.x = a.getX() + width;
this.y = a.getY() + height; this.y = a.getY() + height;
this.angle = -(float)Math.toRadians(a.getAngleStart()); this.angle = -FloatMath.toRadians(a.getAngleStart());
this.extent = -a.getAngleExtent(); this.extent = -a.getAngleExtent();
this.type = a.getArcType(); this.type = a.getArcType();
this.t = t; this.t = t;
@@ -278,16 +278,16 @@ public abstract class AbstractArc extends RectangularShape implements IArc
if (Math.abs(extent) >= 360f) { if (Math.abs(extent) >= 360f) {
arcCount = 4; arcCount = 4;
k = 4f / 3f * ((float)Math.sqrt(2f) - 1f); k = 4f / 3f * (FloatMath.sqrt(2f) - 1f);
step = (float)Math.PI / 2f; step = FloatMath.PI / 2f;
if (extent < 0f) { if (extent < 0f) {
step = -step; step = -step;
k = -k; k = -k;
} }
} else { } else {
arcCount = (int)Math.rint(Math.abs(extent) / 90f); arcCount = (int)Math.rint(Math.abs(extent) / 90f);
step = (float)Math.toRadians(extent / arcCount); step = FloatMath.toRadians(extent / arcCount);
k = 4f / 3f * (1f - (float)Math.cos(step / 2f)) / (float)Math.sin(step / 2f); k = 4f / 3f * (1f - FloatMath.cos(step / 2f)) / FloatMath.sin(step / 2f);
} }
lineCount = 0; lineCount = 0;
@@ -319,8 +319,8 @@ public abstract class AbstractArc extends RectangularShape implements IArc
if (index == 0) { if (index == 0) {
type = SEG_MOVETO; type = SEG_MOVETO;
count = 1; count = 1;
cos = (float)Math.cos(angle); cos = FloatMath.cos(angle);
sin = (float)Math.sin(angle); sin = FloatMath.sin(angle);
kx = k * width * sin; kx = k * width * sin;
ky = k * height * cos; ky = k * height * cos;
coords[0] = mx = x + cos * width; coords[0] = mx = x + cos * width;
@@ -331,8 +331,8 @@ public abstract class AbstractArc extends RectangularShape implements IArc
coords[0] = mx - kx; coords[0] = mx - kx;
coords[1] = my + ky; coords[1] = my + ky;
angle += step; angle += step;
cos = (float)Math.cos(angle); cos = FloatMath.cos(angle);
sin = (float)Math.sin(angle); sin = FloatMath.sin(angle);
kx = k * width * sin; kx = k * width * sin;
ky = k * height * cos; ky = k * height * cos;
coords[4] = mx = x + cos * width; coords[4] = mx = x + cos * width;
@@ -117,7 +117,7 @@ public abstract class AbstractEllipse extends RectangularShape implements IEllip
// the arc. // the arc.
/** The coefficient to calculate control points of Bezier curves. */ /** The coefficient to calculate control points of Bezier curves. */
private static final float U = 2f / 3f * ((float)Math.sqrt(2) - 1f); private static final float U = 2f / 3f * (FloatMath.sqrt(2) - 1f);
/** The points coordinates calculation table. */ /** The points coordinates calculation table. */
private static final float[][] POINTS = { private static final float[][] POINTS = {
@@ -141,7 +141,7 @@ public abstract class AbstractRoundRectangle extends RectangularShape implements
}; };
/** The coefficient to calculate control points of Bezier curves. */ /** The coefficient to calculate control points of Bezier curves. */
protected static final float U = 0.5f - 2f / 3f * ((float)Math.sqrt(2f) - 1f); protected static final float U = 0.5f - 2f / 3f * (FloatMath.sqrt(2f) - 1f);
/** The points coordinates calculation table. */ /** The points coordinates calculation table. */
protected static final float[][] POINTS = { protected static final float[][] POINTS = {
@@ -313,8 +313,8 @@ public class AffineTransform implements Cloneable, Serializable
* @param angle the angle of rotation (in radians). * @param angle the angle of rotation (in radians).
*/ */
public void setToRotation (float angle) { public void setToRotation (float angle) {
float sin = (float)Math.sin(angle); float sin = FloatMath.sin(angle);
float cos = (float)Math.cos(angle); float cos = FloatMath.cos(angle);
if (Math.abs(cos) < ZERO) { if (Math.abs(cos) < ZERO) {
cos = 0; cos = 0;
sin = sin > 0 ? 1f : -1f; sin = sin > 0 ? 1f : -1f;
+12 -12
View File
@@ -174,16 +174,16 @@ public class Arc extends AbstractArc implements Serializable
*/ */
public void setArcByTangent (IPoint p1, IPoint p2, IPoint p3, float radius) { public void setArcByTangent (IPoint p1, IPoint p2, IPoint p3, float radius) {
// use simple geometric calculations of arc center, radius and angles by tangents // use simple geometric calculations of arc center, radius and angles by tangents
float a1 = -(float)Math.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX()); float a1 = -FloatMath.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX());
float a2 = -(float)Math.atan2(p3.getY() - p2.getY(), p3.getX() - p2.getX()); float a2 = -FloatMath.atan2(p3.getY() - p2.getY(), p3.getX() - p2.getX());
float am = (a1 + a2) / 2f; float am = (a1 + a2) / 2f;
float ah = a1 - am; float ah = a1 - am;
float d = radius / Math.abs((float)Math.sin(ah)); float d = radius / Math.abs(FloatMath.sin(ah));
float x = p2.getX() + d * (float)Math.cos(am); float x = p2.getX() + d * FloatMath.cos(am);
float y = p2.getY() - d * (float)Math.sin(am); float y = p2.getY() - d * FloatMath.sin(am);
ah = ah >= 0f ? (float)Math.PI * 1.5f - ah : (float)Math.PI * 0.5f - ah; ah = ah >= 0f ? FloatMath.PI * 1.5f - ah : FloatMath.PI * 0.5f - ah;
a1 = getNormAngle((float)Math.toDegrees(am - ah)); a1 = getNormAngle(FloatMath.toDegrees(am - ah));
a2 = getNormAngle((float)Math.toDegrees(am + ah)); a2 = getNormAngle(FloatMath.toDegrees(am + ah));
float delta = a2 - a1; float delta = a2 - a1;
if (delta <= 0f) { if (delta <= 0f) {
delta += 360f; delta += 360f;
@@ -196,8 +196,8 @@ public class Arc extends AbstractArc implements Serializable
* the center of this arc. * the center of this arc.
*/ */
public void setAngleStart (IPoint point) { public void setAngleStart (IPoint point) {
float angle = (float)Math.atan2(point.getY() - getCenterY(), point.getX() - getCenterX()); float angle = FloatMath.atan2(point.getY() - getCenterY(), point.getX() - getCenterX());
setAngleStart(getNormAngle(-(float)Math.toDegrees(angle))); setAngleStart(getNormAngle(-FloatMath.toDegrees(angle)));
} }
/** /**
@@ -210,8 +210,8 @@ public class Arc extends AbstractArc implements Serializable
public void setAngles (float x1, float y1, float x2, float y2) { public void setAngles (float x1, float y1, float x2, float y2) {
float cx = getCenterX(); float cx = getCenterX();
float cy = getCenterY(); float cy = getCenterY();
float a1 = getNormAngle(-(float)Math.toDegrees(Math.atan2(y1 - cy, x1 - cx))); float a1 = getNormAngle(-FloatMath.toDegrees(FloatMath.atan2(y1 - cy, x1 - cx)));
float a2 = getNormAngle(-(float)Math.toDegrees(Math.atan2(y2 - cy, x2 - cx))); float a2 = getNormAngle(-FloatMath.toDegrees(FloatMath.atan2(y2 - cy, x2 - cx)));
a2 -= a1; a2 -= a1;
if (a2 <= 0f) { if (a2 <= 0f) {
a2 += 360f; a2 += 360f;
+8 -8
View File
@@ -38,7 +38,7 @@ class Crossing
if (d < 0f) { if (d < 0f) {
return 0; return 0;
} }
d = (float)Math.sqrt(d); d = FloatMath.sqrt(d);
res[rc++] = (-b + d) / (a * 2f); res[rc++] = (-b + d) / (a * 2f);
// d != 0f // d != 0f
if (d != 0f) { if (d != 0f) {
@@ -72,15 +72,15 @@ class Crossing
float n = -a / 3f; float n = -a / 3f;
if (R2 < Q3) { if (R2 < Q3) {
float t = (float)Math.acos(R / Math.sqrt(Q3)) / 3f; float t = FloatMath.acos(R / FloatMath.sqrt(Q3)) / 3f;
float p = 2f * (float)Math.PI / 3f; float p = 2f * FloatMath.PI / 3f;
float m = -2f * (float)Math.sqrt(Q); float m = -2f * FloatMath.sqrt(Q);
res[rc++] = m * (float)Math.cos(t) + n; res[rc++] = m * FloatMath.cos(t) + n;
res[rc++] = m * (float)Math.cos(t + p) + n; res[rc++] = m * FloatMath.cos(t + p) + n;
res[rc++] = m * (float)Math.cos(t - p) + n; res[rc++] = m * FloatMath.cos(t - p) + n;
} else { } else {
// Debug.println("R2 >= Q3 (" + R2 + "/" + Q3 + ")"); // Debug.println("R2 >= Q3 (" + R2 + "/" + Q3 + ")");
float A = (float)Math.pow(Math.abs(R) + Math.sqrt(R2 - Q3), 1f / 3f); float A = FloatMath.pow(Math.abs(R) + FloatMath.sqrt(R2 - Q3), 1f / 3f);
if (R > 0f) { if (R > 0f) {
A = -A; A = -A;
} }
+1 -1
View File
@@ -23,7 +23,7 @@ public class CubicCurves
public static float getFlatness (float x1, float y1, float ctrlx1, float ctrly1, public static float getFlatness (float x1, float y1, float ctrlx1, float ctrly1,
float ctrlx2, float ctrly2, float x2, float y2) { float ctrlx2, float ctrly2, float x2, float y2) {
return (float)Math.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2)); return FloatMath.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2));
} }
public static float getFlatness (float[] coords, int offset) { public static float getFlatness (float[] coords, int offset) {
+1 -1
View File
@@ -9,7 +9,7 @@ package pythagoras.f;
*/ */
public class GeometryUtil public class GeometryUtil
{ {
public static final float EPSILON = (float)Math.pow(10, -14); public static final float EPSILON = FloatMath.pow(10, -14);
public static int intersectLinesWithParams (float x1, float y1, float x2, float y2, public static int intersectLinesWithParams (float x1, float y1, float x2, float y2,
float x3, float y3, float x4, float y4, float x3, float y3, float x4, float y4,
+2 -2
View File
@@ -81,7 +81,7 @@ public class Lines
* Returns the distance from the specified point to the specified line. * Returns the distance from the specified point to the specified line.
*/ */
public static float pointLineDist (float px, float py, float x1, float y1, float x2, float y2) { public static float pointLineDist (float px, float py, float x1, float y1, float x2, float y2) {
return (float)Math.sqrt(pointLineDistSq(px, py, x1, y1, x2, y2)); return FloatMath.sqrt(pointLineDistSq(px, py, x1, y1, x2, y2));
} }
/** /**
@@ -119,7 +119,7 @@ public class Lines
* Returns the distance between the specified point and the specified line segment. * Returns the distance between the specified point and the specified line segment.
*/ */
public static float pointSegDist (float px, float py, float x1, float y1, float x2, float y2) { public static float pointSegDist (float px, float py, float x1, float y1, float x2, float y2) {
return (float)Math.sqrt(pointSegDistSq(px, py, x1, y1, x2, y2)); return FloatMath.sqrt(pointSegDistSq(px, py, x1, y1, x2, y2));
} }
/** /**