\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{url}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}

\title{Graphics - Practicumopdracht 1}
\author{Sander van Veen \& Tadde\"us Kroes \\ 6167969 \& 6054129 \\
\url{sandervv@gmail.com} \& \url{taddeuskroes@hotmail.com}}

\maketitle

\tableofcontents

\pagebreak

\section{Questions}
\begin{enumerate}

%question 1
\item \emph{What factor(s) inherent in the rasterization algorithm used here influence the speed with which a triangle is drawn?}

%answer
\begin{itemize}

\item Early break points within a loop. We used this in two different cases. First there is the equation $\alpha >= 0$ \footnote[1]{All equations in this question can found in the pseudocode on page 169 in the book.}. This equation is executed along with $\beta >= 0$ and $\gamma >= 0$ after calculating $\alpha$, $\beta$ and $\gamma$. Instead, we could also put the equality check after each calculation, saving us 1 or even 2 (best case) multiplications in each iteration. Line 140-142 in \emph{trirast.c} realize this principle: if \texttt{k} turns out to be less then zero, the condition is false and \texttt{l} is not calculated, etc.. \\
The second case is when the triangle is already being drawn. All horizontal lines in the triangle are closed, so they only end once. So, when the line ends (when the condition is false and drawing is true) the rest of the line can be skipped.

\item Precalculate (parts of) calculations that are used inside a loop. For example: \\ \\
\texttt{
for( b = 0; b = 10; b++ ) \\
\{ \\
    a = b / c; \\
\} \\
} \\
can be replaced by: \\ \\
\texttt{
d = 1 / c; \\
for( b = 0; b = 10; b++ ) \\
\{ \\
	a = b * d; \\
\}
} \\
\\
because a multiplication costs less time than a division. For example, we used this feature when calculating $\frac{\texttt{alpha}}{f_{12}(x0, y0)}$ (line 140, \emph{trirast.c}).

\end{itemize}

%question 2
\item \emph{The book notes on page 65 at the bottom that “[...] the test is not perfect because the line through the edge may also go through the offscreen point [...]”. How would you handle a situation in which a pixel is exactly on an edge and the edge runs exactly through the offscreen point?}\\

%answer
This can be solved by defining an alternative offscreen point. This second point can be used in cases when the edge runs through the first point.

\end{enumerate}

\pagebreak

\section{Incremental algorithm}

In the optimized code, it is possible to exit the inner loop, when one or more
 of the variables $\alpha$, $\beta$ or $\gamma$ is less than zero. This is an 
 early exit point, because the expression for those variables will return zero
 till the end of the inner loop.
\\
\\
We asked ourselves the question: "If it is possible to leave the inner loop 
early, is it possible to calculate when the inner loop should be entered?". 
\\
\\
Below is a triangle drawn for the points a (2,4), b (0,1) and c (3,0). In order 
 to draw the triangle in OpenGL, the outer loop will start at $y = 0$ and
 increases $y$ with 1 until $y = 4$. If the program starts with $x = 3$ and adds 
 the delta's of the triangle, it is able to reduce the inner loop.

\begin{center}
	\includegraphics[scale=.6]{triangle-plot-1.png}
	\captionof{figure}{Example of a triangle with delta's on each side.}
\end{center}
\\
\\
The equation for a delta of a line is: $\Delta ab = (a_x-b_x)/(a_y-b_y)$. In the 
triangle shown above, this equation results in the following constants:

$$\Delta ab = (2-0)/(4-2) = 1$$
$$\Delta ac = (2-3)/(4-0) = -1/4$$
$$\Delta bc = (0-3)/(1-0) = -3$$
\\
Given these constants, the inner loop can be constructed using an incremental
algorithm. While $y < b_y$, the start of the inner loop is the previous start 
plus $\Delta bc$ and the end of the inner loop is the previous end plus 
$\Delta ac$. When $y > b_y$, the start of the inner loop is the previous
start plus $\Delta ab$ and the end of the inner loop is the previous end plus
$\Delta ac$. This algorithm iterates over the drawn pixels only, except for
the pixels on the so-called "triangle edge". Those egde pixels are drawn, if
they succeed in the off-screen point test.

\end{document}
