- // L1 distance (metropolitan distance)
- private static float distance(ResultPoint a, ResultPoint b) {
- return Math.abs(a.getX() - b.getX()) + Math.abs(a.getY() - b.getY());
- }
-
- /**
- * Gets the coordinate of an extreme black point of a segment
- *
- * @param a min value of the scanned coordinate
- * @param b max value of the scanned coordinate
- * @param fixed value of fixed coordinate
- * @param horizontal set to true if scan must be horizontal, false if vertical
- * @return {@link ResultPoint} describing the black point. If scan is horizontal,
- * the returned point is the first encountered if it is on the left of the image,
- * else the last one. If scan is vertical, the returned point is the first encountered
- * if it is on the top of the image, else the last one.
- * {@link ResultPoint} is null if not black point has been found
- */
- private ResultPoint getBlackPoint(int a, int b, int fixed, boolean horizontal) {
-
- ResultPoint last = null;
-
- if (horizontal) {
- for (int x = a; x < b; x++) {
- if (image.get(x, fixed)) {
- if (x < width / 2) {
- return new ResultPoint(x, fixed);
- } else {
- while (x < width && image.get(x, fixed)) {
- x++;
- }
- x--;
- last = new ResultPoint(x, fixed);
- }
- }
- }
- } else {
- for (int y = a; y < b; y++) {
- if (image.get(fixed, y)) {
- if (y < height / 2) {
- return new ResultPoint(fixed, y);
- } else {
- while (y < height && image.get(fixed, y)) {
- y++;
- }
- y--;
- last = new ResultPoint(fixed, y);
- }
- }
- }
- }
-
- return last;
- }
-
- /**
- * Same as getBlackPoint, but returned point is the last one found.
- *
- * @param a min value of the scanned coordinate
- * @param b max value of the scanned coordinate
- * @param fixed value of fixed coordinate
- * @param horizontal set to true if scan must be horizontal, false if vertical
- * @return {@link ResultPoint} describing the black point.
- */
- private ResultPoint getBlackPointInverted(int a, int b, int fixed, boolean horizontal) {
-
- if (horizontal) {
- for (int x = b + 1; x >= a; x--) {
- if (image.get(x, fixed)) {
- return new ResultPoint(x, fixed);
- }
- }
- } else {
- for (int y = b + 1; y >= a; y--) {
- if (image.get(fixed, y)) {
- return new ResultPoint(fixed, y);
- }
- }
- }
-
- return null;
- }
-