java.lang.Object
eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.LineInterpolator

public class LineInterpolator extends Object
Interpolates the x coordinate along a 2D line edge for scanline-based polygon rasterization.

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

    Constructors
    Constructor
    Description
    Creates a new line interpolator with uninitialized endpoints.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    containsY(int y)
    Tests whether the given y coordinate falls within the vertical span of this edge.
    int
    getX(int y)
    Computes the interpolated x coordinate rounded to the nearest integer.
    void
    Sets the two endpoints of this edge and precomputes the width, height, and absolute height.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:
      true if y is 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

      public void setPoints(Point2D p1, Point2D p2)
      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 endpoint
      p2 - the second endpoint