Skip to content

Commit

Permalink
Fix triangle intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Jan 23, 2025
1 parent 082b1b4 commit d8907b9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,11 @@ private static Exception mapTriangle(final Pair<AffineModel2D, double[][]> ai,
final double dy = affineTransform2D.d(0).getDoublePosition(1);
final double[] source = new double[2];
for (int targetY = minY; targetY <= maxY; ++targetY) {
final Range xRange = triangle.intersect(targetY, minX, maxX);
/* TODO: Actually, "maxX" should be "maxX + 1" here, but we compensate for the additional "+1" below.
"maxX + 1" would be correct because intersect range upper bound is exclusive.
*/
final Range xRange = triangle.intersect(targetY, minX, maxX + 1);
source[0] = xRange.from();
source[1] = targetY;
affineTransform2D.apply(source, source);
lineMapper.map(source[0], source[1], dx, dy, xRange.from(), targetY,
xRange.length() + 1
/* TODO: This "+1" is to fix the tests and be compatible to the old code.
However, it leads to pixels on the edge of neighboring triangles to be drawn twice.
*/
);
lineMapper.map(source[0], source[1], dx, dy, xRange.from(), targetY, xRange.length());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int length() {
Range intersect(final double y, final int rangeMinX, final int rangeMaxX) {
final double[] bounds = new double[]{rangeMinX, rangeMaxX};
intersect(y, bounds);
return new Range((int) Math.ceil(bounds[0]), (int) Math.ceil(bounds[1]));
return new Range((int) Math.ceil(bounds[0]), Math.min(rangeMaxX, (int) Math.floor(bounds[1] + 1)));
}

void intersect(final double y, final double[] bounds) {
Expand Down

0 comments on commit d8907b9

Please sign in to comment.