shouldMergeArtifacts method
Determines if two artifacts should be merged based on their spatial relationship.
This method checks if the smaller artifact significantly overlaps with the larger one. If the smaller artifact has at least 80% overlap with the larger one, they are considered parts of the same character and should be merged.
Parameters:
artifact1: The first artifact to check.
artifact2: The second artifact to check.
Returns: true if the artifacts should be merged, false otherwise.
Implementation
bool shouldMergeArtifacts(
final Artifact artifact1,
final Artifact artifact2,
) {
// Calculate areas to determine which is smaller
final int area1 = artifact1.rectFound.width * artifact1.rectFound.height;
final int area2 = artifact2.rectFound.width * artifact2.rectFound.height;
// Identify smaller and larger artifacts
final Artifact smaller = area1 <= area2 ? artifact1 : artifact2;
final Artifact larger = area1 <= area2 ? artifact2 : artifact1;
// Calculate overlap area
final IntRect overlap = smaller.rectFound.intersect(larger.rectFound);
if (overlap.isEmpty) {
return false;
}
final int overlapArea = overlap.width * overlap.height;
final int smallerArea = smaller.rectFound.width * smaller.rectFound.height;
// If the smaller artifact overlaps with the larger one by at least 80%,
// they should be merged
return overlapArea >= (smallerArea * 0.8);
}