~luyu/comp5411-asgn3

3158dda4025c8ade71be43bc6cacb4284ed302bf — Luyu Cheng 8 months ago main
Add everything
A  => .gitignore +305 -0
@@ 1,305 @@
__latexindent_temp.tex
*.gnuplot
main.pdf

# Created by https://www.toptal.com/developers/gitignore/api/latex
# Edit at https://www.toptal.com/developers/gitignore?templates=latex

### LaTeX ###
## Core latex/pdflatex auxiliary files:
*.aux
*.lof
*.log
*.lot
*.fls
*.out
*.toc
*.fmt
*.fot
*.cb
*.cb2
.*.lb

## Intermediate documents:
*.dvi
*.xdv
*-converted-to.*
# these rules might exclude image files for figures etc.
# *.ps
# *.eps
# *.pdf

## Generated if empty string is given at "Please type another file name for output:"
.pdf

## Bibliography auxiliary files (bibtex/biblatex/biber):
*.bbl
*.bcf
*.blg
*-blx.aux
*-blx.bib
*.run.xml

## Build tool auxiliary files:
*.fdb_latexmk
*.synctex
*.synctex(busy)
*.synctex.gz
*.synctex.gz(busy)
*.pdfsync

## Build tool directories for auxiliary files
# latexrun
latex.out/

## Auxiliary and intermediate files from other packages:
# algorithms
*.alg
*.loa

# achemso
acs-*.bib

# amsthm
*.thm

# beamer
*.nav
*.pre
*.snm
*.vrb

# changes
*.soc

# comment
*.cut

# cprotect
*.cpt

# elsarticle (documentclass of Elsevier journals)
*.spl

# endnotes
*.ent

# fixme
*.lox

# feynmf/feynmp
*.mf
*.mp
*.t[1-9]
*.t[1-9][0-9]
*.tfm

#(r)(e)ledmac/(r)(e)ledpar
*.end
*.?end
*.[1-9]
*.[1-9][0-9]
*.[1-9][0-9][0-9]
*.[1-9]R
*.[1-9][0-9]R
*.[1-9][0-9][0-9]R
*.eledsec[1-9]
*.eledsec[1-9]R
*.eledsec[1-9][0-9]
*.eledsec[1-9][0-9]R
*.eledsec[1-9][0-9][0-9]
*.eledsec[1-9][0-9][0-9]R

# glossaries
*.acn
*.acr
*.glg
*.glo
*.gls
*.glsdefs
*.lzo
*.lzs

# uncomment this for glossaries-extra (will ignore makeindex's style files!)
# *.ist

# gnuplottex
*-gnuplottex-*

# gregoriotex
*.gaux
*.glog
*.gtex

# htlatex
*.4ct
*.4tc
*.idv
*.lg
*.trc
*.xref

# hyperref
*.brf

# knitr
*-concordance.tex
# TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files
# *.tikz
*-tikzDictionary

# listings
*.lol

# luatexja-ruby
*.ltjruby

# makeidx
*.idx
*.ilg
*.ind

# minitoc
*.maf
*.mlf
*.mlt
*.mtc[0-9]*
*.slf[0-9]*
*.slt[0-9]*
*.stc[0-9]*

# minted
_minted*
*.pyg

# morewrites
*.mw

# newpax
*.newpax

# nomencl
*.nlg
*.nlo
*.nls

# pax
*.pax

# pdfpcnotes
*.pdfpc

# sagetex
*.sagetex.sage
*.sagetex.py
*.sagetex.scmd

# scrwfile
*.wrt

# sympy
*.sout
*.sympy
sympy-plots-for-*.tex/

# pdfcomment
*.upa
*.upb

# pythontex
*.pytxcode
pythontex-files-*/

# tcolorbox
*.listing

# thmtools
*.loe

# TikZ & PGF
*.dpth
*.md5
*.auxlock

# todonotes
*.tdo

# vhistory
*.hst
*.ver

# easy-todo
*.lod

# xcolor
*.xcp

# xmpincl
*.xmpi

# xindy
*.xdy

# xypic precompiled matrices and outlines
*.xyc
*.xyd

# endfloat
*.ttt
*.fff

# Latexian
TSWLatexianTemp*

## Editors:
# WinEdt
*.bak
*.sav

# Texpad
.texpadtmp

# LyX
*.lyx~

# Kile
*.backup

# gummi
.*.swp

# KBibTeX
*~[0-9]*

# TeXnicCenter
*.tps

