scorePattern function
Implementation
double scorePattern(
Position<double> point, List<double> ratios, BitMatrix matrix) {
try {
final horizontalRun = countBlackWhiteRun(
origin: point,
end: Position<double>(-1, point.y),
matrix: matrix,
length: ratios.length,
);
final verticalRun = countBlackWhiteRun(
origin: point,
end: Position<double>(point.x, -1),
matrix: matrix,
length: ratios.length,
);
final topLeftPoint = Position<double>(
max(0, point.x - point.y) - 1,
max(0, point.y - point.x) - 1,
);
final topLeftBottomRightRun = countBlackWhiteRun(
origin: point,
end: topLeftPoint,
matrix: matrix,
length: ratios.length,
);
final bottomLeftPoint = Position<double>(
min(matrix.width, point.x + point.y) + 1,
min(matrix.height, point.y + point.x) + 1,
);
final bottomLeftTopRightRun = countBlackWhiteRun(
origin: point,
end: bottomLeftPoint,
matrix: matrix,
length: ratios.length,
);
final horzError = _scoreBlackWhiteRun(horizontalRun, ratios);
final vertError = _scoreBlackWhiteRun(verticalRun, ratios);
final diagDownError = _scoreBlackWhiteRun(topLeftBottomRightRun, ratios);
final diagUpError = _scoreBlackWhiteRun(bottomLeftTopRightRun, ratios);
final ratioError = sqrt(horzError.error * horzError.error +
vertError.error * vertError.error +
diagDownError.error * diagDownError.error +
diagUpError.error * diagUpError.error);
final avgSize = (horzError.averageSize +
vertError.averageSize +
diagDownError.averageSize +
diagUpError.averageSize) /
4;
final sizeError = (pow(horzError.averageSize - avgSize, 2) +
pow(vertError.averageSize - avgSize, 2) +
pow(diagDownError.averageSize - avgSize, 2) +
pow(diagUpError.averageSize - avgSize, 2)) /
avgSize;
return ratioError + sizeError;
} catch (e) {
return double.infinity;
}
}