java.lang.Object
io.github.shimeoki.jfx.rasterization.DDATriangler
All Implemented Interfaces:
Triangler

public final class DDATriangler extends Object implements Triangler
A Triangler implementation using the digital differential analyzer algorithm for drawing slopes and drawing pixels one by one.

The algorithm separates the triangle into two (with flat side on max and/or min Y level), and then draws the slopes pixel by pixel with the triangle itself, using the scanline approach.

Uses floats for all calculations and converts the coordinates for rasterization to integers at the last stage - drawing horizontal lines. All floats at the convertion are floored (not rounded) for consistency.

Because of that, the triangles can be displaced to the start of the coordinate plane, and the gaps can be seen between the triangles, if this rasterization is used to draw triangulated polygons.

Sets pixels one by one with the PixelWriter.setColor(int, int, javafx.scene.paint.Color) call (the rasterization is not buffered). It's very slow, so this implementation is not recommended for fast rendering.

Algorithm documentation: Wikipedia.

The implementation is heavily based on this article.

Since:
2.0.0
  • Constructor Details

    • DDATriangler

      public DDATriangler(javafx.scene.canvas.GraphicsContext ctx)
      Creates a new DDATriangler instance.

      Anchores itself to the passed GraphicsContext: contains the reference to the PixelWriter from the context.

      It's not a singleton class, so instances should be created and used separately.

      Parameters:
      ctx - graphics context to use for the rasterization
      Since:
      2.0.0
      See Also:
      • GraphicsContext
  • Method Details

    • filler

      public Filler filler()
      Description copied from interface: Triangler
      Gets a Filler currently used by this triangler.
      Specified by:
      filler in interface Triangler
      Returns:
      reference to the currently used filler
      See Also:
    • setFiller

      public void setFiller(Filler f)
      Description copied from interface: Triangler
      Sets the Filler to use for the rasterization.
      Specified by:
      setFiller in interface Triangler
      Parameters:
      f - the filler to use
      See Also:
    • draw

      public void draw(Triangle t)
      Description copied from interface: Triangler
      Draws the Triangle with the currently used filler.
      Specified by:
      draw in interface Triangler
      Parameters:
      t - the Triangle to draw