mergeArtifact method
Merges the current artifact with another artifact.
This method combines the current artifact with the provided artifact, creating a new, larger artifact that encompasses both.
Parameters:
toMerge
: The Artifact to be merged with the current artifact.
The merging process involves:
- Creating a new rectangle that encompasses both artifacts.
- Creating a new matrix (grid) large enough to contain both artifacts' data.
- Copying the data from both artifacts into the new matrix.
- Updating the current artifact's matrix and rectangle to reflect the merged state.
Note: This method modifies the current artifact in-place.
Implementation
void mergeArtifact(final Artifact toMerge) {
// Create a new rectangle that encompasses both artifacts
final Rect newRect = Rect.fromLTRB(
min(
this._matrix.rectangle.left,
toMerge._matrix.rectangle.left,
),
min(
this._matrix.rectangle.top,
toMerge._matrix.rectangle.top,
),
max(
this._matrix.rectangle.right,
toMerge._matrix.rectangle.right,
),
max(
this._matrix.rectangle.bottom,
toMerge._matrix.rectangle.bottom,
),
);
// Merge the grids
final Matrix newGrid = Matrix(newRect.width, newRect.height);
// Copy both grids onto the new grid
Matrix.copyGrid(
this.matrix,
newGrid,
(this._matrix.rectangle.left - newRect.left).toInt(),
(this._matrix.rectangle.top - newRect.top).toInt(),
);
Matrix.copyGrid(
toMerge.matrix,
newGrid,
(toMerge._matrix.rectangle.left - newRect.left).toInt(),
(toMerge._matrix.rectangle.top - newRect.top).toInt(),
);
this.matrix.setGrid(newGrid.data);
this.matrix.rectangle =
this.matrix.rectangle.expandToInclude(toMerge.matrix.rectangle);
}