Replace Math.round() with simple workalike -- faster, and needed to work in JavaME

git-svn-id: https://zxing.googlecode.com/svn/trunk@1585 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-09-13 09:05:15 +00:00
parent 4c01e8897d
commit 05582c4975
2 changed files with 19 additions and 4 deletions

View file

@ -206,6 +206,13 @@ public final class WhiteRectangleDetector {
}
}
/**
* Ends up being a bit faster than Math.round(). This merely rounds its
* argument to the nearest int, where x.5 rounds up.
*/
private static int round(float d) {
return (int) (d + 0.5f);
}
private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
int dist = distanceL2(aX, aY, bX, bY);
@ -213,8 +220,8 @@ public final class WhiteRectangleDetector {
float yStep = (bY - aY) / dist;
for (int i = 0; i < dist; i++) {
int x = Math.round(aX + i * xStep);
int y = Math.round(aY + i * yStep);
int x = round(aX + i * xStep);
int y = round(aY + i * yStep);
if (image.get(x, y)) {
return new ResultPoint(x, y);
}
@ -225,7 +232,7 @@ public final class WhiteRectangleDetector {
private static int distanceL2(float aX, float aY, float bX, float bY) {
float xDiff = aX - bX;
float yDiff = aY - bY;
return (int) Math.round(Math.sqrt(xDiff * xDiff + yDiff * yDiff));
return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff));
}
/**

View file

@ -214,9 +214,17 @@ public final class Detector {
return (p.getX() >= 0 && p.getX() < image.width && p.getY() > 0 && p.getY() < image.height);
}
/**
* Ends up being a bit faster than Math.round(). This merely rounds its
* argument to the nearest int, where x.5 rounds up.
*/
private static int round(float d) {
return (int) (d + 0.5f);
}
// L2 distance
private static int distance(ResultPoint a, ResultPoint b) {
return (int) Math.round(Math.sqrt((a.getX() - b.getX())
return round((float) Math.sqrt((a.getX() - b.getX())
* (a.getX() - b.getX()) + (a.getY() - b.getY())
* (a.getY() - b.getY())));
}