\documentclass[12pt]{article}

\usepackage{ngerman,a4}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{stmaryrd}
\usepackage{enumerate}
\usepackage{alltt}
\usepackage[latin1]{inputenc}
%\usepackage{graphicx}
\usepackage{cancel}
\usepackage{fancyhdr}

\title{Computer Graphics \\ Assignment 1}
\author{Jan Hendrik Dithmar - Matriculation number 2031259 \\
				Pascal Gwosdek - Matriculation number 2505221
	}
\date{}

\pagestyle{fancy}

\lhead{Assignment 1}
\chead{}
\rhead{Dithmar (2031259), Gwosdek (2505221)}
\lfoot{Computer Graphics 1}
\cfoot{Page \thepage}
\rfoot{WS 2006/2007}

\begin{document}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\newcommand{\AND}{\ensuremath{\wedge}}
\newcommand{\OR}{\ensuremath{\vee}}
\newcommand{\SetR}{\ensuremath{\mathbb{R}}}
\newcommand{\SetN}{\ensuremath{\mathbb{N}}}
\newcommand{\SetC}{\ensuremath{\mathbb{C}}}
\newcommand{\SetZ}{\ensuremath{\mathbb{Z}}}
\newcommand{\SetQ}{\ensuremath{\mathbb{Q}}}
\newcommand{\SetB}{\ensuremath{\mathbb{B}}}
\newcommand{\QED}{\ensuremath{\hspace*{\fill}\square}}
\newcommand{\FLASH}{\ensuremath{\hspace*{\fill}\lightning}}
\newcommand{\OnotO}{\ensuremath{\mathcal{O}}}
\newcommand{\bild}[4]{
	\begin{figure}[htpb]
		\centering
			\includegraphics[width=#1cm]{#2}\\[0.2cm]
			\caption{#3}
			\label{#4}
	\end{figure}
	}

\maketitle

\thispagestyle{empty}

\newpage
\setcounter{page}{1}

\section*{Exercise 1.1}
\begin{eqnarray*}
	focus & = & |\underline{dir}| = \sqrt{\underline{dir}.x^2 + \underline{dir}.y^2 + \underline{dir}.z^2} \\
	\mbox{\itshape aspect ratio} & = & \frac{resX}{resY} \\
%	\underline{y} & = & - \frac{up}{|up|} \cdot focus \cdot \tan \left(\frac{angle}{2}\right) \\
	\underline{y} & = & focus \cdot \tan \left(\frac{angle}{2}\right) \cdot \frac{\underline{dir} \times \underline{x}}{|\underline{dir} \times \underline{x}|} \\
%	\underline{x} & = & (\mbox{\itshape aspect ratio} \cdot \underline{y}) \times \left( \frac{dir}{|dir|}\right) \\
	\underline{x} & = & (\mbox{\itshape aspect ratio} \cdot focus) \cdot \tan \left(\frac{angle}{2}\right) \cdot \frac{\underline{dir} \times \underline{up}}{|\underline{dir} \times \underline{up}|} \\
	\underline{f} & = & focus \cdot \frac{\underline{dir}}{|\underline{dir}|}
\end{eqnarray*}
Now one has to use the formula from the lecture:
$$\underline{ray.dir} = \underline{f} + 2 \left( \frac{X + 0.5}{resX} - \frac{1}{2}\right) \cdot \underline{x} + 2 \left(\frac{Y + 0.5}{resY} - \frac{1}{2}\right) \cdot \underline{y}$$

\newpage
\section*{Exercise 1.2}
\subsubsection*{(a)}
We have to put the ray into a plane:
\begin{eqnarray*}
	& & (p-a) \cdot n = 0 \\
	& \Leftrightarrow & (r(t)-a) \cdot n = 0 \\
	& \Leftrightarrow & (o_x + td_x - ax) \cdot n_x + (o_y + td_y - a_y) \cdot n_y + (o_z + td_z - a_z) \cdot n_z = 0
\end{eqnarray*}
Now, there is a case distinction.
\begin{enumerate}[1.]
	\item the ray is parallel to the plane and also the ray is in the plane \\
		$\Rightarrow d \cdot n = 0$ and $(o-a) \cdot n = 0$ \\
		$\Rightarrow$ for all $t$, the equation is satisfied $\checkmark$
	\item the ray is parallel to the plane and the ray is not in the plane \\
		$\Rightarrow d \cdot n = 0$, but $(o-a) \cdot n \neq 0$ \\
		$\Rightarrow$ no $t$ satisfies the equation $\checkmark$
	\item the ray is not parallel to the plane (and obviously the ray is not in the plane) \\
		$\Rightarrow$ exactly one intersection with
		$$t = \frac{(a_x-o_x) \cdot n_x + (a_y-o_y) \cdot n_y + (a_z-o_z) \cdot n_z}{d_x \cdot n_x + d_y \cdot n_y + d_z \cdot n_z} = \frac{(a-o) \cdot n}{d \cdot n}$$
		Since $d$ not perpendicular to $n$, $d \cdot n \neq 0$ $\checkmark$
\end{enumerate}

\newpage
\subsubsection*{(b)}
Now we put the ray into a sphere:
\begin{eqnarray*}
	& & (p - C)^2 - R^2 = 0 \\
	& \Leftrightarrow & (p_x - C_x)^2 + (p_y - C_y)^2 + (p_z - C_z)^2 - R^2 = 0 \\
	& \Leftrightarrow & (o_x + td_x - C_x)^2 + (o_y + td_y - C_y)^2 + (o_z + td_z - C_z)^2 - R^2 = 0 \\
	& \Leftrightarrow & (td_x + (o_x - C_x))^2 + (td_y + (o_y - C_y))^2 + (td_z - (o_z - C_z))^2 - R^2 = 0 \\
	& \Leftrightarrow & t^2 d_x^2 + 2 td_x (o_x - C_x) + (o_x - C_x)^2 \\
	& & + t^2 d_y^2 + 2 td_y (o_y - C_y) + (o_y - C_y)^2 \\
	& & + t^2 d_x^2 + 2 td_x (o_x - C_x) + (o_x - C_x)^2 - R^2 = 0 \\
	& \Leftrightarrow & t^2 (d_x^2 + d_y^2 + d_z^2) + 2t(d_x(o_x - C_x) + d_y(o_y - C_y) + d_z(o_z - C_z)) \\
	& & + (o_x - C_x)^2 + (o_y - C_y)^2 + (o_z - C_z)^2 - R^2 = 0 \\
	& \Leftrightarrow & t^2 \cdot d^2 + 2t(d(o-C)) + (o-C)^2 - R^2 = 0 \\
	& \Rightarrow & t = \frac{-2(d(o-C)) \pm \sqrt{4(d(o-C))^2 - 4d^2 \cdot (o-C^2) - R^2}}{2d^2} \\
	& \Rightarrow & t = \frac{-d(o-C) \pm \sqrt{M}}{d^2} \quad \mbox{where } M = (d(o-C))^2 - d^2(o-C)^2 + R^2
\end{eqnarray*}
There are three possible cases:
\begin{enumerate}[1.]
	\item $M=0 \AND t_1>0 \AND t_2>0$: there exists exactly one solution. \\The ray is tangential. $\checkmark$
	\item $M<0 \AND t_1<0 \AND t_2<0$: there exists no solution. \\The ray is missing the sphere. $\checkmark$
	\item $M>0 \AND t_1>0 \AND t_2>0$: there exist two solutions. \\The ray hits the sphere. $\checkmark$
\end{enumerate}

\newpage
\section*{Exercise 1.4}
\subsubsection*{(a)}
We have a ray $R(t) = O + t \cdot D$ and a quadric $ax^2+by^2+cz^2+dxy+exz+fyz+gx+hy+iz+j=0$. Now we can start with the following equation:
\begin{eqnarray*}
	& & a(o_x+td_x)^2 + b(o_y+td_y)^2 + c(o_z+td_z)^2 + d(o_x+td_x)(o_y+td_y) \\
	& & + e(o_x+td_x)(o_z+td_z) + f(o_y+td_y)(o_z+td_z) + g(o_x+td_x) + h(o_y+td_y) \\
	& & + i(o_z+td_z) + j = 0 \\
	& \Leftrightarrow & a(o_x^2+2o_xtd_x+t^2d_x^2) + b(o_y^2+2o_ytd_y+t^2d_y^2) + c(o_z^2+2o_ztd_z+t^2d_z^2) \\
	& & + do_xo_y + do_xtd_y + dtd_xo_y + dtd_xtd_y + eo_xo_z + eo_xtd_z + etd_xo_z + etd_xtd_z \\
	& & + fo_yo_z + fo_ytd_z + ftd_yo_z + ftd_ytd_z + go_x + gtd_x + ho_y + htd_y \\
	& & + io_z + otd_z + j = 0 \\
	& \Leftrightarrow & t^2 \cdot (\underbrace{ad_x^2 + bd_y^2 + cd_z^2 + dd_xd_y + ed_xd_z + fd_yd_z}_{\alpha}) \\
	& & + t \cdot (\underbrace{2ao_xd_x + 2bo_yd_y + 2co_zd_z + do_xd_y + dd_xo_y + eo_xd_z + ed_xo_z}_{\beta} \\
	& & \quad \quad \underbrace{+ fo_yd_z + fd_yo_z + gd_x + hd_y + id_z}_{\beta}) \\
	& & + \underbrace{ao_x^2 + bo_y^2 + co_z^2 + do_xo_y + eo_xo_z + fo_yo_z + go_x + ho_y + io_z + j}_{\gamma} = 0 \\
	& \Leftrightarrow & \alpha t^2 + \beta t + \gamma = 0 \\
	& \Rightarrow & t_{1,2} = - \frac{\beta}{2\alpha} \pm \sqrt{\frac{\beta^2}{4\alpha^2} - \frac{\gamma}{\alpha}} \\
\end{eqnarray*}

\newpage
\subsubsection*{(b)}
We take the formula of the sphere:
\begin{eqnarray*}
	& & (x-C_x)^2 + (y-C_y)^2 + (z-C_z)^2 = R^2 \\
	& \Leftrightarrow & x^2 - 2xC_x + C_x^2 + y^2 - 2yC_y^2 + C_y^2 + z^2 - 2zC_z + C_z^2 - R^2 = 0 \\
	& \Leftrightarrow & x^2 + y^2 + z^2 - 2xC_x - 2yC_y - 2zC_z + C_x^2 + C_y^2 + C_z^2 - R^2 = 0
\end{eqnarray*}
Now we represent the equation above by using the quadric formula with
$$a=1,b=1,c=1,d=0,e=0,f=0,$$
$$g=-2C_x,h=-2C_y,i=-2C_z,j=C_x^2+C_y^2+C_z^2-R^2$$

\begin{eqnarray*}
	\Rightarrow \alpha & = & d_x^2 + d_y^2 + d_z^2 = d^2 \\
	\beta & = & 2o_xd_x + 2o_yd_y + 2o_zd_z - 2C_xd_x - 2C_yd_y - 2C_zd_z \\
	& = & 2((o-C)d) \\
	\gamma & = & o_x^2 + o_y^2 + o_z^2 + 2C_xo_x - 2C_yo_y - 2C_zo_z + C_x^2 + C_y^2 + C_z^2 - R^2 \\
	& = & o_x^2 - 2C_xo_x + C_x^2 + o_y^2 - 2C_yo_y + C_y^2 + o_z^2 - 2C_zo_z + C_z^2 - R^2 \\
	& = & (o_x-C_x)^2 + (o_y-C_y)^2 + (o_z-C_z)^2 - R^2 \\
	& = & (o-C)^2 - R^2
\end{eqnarray*}
Now we can state the values for $t$:
\begin{eqnarray*}
	t_{1,2} & = & - \frac{2((o-C)d)}{2d^2} \pm \sqrt{\frac{(2((o-C)d)^2}{4(d^2)^2} - \frac{(o-C)^2 - R^2}{d^2}} \\
	& = & \frac{(o-C)d}{d^2} \pm \sqrt{\frac{((o-C)d)^2}{(d^2)^2} - \frac{(o-C)^2-R^2}{d^2}} \\
	& = & \frac{(o-C)d \pm \sqrt{((o-C)d)^2 - d^2(o-C)^2 - R^2}}{d^2}
\end{eqnarray*}

\end{document}