JMU
Rasterization of 2D Shapes
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Discretization

Two Approaches

images/discretizations-2d.gif
Line Segments: Scanning
Line Segments: Scanning (cont.)
Line Segments: Discrete Differences
Line Segments: Discrete Differences (cont.)
Line Segments: Discrete Differences (cont.)
Line Segments: Discrete Differences (cont.)
Line Segments: Discrete Differences (cont.)

Conclusion - It seems like a good idea but it's hard to get everything to work out properly

Line Segments: Parametric Algorithm
Line Segments: Parametric Algorithm (cont.)
Line Segments: Parametric Algorithm (cont.)

Assuming the Slope is in [-1, 1]

  double   y;  
  int      xEnd, xStart;

  if (p_1 <= q_1) {
     xStart = round(p_1); 
     xEnd   = round(q_1); 
  } else {
     xStart = round(q_1); 
     xEnd   = round(p_1); 
  }
    
  for (int x=xStart; x<=xEnd; x++) {
     if (q_1 == p_1) { // Prevent a divide by zero
        y = p_2;
     } else {
        lambda = (x - p_1)/(q_1 - p_1);
        y = p_2 + lambda * (q_2 - p_2);  
     }
    
     // Set the color of pixel (x,round(y))
  }
  
Line Segments: Parametric Algorithm (cont.)
Line Segments: Parametric Algorithm (cont.)
Line Segments: Parametric Algorithm (cont.)
Line Segments: Other Algorithms
Line Segments: Antialiasing
Triangles: Getting Started
Triangles: Getting Started (cont.)
Triangles: Getting Started (cont.)

Points Inside the Triangle

images/triangle2d-inside.gif
Triangles: Getting Started (cont.)

Points Outside the Triangle

images/triangle2d-outside.gif
Triangles: Pointwise Fill
Triangles: Pointwise Fill (cont.)
Triangles: Pointwise Fill (cont.)

Reduce the Search Space Using the Bounding Rectangle

images/triangle2d-boundingrectangle.gif

Only test points in the bounding rectangle.

Triangles: Search-Based Scan Line Fill

Testing Fewer Points

images/triangle2d-improvedfill.gif

Don't test points that must be inside.

Triangles: Closed-Form Scan Line Fill
Convex Polygons: Getting Started

\(p\) and \(t\) are in the Same Halfspace formed by \(r\) and \(s\)

images/polygon-inside.gif
Convex Polygons: Pointwise Fill
Convex Polygons: Pointwise Fill (cont.)
Convex Polygons: Pointwise Fills (cont.)
Convex Polygons: Scan Line Fills