<- back
hw2 (100 + 4 pts) upload hw.cpp* to glow
codebase & Github Issues (get homework help, report bugs, ask questions about lecture, etc.) video of a perfect solution to the main hw (no extra credit) for full credit, your submission should produce the same or very similar output
note: this is a somewhat lighter hw. if you feel like you only sort of understood the last hw, i recommend using this week to fill in any gaps before things blast off again 🚀

We can define linear interpolation as $LERP(t, \mathbf{a}, \mathbf{b}) = (1-t)\mathbf{a} + t\mathbf{b}$. $LERP(0, a, b)$ is $a$. $LERP(1, a, b)$ is $b$. $LERP(.5, a, b)$ is the average of $a$ and $b$. We can define a clamp as $CLAMP(t, a, b) = MIN(MAX(t, a), b)$. This function "clamps $c$ to the inveral $[a, b]$." To do a "clamped lerp" (which does not extrapolate"outside of the interval $[a, b]$) we can use $LERP(CLAMP(t, 0, 1), \mathbf{a}, \mathbf{b})$. To smoothly bounce back and forth between $\mathbf{a}$ and $\mathbf{b}$, we can use $LERP(.5 - .5cos(t\pi), \mathbf{a}, \mathbf{b})$, which we could perhaps call "cosine linear interpolation". The length of a vector is $|\mathbf{v}| = \sqrt{\sum_iv_i^2}$. In 3D, $|\mathbf{v}| = \sqrt{v_x^2 + v_y^2 + v_z^2}$. Exercise: Derive the formula for the distance between two points $distance(\mathbf{p}, \mathbf{q}) = \sqrt{\sum_i(q_i-p_i)^2}$.
HINT The distance between points $\mathbf{p}$ and $\mathbf{q}$ is the length of the vector pointing from $\mathbf{p}$ to $\mathbf{q}$.
Matrix multiplication is associative, i.e., $(\mathbf{A}\mathbf{B})\mathbf{C} = \mathbf{A}(\mathbf{B}\mathbf{C})$. Matrix multiplication is not, in general, commutative, i.e., $\mathbf{A}\mathbf{B}$ may or may not be equal to $\mathbf{B}\mathbf{A}$. $\mathbf{I}$ is the identity matrix, which is a square matrix with $1$'s on the diagonal and $0$'s everywhere else. $\mathbf{I}\mathbf{M} = \mathbf{M}\mathbf{I} = \mathbf{M}$. $\mathbf{I}\mathbf{v} = \mathbf{v}$. The inverse of a square matrix $\mathbf{A}$ is $\mathbf{A}^{-1}$. $\mathbf{A}^{-1}\mathbf{A} = \mathbf{A}\mathbf{A}^{-1} = \mathbf{I}$. Exercise: Prove $(\mathbf{A}\mathbf{B})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1}$.
HINT $\mathbf{I} = \mathbf{A}\mathbf{B}\mathbf{B}^{-1}\mathbf{A}^{-1}$
$\mathbf{A}\mathbf{x} = \mathbf{b}$ is a linear system of equations written as a matrix equation. E.g., the general 3D linear system of equations $$\begin{cases}A_{11}x_1 + A_{12}x_2 + A_{13}x_3 = b_1\\A_{21}x_1+A_{22}x_2+ A_{23}x_3 = b_2\\A_{31}x_1 + A_{32}x_2 + A_{33}x_3 = b_3\end{cases}$$can be written as the matrix equation $$\begin{bmatrix}A_{11}&A_{12}& A_{13}\\A_{21}&A_{22}& A_{23}\\A_{31}&A_{32}& A_{33}\end{bmatrix}\begin{bmatrix}x_1\\x_2\\x_3\end{bmatrix} = \begin{bmatrix}b_1\\b_2\\b_3\end{bmatrix},$$which we summarize as $\mathbf{A}\mathbf{x} = \mathbf{b}.$ If $\mathbf{A}$ is invertible, then the solution to the system is $\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}$. This is powerful! This also raises the question: "Why did we spend so many hours of our finite lives solving linear systems by hand?"
a. (20 pts) hello linear system Write the following linear system as a matrix equation, and solve it using the snail function inverse(mat3 M). Print your answer using using snails pretty printing function pprint(...). $$\begin{cases}3x=z\\x-y=-1\\z-y=1\end{cases}$$If we call the solution to the system point $\mathbf{p}=(x, y, z)^T$, what is distance from $\mathbf{p}$ to $\mathbf{q} = (-0.282550, 3.282550, 5.565100)^T$? printf("%lf\n", ...); your answer.
HINT rewrite the system in the form $$\begin{cases}A_{11}x+A_{12}y+A_{13}z=b_1\\A_{21}x+A_{22}y+A_{23}z=b_2\\A_{31}x+A_{32}y+A_{33}z=b_3\end{cases}$$
b. (5 pts) point in circle Complete the app by setting the boolean inside to indicate whether the test point is inside the circle. c. (25 pts) points in circle Extend your solution to the previous problem into a new app, which should do the same thing, but now with 4096 points. All points should still be draggable. d. (10 pts) contrived problem to make you learn how to use a debugger Please find the first frame that local variable k ends in the digits 47 (printf _not_ allowed), and set variable the_first_frame_that_k_ends_in_47 to match. if you are successful, the screen should turn William's purple (don't be fooled by a slightly darker decoy purple).
premium bonus HINT debuggers are powerful! you can actually type expressions into the watch window. for example, k % 100 == 47
e. (10 pts) fun with lerp Draw three dots with x-coordinate interpolated between a and b according to time t. A green dot should use (vanilla, unclamped) linear interpolation; a red dot should use clamped linear interpolation; and a purple dot should use "cosine linear interpolation." 🔺. (+ 1 pt extra credit) Add a white dot that uses triangle wave linear interpolation. Up until time 1, it should behave exactly like vanilla or clamped lerp. Then it will look like the ball is bouncing off the vertical lines, a la hw1. No workarounds allowed (though also no pressure to write a macro). f. (10 pts) fun with barycentric coordinates Consider a 2D triangle with vertices $\mathbf{a}$, $\mathbf{b}$, $\mathbf{c}$. Consider a point $\mathbf{p}$ with barycentric coordinates $(\alpha, \beta, \gamma)^T$. I.e. $\mathbf{p} = \alpha\mathbf{a} + \beta\mathbf{b} + \gamma\mathbf{c}$, where $\alpha + \beta + \gamma = 1$. Solve for $\alpha, \beta, \gamma$ using the snail function inverse(mat3 M).
HINT $$\begin{cases}a_x\alpha+b_x\beta+c_x\gamma=p_x\\a_y\alpha+b_y\beta+c_y\gamma=p_y\\\alpha+\beta+\gamma=1\end{cases}$$
g. (20 pts + up to 3 pts extra credit) creative coding take 0 Using no fewer than 3 calls to LERP (or CLAMPED_LERP, COS_LERP), make an animation evocative of the topic "Changing Seasons" or "Winter Wonderland" that you find visually pleasing. You may use basic_draw(...) and gl_*. You are encouraged to interpolate position, color, size... note: You will get full credit (but no extra credit) for meeting the bare minimum of this spec.
🐄