Rasterization of 2D Shapes
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Two Approaches
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)) }
double deltay, y; int xEnd, xStart; if (p_1 <= q_1) { xStart = round(p_1); xEnd = round(q_1); y = p_2; if (q_1 == p_1) deltay = 0; else deltay = (q_2 - p_2)/(q_1 - p_1); } else { xStart = round(q_1); xEnd = round(p_1); y = q_2; if (p_1 == q_1) deltay = 0; else deltay = (p_2 - q_2)/(p_1 - q_1); } for (int x=xStart; x<=xEnd; x++) { // Set the color of pixel (x,round(y)) y += deltay; }
Points Inside the Triangle
Points Outside the Triangle
Reduce the Search Space Using the Bounding Rectangle
Only test points in the bounding rectangle.
Testing Fewer Points
Don't test points that must be inside.
\(p\) and \(t\) are in the Same Halfspace formed by \(r\) and \(s\)