# auto folder when using emacs and auctex
./auto/*
*.el

# expex forward references with \gathertags
*-tags.tex

# standalone packages
*.sta

# Makeindex log files
*.lpz

# xwatermark package
*.xwm

# REVTeX puts footnotes in the bibliography by default, unless the nofootinbib
# option is specified. Footnotes are the stored in a file with suffix Notes.bib.
# Uncomment the next line to have this generated file ignored.
#*Notes.bib

### LaTeX Patch ###
# LIPIcs / OASIcs
*.vtc

# glossaries
*.glstex

# End of https://www.toptal.com/developers/gitignore/api/latex

A  => README.md +1 -0
@@ 1,1 @@
# comp5411-asgn3
\ No newline at end of file

A  => main.tex +488 -0
@@ 1,488 @@
\documentclass[a4paper]{article}

\usepackage{amsmath,amsfonts}

\setlength{\oddsidemargin}{0in}
\setlength{\textwidth}{6.5in}
% \setlength{\topmargin}{0in}
% \setlength{\headheight}{0in}
% \setlength{\headsep}{0in}
% \setlength{\textheight}{8.7in}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[T1]{fontenc}
\usepackage{charter}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{multicol}
\usepackage{array}
\usepackage{amsmath}
\usepackage{cancel}
\usepackage{enumitem}
\usepackage{pgfplots}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{xcolor}
\usetikzlibrary{arrows.meta}

\hypersetup{colorlinks=true,linkcolor=purple}

\def\comma{\text{,}}
\def\period{\text{.}}
\newcommand*{\transpose}{^{\mkern-1.5mu\mathsf{T}}}

\newtheorem*{lemma}{Lemma}
\newtheorem*{property}{Property}

\setcounter{section}{-1}

\title{Advanced Computer Graphics \\ \Large{Geometry Part -- Written Assignment}}
\author{Luyu Cheng, HKUST}

\begin{document}
\maketitle

\newcommand{\bc}{B\'{e}zier curve}
\newcommand{\bcs}{B\'{e}zier curves}
\newcommand{\ubs}[2]{\underset{#1}{\underbrace{#2}}}

\begin{enumerate}
    \item 
    \begin{enumerate}
        \item {\bfseries Endpoint interpolation property.} Compute the endpoints. \begin{alignat*}{1}
            C(0) & = \frac16P_0 + \frac23P_1 + \frac16P_2 \neq P_0 \\
            C(1) & = \frac16P_1 + \frac23P_2 + \frac16P_3 \neq P_3
        \end{alignat*}
        Therefore, the basis functions do not satisfy the endpoint interpolation property.

        {\bfseries Partition of unity property.} All terms of $u$ are cancelled in the calculation.
        \begin{align*}
            \sum_{k=0}^3N_i(u) &= \frac16(1-u)^3 + \frac16(3u^3−6u^2+4) + \frac16(−3u^3+3u^2+3u+1)+\frac16u^3\\
            &= \frac16\left(
                \left(\cancel{-u^3}+\cancel{3u^2}-\cancel{3u}+1\right)
                +\left(\cancel{3u^3}−\cancel{6u^2}+4\right)
                +\left(−\cancel{3u^3}+\cancel{3u^2}+\cancel{3u}+1\right)
                +\cancel{u^3}\right) \\
            &= 1
        \end{align*}
        Therefore, the basis functions have the partition of unity property.

        {\bfseries Convex hull property.} $C(u)$ is a weighted average of four control points and weights sum to 1 (i.e. is a convex combination of control points). Therefore, $C(u)$ belongs to the convex hull of control points.

        % Derive the first derivative and the second derivative of C(u). Let C1(u) and C2(u) be two B-sline curves defined by P0P1P2P3 and P1P2P3P4, respectively. Find the first and second derivatives of the two B-spline curves at the joint and discuss the order of continuity there.
        \item \label{it:1b} Here are the first and the second derivatives of $C(u)$. \begin{alignat*}{6}
            C'(u) & = & \ubs{N'_0(u)}{-\frac{1}{2}(1-u)^2} P_0 & + & \ubs{N'_1(u)}{\left(\frac{3}{2}u^2 - 2u\right)} P_1 & + & \ubs{N'_2(u)}{\left(-\frac{3}{2}u^2+u+\frac{1}{2}\right)} P_2 & + \ubs{N'_3(u)}{\frac{1}{2}u^2} P_3 \\
            C''(u) & = & \ubs{N''_0(u)}{(1-u)} P_0 & + & \ubs{N''_1(u)}{(3u-2)} P_1 & + & \ubs{N''_2(u)}{(-3u+1)} P_2 & + \ubs{N''_3(u)}{u} P_3
        \end{alignat*}

        Prove the $C_0$ continuity at the joint of $C_1$ and $C_2$. \begin{alignat*}{3}
            C_1(1) & =
            \frac16P_1 + \frac23P_2 + \frac16P_3 && =
            C_2(0) && =
            \frac16P_1 + \frac23P_2 + \frac16P_3
        \end{alignat*}

        Prove the $C_1$ continuity at the joint of $C_1$ and $C_2$. \begin{alignat*}{3}
            C'_1(1) & = &
            \cancel{-\frac{1}{2}(1-1)^2 P_0} + \left(\frac{3}{2} - 2\right) P_1 + \cancel{\left(-\frac{3}{2}+1+\frac{1}{2}\right)} P_2 + \frac{1}{2} P_3 & =
            -\frac12P_1 + \frac12P_3 \\
            C'_2(0) & = &
            -\frac{1}{2}(1-0)^2 P_1 + \cancel{\left(\frac{3}{2}\cdot 0 - 0\right)} P_2 + \left(-\frac{3}{2}+0+\frac{1}{2}\right) P_3 + \cancel{\frac{1}{2}\cdot 0 P_4} & =
            -\frac12P_1 + \frac12P_3
        \end{alignat*}

        Prove the $C_2$ continuity at the joint of $C_1$ and $C_2$. \begin{alignat*}{3}
            C''_1(1) & = &
            \cancel{(1-1) P_0} + (3-2) P_1 + (-3+1) P_2 + 1 P_3 & =
            P_1 -2 P_2 + P_3 \\
            C''_2(0) & = &
            (1 - 0)P_1 + (0 - 2)P_2 + (0 + 1)P_3 + \cancel{0 P_3} & =
            P_1 - 2P_2 + P_3
        \end{alignat*}

        Therefore, the joint of B-spline curves is $C^2$ continuity.
        
        % What properties of the basis functions lead to the order of continuity being independent of the location of the control points?
        \item If we concatenate the basis functions into a piecewise function $N(x)$. $N(x)$ is $C^2$ continuous at every joints. The $C^2$ continuity makes the order of continuity is independent of the location of control points. \begin{equation}
            \label{eq:piecewise-basis-functions}
            N(x)=\begin{cases}
                N_0(r) & x=4n+0+r\\
                N_1(r) & x=4n+1+r\\
                N_2(r) & x=4n+2+r\\
                N_3(r) & x=4n+3+r\\
            \end{cases}, n\in \mathbb Z, 0\le r<1.
        \end{equation} We can discover the property by observing the calculation in solution \ref{it:1b}. We can find that \begin{displaymath}
            \begin{array}{ccc}
                N_1(1) = N_0(0), & N'_1(1) = N'_0(0), & N''_1(1) = N''_0(0); \\
                N_2(1) = N_1(0), & N'_2(1) = N'_1(0), & N''_2(1) = N''_1(0); \\
                N_3(1) = N_2(0), & N'_3(1) = N'_2(0), & N''_3(1) = N''_2(0).
            \end{array}
        \end{displaymath}
        We can also find these relations on the plot (Figure \ref{fig:1c}) of the basis functions and their derivatives. Note that values at endpoints of functions are the same.

        \begin{figure}
            \centering
            \begin{subfigure}{0.31\textwidth}
                \centering
                \begin{tikzpicture}[scale=0.95]
                    \centering
                    \begin{axis}[
                        width=0.8\textwidth,
                        height=1.2\textwidth,
                        scale only axis,
                        domain=0:1,
                        xmin=0, xmax=1,
                        ymin=-0.5, ymax=1.0,
                        grid,
                        minor x tick num=1,
                        minor y tick num=1,
                    ]
                        \addplot[no marks,red,thick] {1/6*(1-x)^3};
                        \addplot[no marks,teal,thick] {1/6*(3*x^3+(-6)*x^2+4)};
                        \addplot[no marks,blue,thick] {1/6*((-3)*x^3+3*x^2+3*x+1)};
                        \addplot[no marks,purple,thick] {1/6 * x^3};
                    \end{axis}
                \end{tikzpicture}
                \caption{The plot of \textcolor{red}{$N_0(u)$}, \textcolor{teal}{$N_1(u)$}, \textcolor{blue}{$N_2(u)$}, and \textcolor{purple}{$N_3(u)$}.}
            \end{subfigure}
            \hfill
            \begin{subfigure}{0.31\textwidth}
                \centering
                \begin{tikzpicture}[scale=0.95]
                    \centering
                    \begin{axis}[
                        width=0.8\textwidth,
                        height=1.2\textwidth,
                        scale only axis,
                        domain=0:1,
                        xmin=0, xmax=1,
                        ymin=-0.75, ymax=0.75,
                        grid,
                        minor x tick num=1,
                        minor y tick num=1,
                    ]
                        \addplot[no marks,red,thick] {(-1/2)*(1-x)^2};
                        \addplot[no marks,teal,thick] {(3/2)*x^2-2*x};
                        \addplot[no marks,blue,thick] {-(3/2)*x^2+x+1/2};
                        \addplot[no marks,purple,thick] {(1/2)*x^2};
                    \end{axis}
                \end{tikzpicture}
                \caption{The plot of \textcolor{red}{$N'_0(u)$}, \textcolor{teal}{$N'_1(u)$}, \textcolor{blue}{$N'_2(u)$}, and \textcolor{purple}{$N'_3(u)$}.}
            \end{subfigure}
            \hfill
            \begin{subfigure}{0.32\textwidth}
                \centering
                \begin{tikzpicture}[scale=0.95]
                    \centering
                    \begin{axis}[
                        width=0.8\textwidth,
                        height=1.2\textwidth,
                        scale only axis,
                        domain=0:1,
                        xmin=0, xmax=1,
                        ymin=-0.5, ymax=1,
                        grid,
                        minor x tick num=1,
                        minor y tick num=1,
                    ]
                        \addplot[no marks,red,thick] {1-x};
                        \addplot[no marks,teal,thick] {3*x-2};
                        \addplot[no marks,blue,thick] {0-3*x+1};
                        \addplot[no marks,purple,thick] {x};
                    \end{axis}
                \end{tikzpicture}
                \caption{The plot of \textcolor{red}{$N''_0(u)$}, \textcolor{teal}{$N''_1(u)$}, \textcolor{blue}{$N''_2(u)$}, and \textcolor{purple}{$N''_3(u)$}.}
            \end{subfigure}
            \caption{The plot of the basis functions and their derivatives. Note that values at endpoints of these functions are the same.}
            \label{fig:1c}
        \end{figure}
    \end{enumerate}
    \item
    \begin{enumerate}
        \item \label{it:2a} Suppose the points of $C(u)$ are $P_0=(0,0)$, $P_1=(1,4)$, and $P_2=(5,3)$. We have
        \begin{alignat*}{6}
            P_0^{(1)} &= \left(1-\frac{1}{4}\right)P_0       &&+ \frac{1}{4}P_1       &&= (\frac{1}{4},1) \\
            P_1^{(1)} &= \left(1-\frac{1}{4}\right)P_1       &&+ \frac{1}{4}P_2       &&= (2,\frac{15}{4}) \\
            P_0^{(2)} &= \left(1-\frac{1}{4}\right)P_0^{(1)} &&+ \frac{1}{4}P_1^{(1)} &&= (\frac{11}{16},\frac{27}{16})
        \end{alignat*}
        Figure \ref{fig:2-a} illustrates $C(u)$ and $Q(0.25)$. \footnote{Figures in this report are plotted with Ti$k$Z.}

        \begin{figure}
            \centering
            \begin{tikzpicture}
                \draw[loosely dotted] (0,0) grid (5.5,4.5);
                % \path[use as bounding box] (-2,-1) rectangle (5,5);
                \draw[->] (-0.2,0) -- (5.5,0) node[right] {$x$};
                \draw[->] (0,-0.25) -- (0,4.5) node[above] {$y$};
                \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5}
                \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
                \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4}
                \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
                \shade[top color=blue,bottom color=gray!50]
                    (0,0) parabola (0.1*1,0.01*1) |- (0,0);
                \draw[color=gray] (0,0) -- (1,4) -- (5,3);
                % P0, (1/3 P0+2/3 P1), (2/3 P1+1/3 P2), P2
                \draw[thick] (0,0) .. controls (2/3,2.664) and (2.331,3.663) .. (5,3);
                \foreach \p/\ptext/\o in {
                    (0,0)/$P_0$/{above left},
                    (1,4)/$P_1$/above,
                    (5,3)/$P_2$/right,
                    (0.6875,1.6875)/$Q(0.25)$/{right}} {
                    \draw[fill=black] \p circle (2pt) node[\o] {\ptext};
                }
            \end{tikzpicture}
            \caption{The quadratic Bezier curve in problem \ref{it:2a}}
            \label{fig:2-a}
        \end{figure}

        % Problem 2 (b)
        \item Let $P_0$, $P_1$, and $P_2$ be the points of a \bc. \begin{displaymath}
            \begin{alignedat}{1}
                P_0^{(1)} &= (1-t)P_0 + tP_1 \\
                P_1^{(1)} &= (1-t)P_1 + tP_2 \\
                P_0^{(2)} &= (1-t)P_0^{(1)} + tP_1^{(1)} \\
                &= (1-t)\left((1-t)P_0 + tP_1\right) + t\left((1-t)P_1 + tP_2\right) \\
                &= \ubs{\beta_0}{(1-t)^2} P_0 + \ubs{\beta_1}{2t(1-t)}P_1 + \ubs{\beta_2}{t^2} P_2
            \end{alignedat}
        \end{displaymath}
        Therefore, the basis functions are \begin{displaymath}
            \begin{alignedat}{1}
                \beta_0 &= t^2 \\
                \beta_1 &= 2t(1-t) \\
                \beta_2 &= (1-t)^2.
            \end{alignedat}
        \end{displaymath}

        % Problem 2. (c) Express the quadratic Bezier curves and their first derivative in matrix form.
        \item We can derive the matrix form of quadratic \bcs\space as follows. \begin{displaymath}
            \begin{alignedat}{1}
                C(t) & = (1-t)^2 P_1 + 2(1-t)t P_2 + t^2 P_3 \\
                & = (1 - 2t + t^2)P_1 + (0 + 2t - 2t^2)P_2 + (0 + 0t + t^2)P_3 \\
                & = \left[\begin{matrix}
                    1 - 2t + t^2 \\
                    0 + 2t - 2t^2 \\
                    0 + 0t + t^2
                \end{matrix}\right]^{\transpose} \left[\begin{matrix}
                    P_1 \\ P_2 \\ P_3
                \end{matrix}\right] \\
                & = \left[\begin{matrix}
                    1 \\ t \\ t^2
                \end{matrix}\right]^{\transpose} \left[\begin{matrix}
                    1  & 0  & 0 \\
                    -2 & 2  & 0 \\
                    1  & -2 & 1
                \end{matrix}\right] \left[\begin{matrix}
                    P_1 \\ P_2 \\ P_3
                \end{matrix}\right]
            \end{alignedat}
        \end{displaymath}
        We can derive the matrix form of the first derivative of quadratic \bcs\space as follows. \begin{displaymath}
            \begin{alignedat}{1}
                C'(t) & = 2t P_1 + 2(1-2t)P_2 + 2(t-1)P_3 \\
                & = (0+2t) P_1 + (2-4t) P_2 + (-1+2t) P_3 \\
                & = \left[\begin{matrix}
                    1 \\ t
                \end{matrix}\right]^{\transpose} \left[\begin{matrix}
                    0 & 2  & -1 \\
                    2 & -4 & 2
                \end{matrix}\right] \left[\begin{matrix}
                    P_1 \\ P_2 \\ P_3
                \end{matrix}\right]
            \end{alignedat}
        \end{displaymath}

        % Problem 2. (d) What are the conditions for two quadratic Bezier curves to join at C1 continuity in terms of the placement of the certain control points?
        \item Suppose we have two quadratic \bcs, $C_1(t)$ and $C_2(t)$. The points of $C_1(t)$ are $P_1$, $P_2$, and $P_3$. The points of $C_2(t)$ are $P_3$, $P_4$, and $P_5$.  Expand and reduce $C_1(u)$ and $C_2(t)$, we will have 
        \begin{alignat*}{4}
            C_1(t) & = (P_1-2P_2+P_3)t^2+2(-P_1+P_2)t+P_1\\
            C_2(t) & = (P_4-2P_5+P_6)t^2+2(-P_4+P_5)t+P_4 .
        \end{alignat*}
        Their derivatives are
        \begin{alignat*}{4}
            C'_1(t) & = 2t(P_1-2P_2+P_3)+2(-P_1+P_2)\\
            C'_2(t) & = 2t(P_4-2P_5+P_6)+2(-P_4+P_5) .
        \end{alignat*}

        Let $C_1(1)=C_2(0)$, we will have
        \begin{alignat*}{3}
            (\cancel{P_1}-\cancel{2P_2}+P_3)+\cancel{2(-P_1+P_2)}+\cancel{P_1} & = P_4 \\
            P_3 & = P_4
        \end{alignat*}

        Let $C'_1(1)=C'_2(0)$, we will have
        \begin{alignat*}{3}
            2(P_1-2P_2+P_3)+2(-P_1+P_2) & = 2(-P_4+P_5) \\
            -P_2 + P_3 & = -P_4 + P_5 \\
            \Longrightarrow P_3 = P_4 & = \frac{P_2 + P_5}2
        \end{alignat*}
        Therefore, the conditions are $P_3 = P_4 = \frac{P_2 + P_5}2$, which means that $P_3$ and $P_4$ are the midpoint of $P_2$ and $P_5$.
        
        % Problem 2. (e) Given a sequence of four points B0, B1, B2, B3. Explain how you might construct a C1 continuous quadratic Bezier spline comprising two Bezier curves that approximates the given points (i.e., none of the given points need to be interpolated). Draw a sketch of your construction.
        \item \label{it:2e} Let \begin{displaymath}
            \begin{array}{ccccc}
                P_0 = \frac{2}{3} B_0+\frac{1}{3} B_1\text{,} &
                P_1 = B_1\text{,} &
                P_2 = \frac{B_1+B_2}{2}\text{,} &
                P_3 = B_2\text{,} &
                P_4 = \frac{1}{3} B_2+\frac{2}{3} B_3\text{.}
            \end{array}
        \end{displaymath}
        Let $P_0, P_1, P_2$ be control points of the first \bc\space and $P_1, P_2, P_3$ be control points of the second \bc. Figure \ref{fig:2e} shows an example of the construction.

        \begin{figure}
            \centering
            \begin{tikzpicture}
                \draw[loosely dotted] (0,0) grid (8,4);
                % \path[use as bounding box] (-2,-1) rectangle (5,5);
                \draw[->] (-0.2,0) -- (8.5,0) node[right] {$x$};
                \draw[->] (0,-0.25) -- (0,4.5) node[above] {$y$};
                \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6, 7/7, 8/8}
                \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
                \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4}
                \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
                \shade[top color=blue,bottom color=gray!50]
                    (0,0) parabola (0.1*1,0.01*1) |- (0,0);
                \draw[color=gray] (3,0) -- (1,3) -- (4.5,3.5) -- (8,4) -- (6,0);
                % P0, (1/3 P0+2/3 P1), (2/3 P1+1/3 P2), P2
                \draw[thick] (2+1/3,1) .. controls (5/3,2) and (13/6,19/6) .. (4.5,3.5);
                \draw[thick] (4.5,3.5) .. controls (1.5+16/3,3.5/3+8/3) and (16/3+2,8/3) .. (4+8/3,4/3);
                \foreach \p/\ptext/\o in {
                    (3,0)/{$\textcolor{red}{B_0}$}/{above right},
                    {(2+1/3,1)}/{$P_0$}/{above right},
                    (1,3)/{$\textcolor{red}{B_1}$,$P_1$}/{above left},
                    (4.5,3.5)/$P_2$/above,
                    (8,4)/{$\textcolor{red}{B_2}$,$P_3$}/above,
                    {(4+8/3,4/3)}/{$P_4$}/{above left},
                    (6,0)/{$\textcolor{red}{B_3}$}/{above left}} {
                    \draw[fill=black] \p circle (2pt) node[\o] {\ptext};
                }
            \end{tikzpicture}
            \caption{The quadratic B\'{e}zier spline in problem \ref{it:2e}. The given points are in red.}
            \label{fig:2e}
        \end{figure}

        % Problem 2. (f) Repeat part (e) to construct a C2 continuous cubic Bezier spline comprising two Bezier curves for a minimum number of given points.
        \item \label{it:2f} Before the construction, we will introduce the conditions of a $C^2$ continuous cubic B\'ezier spline.
        
        \begin{lemma}
            Suppose a B\'{e}zier spline has \bcs\space $C_1(t)$ and $C_2(t)$. The control points of $C_1(t)$ are $P_1$, $P_2$, $P_3$, and $P_4$. The control points of $C_2(t)$ are $Q_1$, $Q_2$, $Q_3$, and $Q_4$. If their joint is $C^2$ continuous, we have $P_4=Q_1$, $P_2-P_1=Q_4-Q_3$, and $(P_3-P_2)+(P_3-P_4)=(Q_2-Q_1)+(Q_2-Q_3)$. 
        \end{lemma}
        
        We can construct a $C^2$ continuous cubic B\'{e}zier spline (containing two \bcs) with at least \emph{three} given points. Suppose given points are $B_0$, $B_1$, and $B_2$. From the lemma, we know that we need to build 7 control points. The process is as follow.
        
        \begin{enumerate}
            \item Let $P_0 = B_0$ and $P_6 = B_2$. If $B_0$ and $B_2$ should not be interpolated, we can also choose $P_0$ and $P_6$ to be nearby points on line segments $\overline{B_0B_1}$ and $\overline{B_2B_1}$ respectively.
            \item Select $P_1$ and $P_5$ respectively on line segments $\overline{B_0B_1}$ and $\overline{B_1B_2}$ such that $\left|\overline{P_1B_1}\right|=\left|\overline{B_1P_5}\right|$. 
            \item Let $P_2=\frac{P_1+B_1}2$, $P_4=\frac{B_1+P_5}2$, and $P_3=\frac{P_2+P_4}2$.
        \end{enumerate}

        The first cubic \bc\space has control points $P_0$, $P_1$, $P_2$, and $P_3$. The second cubic \bc\space has control points $P_3$, $P_4$, $P_5$, and $P_6$. From Figure \ref{fig:2f} depicts three example of the construction.

        \begin{figure}
            \centering
            \begin{subfigure}{0.55\textwidth}
                \begin{tikzpicture}[scale=0.9]
                    \draw[loosely dotted] (0,0) grid (8.5,4);
                    % \path[use as bounding box] (-2,-1) rectangle (5,5);
                    \draw[->] (-0.2,0) -- (8.5,0) node[right] {$x$};
                    \draw[->] (0,-0.25) -- (0,4.5) node[above] {$y$};
                    \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6, 7/7, 8/8}
                    \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
                    \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4}
                    \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
                    \shade[top color=blue,bottom color=gray!50]
                        (0,0) parabola (0.1*1,0.01*1) |- (0,0);
                    \draw[color=gray] (1,1) -- (2,2) -- (3,3) -- (4,4) -- (5,3) -- (6,2) -- (8,0);
                    \draw[color=gray] (3,3) -- (5,3);
                    \draw[thick] (1,1) .. controls (2,2) and (3,3) .. (4,3);
                    \draw[thick] (4,3) .. controls (5,3) and (6,2) .. (8,0);
                    \foreach \p/\ptext/\o in {
                        (1,1)/{$\textcolor{red}{B_0}$,$P_0$}/{below},
                        (2,2)/{$P_1$}/{above left},
                        (3,3)/$P_2$/{above left},
                        (4,4)/{$\textcolor{red}{B_1}$}/above,
                        (4,3)/{$P_3$}/above,
                        (5,3)/{$P_4$}/{above right},
                        (6,2)/{$P_5$}/{above right},
                        (8,0)/{$\textcolor{red}{B_2}$,$P_6$}/{above right}} {
                        \draw[fill=black] \p circle (2pt) node[\o] {\ptext};
                    }
                \end{tikzpicture}
                \caption{An example.}
            \end{subfigure}
            \begin{subfigure}{0.44\textwidth}
                \centering
                \begin{tikzpicture}[scale=0.9]
                    \draw[loosely dotted] (0,0) grid (6.5,4);
                    % \path[use as bounding box] (-2,-1) rectangle (5,5);
                    \draw[->] (-0.2,0) -- (6.5,0) node[right] {$x$};
                    \draw[->] (0,-0.25) -- (0,4.5) node[above] {$y$};
                    \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6}
                    \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
                    \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4}
                    \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
                    \shade[top color=blue,bottom color=gray!50]
                        (0,0) parabola (0.1*1,0.01*1) |- (0,0);
                    \draw[color=gray] (2.5,0) -- (1.8315935,1.336813) -- (1.1657968,2.6684065) -- (0.5,4) -- (1.959856,3.7080288) -- (3.419712,3.4160576) -- (5.5,3);
                    \draw[color=gray] (1.1657968,2.6684065) -- (1.959856,3.7080288);
                    \draw[thick] (2.5,0) .. controls (1.8315935,1.336813) and (1.1657968,2.6684065) .. (1.5628264,3.1882176);
                    \draw[thick] (1.5628264,3.1882176) .. controls (1.959856,3.7080288) and (3.419712,3.4160576) .. (5.5,3);
                    \foreach \p/\ptext/\o in {
                        (2.5,0)/{$\textcolor{red}{B_0}$,$P_0$}/{above right},
                        (1.8315935,1.336813)/{$P_1$}/{left},
                        (1.1657968,2.6684065)/$P_2$/{left},
                        (0.5,4)/{$\textcolor{red}{B_1}$}/above,
                        (1.5628264,3.1882176)/{$P_3$}/{above left},
                        (1.959856,3.7080288)/{$P_4$}/{above right},
                        (3.419712,3.4160576)/{$P_5$}/{above right},
                        (5.5,3)/{$\textcolor{red}{B_2}$,$P_6$}/{below}} {
                        \draw[fill=black] \p circle (2pt) node[\o] {\ptext};
                    }
                \end{tikzpicture}
                \caption{Another example.}
                \label{fig:2fb}
            \end{subfigure}
            \begin{subfigure}{\textwidth}
                \centering
                \begin{tikzpicture}[scale=0.9]
                    \draw[loosely dotted] (0,0) grid (12,5);
                    % \path[use as bounding box] (-2,-1) rectangle (5,5);
                    \draw[->] (-0.2,0) -- (12,0) node[right] {$x$};
                    \draw[->] (0,-0.25) -- (0,4.5) node[above] {$y$};
                    \foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4, 5/5, 6/6, 7/7, 8/8, 9/9, 10/10, 11/11}
                    \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
                    \foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4, 5/5}
                    \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
                    \shade[top color=blue,bottom color=gray!50]
                        (0,0) parabola (0.1*1,0.01*1) |- (0,0);
                    \draw[color=gray] (0.5,2) -- (5.9285774,3.986065) -- (7.3142886,4.4930325) -- (8.7,5) -- (9.435513,3.7208471) -- (10.171026,2.441694) -- (11,1);
                    \draw[color=gray] (7.3142886,4.4930325) -- (9.435513,3.7208471);
                    \draw[thick] (0.5,2) .. controls (5.9285774,3.986065) and (7.3142886,4.4930325) .. (8.374901,4.10694);
                    \draw[thick] (8.374901,4.10694) .. controls (9.435513,3.7208471) and (10.171026,2.441694) .. (11,1);
                    \foreach \p/\ptext/\o in {
                        (0.5,2)/{$\textcolor{red}{B_0}$,$P_0$}/{below},
                        (5.9285774,3.986065)/{$P_1$}/{above left},
                        (7.3142886,4.4930325)/$P_2$/{above left},
                        (8.7,5)/{$\textcolor{red}{B_1}$}/above,
                        (8.374901,4.10694)/{$P_3$}/above,
                        (9.435513,3.7208471)/{$P_4$}/{above right},
                        (10.171026,2.441694)/{$P_5$}/{above right},
                        (11,1)/{$\textcolor{red}{B_2}$,$P_6$}/{below}} {
                        \draw[fill=black] \p circle (2pt) node[\o] {\ptext};
                    }
                \end{tikzpicture}
                \caption{Yet another example.}
                \label{fig:2fc}
            \end{subfigure}
            \caption{Three examples of the quadratic B\'{e}zier spline construction in problem \ref{it:2f}. The given points are in red.}
            \label{fig:2f}
        \end{figure}
    \end{enumerate}
\end{enumerate}

\end{document}
\ No newline at end of file

A  => problem-2f/.gitignore +1 -0
@@ 1,1 @@
/target

A  => problem-2f/Cargo.lock +277 -0
@@ 1,277 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "approx"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "072df7202e63b127ab55acfe16ce97013d5b97bf160489336d3f1840fd78e99e"
dependencies = [
 "num-traits",
]

[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"

[[package]]
name = "bytemuck"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72957246c41db82b8ef88a5486143830adeb8227ef9837740bdec67724cf2c5b"

[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "euclid"
version = "0.22.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da96828553a086d7b18dcebfc579bd9628b016f86590d7453c115e490fa74b80"
dependencies = [
 "num-traits",
]

[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
 "cfg-if",
 "libc",
 "wasi",
]

[[package]]
name = "libc"
version = "0.2.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b2f96d100e1cf1929e7719b7edb3b90ab5298072638fccd77be9ce942ecdfce"

[[package]]
name = "matrixmultiply"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a8a15b776d9dfaecd44b03c5828c2199cddff5247215858aac14624f8d6b741"
dependencies = [
 "rawpointer",
]

[[package]]
name = "nalgebra"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d506eb7e08d6329505faa8a3a00a5dcc6de9f76e0c77e4b75763ae3c770831ff"
dependencies = [
 "approx",
 "matrixmultiply",
 "nalgebra-macros",
 "num-complex",
 "num-rational",
 "num-traits",
 "simba",
 "typenum",
]

[[package]]
name = "nalgebra-macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "num-complex"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085"
dependencies = [
 "num-traits",
]

[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
 "autocfg",
 "num-traits",
]

[[package]]
name = "num-rational"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a"
dependencies = [
 "autocfg",
 "num-integer",
 "num-traits",
]

[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
 "autocfg",
]

[[package]]
name = "paste"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58"

[[package]]
name = "ppv-lite86"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3ca011bd0129ff4ae15cd04c4eef202cadf6c51c21e47aba319b4e0501db741"

[[package]]
name = "problem-2f"
version = "0.1.0"
dependencies = [
 "euclid",
 "nalgebra",
 "rand",
]

[[package]]
name = "proc-macro2"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
dependencies = [
 "unicode-xid",
]

[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
 "libc",
 "rand_chacha",
 "rand_core",
 "rand_hc",
]

[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
 "ppv-lite86",
 "rand_core",
]

[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
 "getrandom",
]

[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
 "rand_core",
]

[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"

[[package]]
name = "safe_arch"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "794821e4ccb0d9f979512f9c1973480123f9bd62a90d74ab0f9426fcf8f4a529"
dependencies = [
 "bytemuck",
]

[[package]]
name = "simba"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0b7840f121a46d63066ee7a99fc81dcabbc6105e437cae43528cea199b5a05f"
dependencies = [
 "approx",
 "num-complex",
 "num-traits",
 "paste",
 "wide",
]

[[package]]
name = "syn"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-xid",
]

[[package]]
name = "typenum"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec"

[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"

[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

[[package]]
name = "wide"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f2548e954f6619da26c140d020e99e59a2ca872a11f1e6250b829e8c96c893"
dependencies = [
 "bytemuck",
 "safe_arch",
]

A  => problem-2f/Cargo.toml +11 -0
@@ 1,11 @@
[package]
name = "problem-2f"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
euclid = "0.22.6"
rand = "0.8.4"
nalgebra = "0.29.0"

A  => problem-2f/src/main.rs +58 -0
@@ 1,58 @@
use nalgebra::*;
use rand::prelude::*;

fn build(points: [Vector2<f32>; 3]) -> [Vector2<f32>; 7] {
    let p0 = points[0];
    let p6 = points[2];
    let mut rng = rand::thread_rng();
    let ratio: f32 = rng.gen_range(0.4..0.8);
    let mut p1 = ratio * (points[0] - points[1]);
    let mut p5 = ratio * (points[2] - points[1]);
    let n1 = p1.norm();
    let n5 = p5.norm();
    if n1 > n5 {
        p1 = p1 * (n5 / n1);
    } else {
        p5 = p5 * (n1 / n5);
    }
    p1 += points[1];
    p5 += points[1];
    let p2 = (p1 + points[1]) / 2.0;
    let p4 = (p5 + points[1]) / 2.0;
    let p3 = (p2 + p4) / 2.0;
    [p0, p1, p2, p3, p4, p5, p6]
}

fn main() {
    let b = [
        Vector2::<f32>::new(0.5, 2.0),
        Vector2::<f32>::new(8.7, 5.0),
        Vector2::<f32>::new(11.0, 1.0),
    ];
    let p = build(b);
    let mut s: Vec<String> = p
        .iter()
        .map(|p| String::from(format!("({},{})", p.x, p.y)))
        .collect();
    s.push(String::from(format!("({},{})", b[1].x, b[1].y)));
    println!(
        "
                \\draw[color=gray] {0} -- {1} -- {2} -- {7} -- {4} -- {5} -- {6};
                \\draw[color=gray] {2} -- {4};
                \\draw[thick] {0} .. controls {1} and {2} .. {3};
                \\draw[thick] {3} .. controls {4} and {5} .. {6};
                \\foreach \\p/\\ptext/\\o in {{
                    {0}/{{$B_0$,$P_0$}}/{{below}},
                    {1}/{{$P_1$}}/{{above left}},
                    {2}/$P_2$/{{above left}},
                    {7}/{{$B_1$}}/above,
                    {3}/{{$P_3$}}/above,
                    {4}/{{$P_4$}}/{{above right}},
                    {5}/{{$P_5$}}/{{above right}},
                    {6}/{{$B_2$,$P_6$}}/{{above right}}}} {{
                    \\draw[fill=black] \\p circle (2pt) node[\\o] {{\\ptext}};
                }}
    ",
        s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]
    );
}

A  => versions/asgn3-001.pdf +0 -0
A  => versions/asgn3-002.pdf +0 -0
A  => versions/asgn3-003.pdf +0 -0
A  => versions/asgn3-004.pdf +0 -0
A  => versions/asgn3-005.pdf +0 -0