Class LineInterpolator
LineInterpolator represents one edge of a polygon in screen space, defined by
two Point2D endpoints. Given a scanline y coordinate, it computes the corresponding
x coordinate via linear interpolation. This is a core building block for the solid polygon
rasterizer, which fills triangles by sweeping horizontal scanlines and using two
LineInterpolator instances to find the left and right x boundaries at each y level.
Subpixel precision: This class uses double-precision arithmetic throughout the interpolation pipeline to eliminate T-junction gaps. Vertices that should be at the same position but land at slightly different screen coordinates (e.g., 100.4 vs 100.6) will produce consistent interpolated results when rounded, ensuring adjacent polygons fill seamlessly without gaps.
Instances are Comparable, sorted by absolute height (tallest first) and then
by width. This ordering is used during rasterization to select the primary (longest) edge
of the triangle for the outer scanline loop.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new line interpolator with uninitialized endpoints. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsY(int y) Tests whether the given y coordinate falls within the vertical span of this edge.intgetX(int y) Computes the interpolated x coordinate rounded to the nearest integer.voidSets the two endpoints of this edge and precomputes the width, height, and absolute height.
-
Constructor Details
-
LineInterpolator
public LineInterpolator()Creates a new line interpolator with uninitialized endpoints.
-
-
Method Details
-
containsY
public boolean containsY(int y) Tests whether the given y coordinate falls within the vertical span of this edge.Uses double-precision comparison to handle subpixel vertex positions correctly.
- Parameters:
y- the scanline y coordinate to test- Returns:
trueifyis between the y coordinates of the two endpoints (inclusive)
-
getX
public int getX(int y) Computes the interpolated x coordinate rounded to the nearest integer.For horizontal edges (height near zero), returns the midpoint x value to avoid division by zero. This case should only occur when the edge spans exactly one scanline.
- Parameters:
y- the scanline y coordinate- Returns:
- the interpolated x coordinate rounded to the nearest integer
-
setPoints
Sets the two endpoints of this edge and precomputes the width, height, and absolute height.This method stores the endpoints directly and computes spans using double-precision arithmetic from the Point2D coordinates.
- Parameters:
p1- the first endpointp2- the second endpoint
-