Master the fundamental concepts of software rasterizer through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksGiven triangle vertices A, B, and C, any point P in the plane can be written as:
cLoading…
The triple (u, v, w) is the barycentric coordinate of P relative to the triangle. Geometric meaning:
Barycentric coordinates come from edge functions, a 2D cross product:
cLoading…
For a counter-clockwise triangle ABC:
u = edge(P, B, C) / edge(A, B, C)v = edge(P, C, A) / edge(A, B, C)w = edge(P, A, B) / edge(A, B, C)For example, if all three numerators are non-negative for pixel center P, that pixel is inside the triangle. The denominator is twice the signed triangle area. GPUs compute these values in fixed-function rasterizer hardware for attribute interpolation.
You will compute barycentric coordinates for test points and determine whether each lies inside a given triangle. This task asks you to implement edge functions and print inside/outside results. The same math underlies Pixar's REYES renderer and every ray-triangle intersection test in path tracers like PBRT.
Barycentric coordinates say where a point sits relative to a triangle's corners: P = w0·A + w1·B + w2·C with w0 + w1 + w2 = 1. Rasterizers get them almost for free from the edge functions, and use them for both the inside test and attribute interpolation. Compute them exactly, as reduced fractions. Classify points as inside, outside (naming the edges they are beyond), on an edge, or at a vertex, and interpolate a per-vertex attribute.
# starts a comment line.
cLoading…
E(P, Q, R) = (Qx-Px)(Ry-Py) - (Qy-Py)(Rx-Px). Twice the signed area is E(A, B, C).w0 = E(B, C, P), w1 = E(C, A, P), w2 = E(A, B, P), and the barycentrics are wᵢ / area.outside (beyond BC CA AB), listing the negative ones in that order (w0 ↔ BC, w1 ↔ CA, w2 ↔ AB).at vertex A|B|C (the non-zero one), one zero means on edge BC|CA|AB, and none means inside.(w0·VA + w1·VB + w2·VC) / area, printed as a reduced fraction and as a decimal with 4 places, rounded half away from zero.cLoading…
positive winding or negative winding.tri: AX AY BX BY CX CY, tri: coordinates within +-10000tri: degenerate (zero area) (the previous triangle is kept)attr: A B Cpoint: X Y / pixel: X Y, point: define a triangle firstunknown command XInput:
cLoading…
Output:
cLoading…
Hidden tests cover a clockwise (negative) triangle, points on each edge and at each vertex, a point beyond two edges, pixel centres, negative and fractional attribute results, and input errors.