public class SolidPolygon extends AbstractCoordinateShape
A solid-color convex polygon renderer supporting N vertices (N >= 3).

This class serves as the unified polygon type for both rendering and CSG operations. It renders convex polygons by decomposing them into triangles using fan triangulation, and supports CSG operations directly without conversion to intermediate types.

Rendering:

  • Fan triangulation for N-vertex polygons (N-2 triangles)
  • Scanline rasterization with alpha blending
  • Backface culling and flat shading support
  • Mouse interaction via point-in-polygon testing

CSG Support:

  • Lazy-computed plane for BSP operations
  • flip() for inverting polygon orientation
  • deepClone() for creating independent copies

Usage examples:


 // Create a triangle
 SolidPolygon triangle = new SolidPolygon(
     new Point3D(0, 0, 0),
     new Point3D(50, 0, 0),
     new Point3D(25, 50, 0),
     Color.RED
 );

 // Create a quad
 SolidPolygon quad = SolidPolygon.quad(
     new Point3D(-50, -50, 0),
     new Point3D(50, -50, 0),
     new Point3D(50, 50, 0),
     new Point3D(-50, 50, 0),
     Color.BLUE
 );

 // Use with CSG (via AbstractCompositeShape)
 SolidPolygonRectangularBox box = new SolidPolygonRectangularBox(...);
 box.subtract(sphere);
 
See Also:
  • Constructor Details

    • SolidPolygon

      public SolidPolygon(Point3D[] vertices, Color color)
      Creates a solid polygon with the specified vertices and color.
      Parameters:
      vertices - the vertices defining the polygon (must have at least 3)
      color - the fill color of the polygon
      Throws:
      IllegalArgumentException - if vertices is null or has fewer than 3 vertices
    • SolidPolygon

      public SolidPolygon(List<Point3D> points, Color color)
      Creates a solid polygon from a list of points and color.
      Parameters:
      points - the list of points defining the polygon (must have at least 3)
      color - the fill color of the polygon
      Throws:
      IllegalArgumentException - if points is null or has fewer than 3 points
    • SolidPolygon

      public SolidPolygon(Point3D point1, Point3D point2, Point3D point3, Color color)
      Creates a solid triangle with the specified vertices and color.
      Parameters:
      point1 - the first vertex position
      point2 - the second vertex position
      point3 - the third vertex position
      color - the fill color
  • Method Details

    • fromVertices

      public static SolidPolygon fromVertices(List<Vertex> vertices, Color color)
      Creates a solid polygon from existing vertices.

      Used for CSG operations and cloning where vertices already exist. The vertex list is used directly (not copied), so callers should not modify the list after passing it to this method.

      Parameters:
      vertices - the list of Vertex objects (used directly, not copied)
      color - the fill color of the polygon
      Returns:
      a new SolidPolygon with the given vertices (shading disabled by default)
      Throws:
      IllegalArgumentException - if vertices is null or has fewer than 3 vertices
    • fromVertices

      public static SolidPolygon fromVertices(List<Vertex> vertices, Color color, boolean shadingEnabled)
      Creates a solid polygon from existing vertices with specified shading.

      Used for CSG operations and cloning where vertices already exist. The vertex list is used directly (not copied), so callers should not modify the list after passing it to this method.

      Parameters:
      vertices - the list of Vertex objects (used directly, not copied)
      color - the fill color of the polygon
      shadingEnabled - whether shading is enabled for this polygon
      Returns:
      a new SolidPolygon with the given vertices and shading setting
      Throws:
      IllegalArgumentException - if vertices is null or has fewer than 3 vertices
    • triangle

      public static SolidPolygon triangle(Point3D p1, Point3D p2, Point3D p3, Color color)
      Creates a triangle (3-vertex polygon).
      Parameters:
      p1 - the first vertex
      p2 - the second vertex
      p3 - the third vertex
      color - the fill color
      Returns:
      a new SolidPolygon with 3 vertices
    • quad

      public static SolidPolygon quad(Point3D p1, Point3D p2, Point3D p3, Point3D p4, Color color)
      Creates a quad (4-vertex polygon).
      Parameters:
      p1 - the first vertex
      p2 - the second vertex
      p3 - the third vertex
      p4 - the fourth vertex
      color - the fill color
      Returns:
      a new SolidPolygon with 4 vertices
    • drawTriangle

      public static void drawTriangle(RenderingContext context, Point2D onScreenPoint1, Point2D onScreenPoint2, Point2D onScreenPoint3, MouseInteractionController mouseInteractionController, Color color)
      Renders a triangle using scanline rasterization.

      This static method handles:

      • Rounding vertices to integer screen coordinates
      • Mouse hover detection via point-in-triangle test
      • Viewport clipping
      • Scanline rasterization with alpha blending
      Parameters:
      context - the rendering context
      onScreenPoint1 - the first vertex in screen coordinates
      onScreenPoint2 - the second vertex in screen coordinates
      onScreenPoint3 - the third vertex in screen coordinates
      mouseInteractionController - optional controller for mouse events, or null
      color - the fill color
    • getVertexCount

      public int getVertexCount()
      Returns the number of vertices in this polygon.
      Returns:
      the vertex count
    • getColor

      public Color getColor()
      Returns the fill color of this polygon.
      Returns:
      the polygon color
    • setColor

      public void setColor(Color color)
      Sets the fill color of this polygon.
      Parameters:
      color - the new color
    • isShadingEnabled

      public boolean isShadingEnabled()
      Checks if shading is enabled for this polygon.
      Returns:
      true if shading is enabled, false otherwise
    • setShadingEnabled

      public void setShadingEnabled(boolean shadingEnabled)
      Enables or disables shading for this polygon.
      Parameters:
      shadingEnabled - true to enable shading, false to disable
    • isBackfaceCullingEnabled

      public boolean isBackfaceCullingEnabled()
      Checks if backface culling is enabled for this polygon.
      Returns:
      true if backface culling is enabled
    • setBackfaceCulling

      public void setBackfaceCulling(boolean backfaceCulling)
      Enables or disables backface culling for this polygon.
      Parameters:
      backfaceCulling - true to enable backface culling
    • getPlane

      public Plane getPlane()
      Returns the plane containing this polygon.

      Computed from the first three vertices and cached for reuse. Used by BSP tree construction for spatial partitioning.

      Returns:
      the Plane containing this polygon
    • flip

      public void flip()
      Flips the orientation of this polygon.

      Reverses the vertex order and negates vertex normals. Also flips the cached plane if computed. Used during CSG operations when inverting solids.

    • deepClone

      public SolidPolygon deepClone()
      Creates a deep clone of this polygon.

      Clones all vertices and preserves the color, shading, and backface culling settings. Used by CSG operations to create independent copies before modification.

      Returns:
      a new SolidPolygon with cloned data and preserved settings
    • paint

      public void paint(RenderingContext renderBuffer)
      Renders this polygon to the screen.
      Specified by:
      paint in class AbstractCoordinateShape
      Parameters:
      renderBuffer - the rendering context containing the pixel buffer
    • transform

      public void transform(TransformStack transforms, RenderAggregator aggregator, RenderingContext renderingContext)
      Transforms vertices to screen space and computes lighting once per frame.

      Overrides parent to add lighting computation during the single-threaded transform phase. This ensures lighting is calculated only once per polygon per frame, rather than once per render thread.

      Overrides:
      transform in class AbstractCoordinateShape
      Parameters:
      transforms - the transform stack to apply
      aggregator - the render aggregator to queue shapes into
      renderingContext - the rendering context