- float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
-
- // Now count other way -- don't run off image though of course
- int otherToX = fromX - (toX - fromX);
- if (otherToX < 0) {
- // "to" should the be the first value not included, so, the first value off
- // the edge is -1
- otherToX = -1;
- } else if (otherToX >= image.getWidth()) {
- otherToX = image.getWidth();
- }
- int otherToY = fromY - (toY - fromY);
- if (otherToY < 0) {
- otherToY = -1;
- } else if (otherToY >= image.getHeight()) {
- otherToY = image.getHeight();
- }
- result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
- return result - 1.0f; // -1 because we counted the middle pixel twice
- }
+ float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
+
+ // Now count other way -- don't run off image though of course
+ float scale = 1.0f;
+ int otherToX = fromX - (toX - fromX);
+ if (otherToX < 0) {
+ scale = (float) fromX / (float) (fromX - otherToX);
+ otherToX = 0;
+ } else if (otherToX > image.getWidth()) {
+ scale = (float) (image.getWidth() - fromX) / (float) (otherToX - fromX);
+ otherToX = image.getWidth();
+ }
+ int otherToY = (int) (fromY - (toY - fromY) * scale);
+
+ scale = 1.0f;
+ if (otherToY < 0) {
+ scale = (float) fromY / (float) (fromY - otherToY);
+ otherToY = 0;
+ } else if (otherToY > image.getHeight()) {
+ scale = (float) (image.getHeight() - fromY) / (float) (otherToY - fromY);
+ otherToY = image.getHeight();
+ }
+ otherToX = (int) (fromX + (otherToX - fromX) * scale);
+
+ result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
+ return result;
+ }