# Rasterization

## Old-school method: Line sweeping

Note that the alpha & beta are proportions of similar triangles.

## Better mathod for multithread processor

### Barycentric coordinate system

There is a line(A, B), and it’s vertices have different weight.

We assume they are equal to, $a$ and $b$, respectively. So, if we want to find out the barycenter
of the line. We probably know that the barycenter $P$ well may be at the side closing A.

We assume $\frac{AP}{AB}$ equals to $i$ and $\frac{PB}{AB}$ equals to $j$.

Then $i + j = 1$.

Similarly, in the 3D space, the triangle’s barycenter named $P$, then
$P = i \overrightarrow{PA} + j\overrightarrow{PB} + k\overrightarrow{PC}$,
and $i + j + k = 1$.

If we regard $\overrightarrow{PA}$ , $\overrightarrow{PB}$ , and $\overrightarrow{PC}$ as the basis of a coordinate system, we would call system
a barycentric coordinate system.

Rename $(i,j,k)$ to $(w, u, v)$ and change the equation to:

Then the barycentric coordinates of point $P$ become $(1-u-v, u, v)$ .

### Calculate the value of u & v

We can find a linear system of two equations with two variables:

We can rewrite it in matrix form:

Let us program a new code:

In this code, the barycentric function compute the barycentric coordinates of
Point $P$ . The cartesian coordinates of point $P$ come from the triangle function. To determine whether the cartesian coordinates of point $P$ are in the
triangle by determining whether the value of the barycentric coordinates of it has a negative value.

The triangle function also has a clipping of the bounding box with the
screen rectangle to reduce the CPU load for the triangles outside of the
screen.

## References

Triangle rasterization and back face culling

Barycentric coordinate system

Rasterization: Barycentric coordinate system
http://example.com/2022/10/30/rasterize/
Author
mistgc
Posted on
October 30, 2022