CSE 477/598 Introduction to Geometric Modeling
Fall 2015 -- Mock Midterm
Instructor: Gerald Farin

NOTE: To see solutions, select cell after questions and choose Cell->Cell Properties->Open

Linear interpolation:
Suppose the point x lies between the red point p and the blue point q in the ratio 1:3.
a) Sketch x relative to p and q
b) Express this relationship in terms of linear interpolation

p = {0,0};
q = {1,0};
linesegplot = Graphics[{Thick, Black, Line[{p,q}],Red,PointSize[0.08], Point[p],Blue, PointSize[0.08], Point[q]}]

x = (3/4)*p + (1/4)*q;
xplot = Graphics[{ Black, PointSize[0.08], Point[x]}];

Print["p = ",p,"    q = ",q];
Print["x = ",x];
Show[xplot, linesegplot]

Barycentric Coordinates:
The given triangle is defined by vertices p0, p1, p2 colored red, green, and blue, respectively.
Sketch and label the points with the following barycentric coordinates.
r ≃ (1/2, 0, 1/2) 
s ≃ (0, 0, 1)
t ≃ (1/3, 1/3, 1/3)

p0 = {0,0};
p1 = {3,1};
p2 = {0,1};
tplot = Graphics[{Thick,Line[{p0,p1,p2,p0}],PointSize[0.08], Red,Point[p0],Green,Point[p1],Blue,Point[p2]}]

r = 0.5*p0 + 0*p1 + 0.5*p2;
s = 0*p0 + 0*p1 + 1 *p2;
o3 = 1.0/3.0;
t = o3*p0 + o3*p1 + o3*p2;
baryplot = Graphics[{PointSize[0.05], Yellow,Point[r],Magenta,Point[s],Cyan,Point[t]}];
Print["p0 = ",p0,"    p1=",p1,"    p2 =",p2];
Print["r = ",r,"  is yellow"];
Print["s = ",s," is magenta"];
Print["t = ",t," is cyan"];
 
Show[tplot, baryplot]

Evaluation:
For the Bezier points in the figure, sketch the curve and the points generated by the de Casteljau algorithm for t = 1/4.

bpts = {{0,0}, {1,1}, {2,1}, {3,0}};
bpolyplot = Graphics[{Thick, Black, Line[bpts], PointSize[0.03],Point[bpts]}]

curve = BezierFunction[bpts];
curveplot = Graphics[ParametricPlot[curve[u],{u,0,1}, PlotStyle->Directive[Thick,Blue]]];
Show[bpolyplot, curveplot]

tval = 0.25;
b10 = (1-tval)*bpts[[1]] + tval*bpts[[2]];
b11 = (1-tval)*bpts[[2]] + tval*bpts[[3]];
b12 = (1-tval)*bpts[[3]] + tval*bpts[[4]];
intermbez1plot = Graphics[{Red,Thin, Line[{b10,b11,b12}], PointSize[0.03],Point[{b10,b11,b12}]}];

b20 =(1-tval)*b10 + tval*b11;
b21 = (1-tval)*b11 + tval*b12;
intermbez2plot = Graphics[{Green,Thin, Line[{b20,b21}],PointSize[0.03],Point[{b20,b21}]}];

b30 = (1-tval)*b20 + tval*b21;
intermbez3plot = Graphics[{Blue,PointSize[0.03],Point[b30]}];

Show[bpolyplot, curveplot,intermbez1plot,intermbez2plot,intermbez3plot]

Subdivision:
For the curve in the previous problem, sketch and label the control points for the “right” half of the curve after subdivision at t = 1/4.

subdivplot = Graphics[{Yellow, PointSize[0.06],Point[{bpts[[4]], b12, b21, b30}]}];

subdivcurve = BezierFunction[{bpts[[4]], b12, b21, b30}];
subdivcurveplot = Graphics[ParametricPlot[subdivcurve[u],{u,0,1}, PlotStyle->Directive[Thick,Yellow]]];


Show[subdivplot,bpolyplot, curveplot,intermbez1plot,intermbez2plot,intermbez3plot,subdivcurveplot]

Derivatives:
For the Bezier polygon above, at t=0
sketch the first derivative scaled by 1/3 
sketch the second derivative scaled by 1/6

firstdir = bpts[[2]] - bpts[[1]];
seconddir = bpts[[3]] - 2.0*bpts[[2]] + bpts[[1]];
deriv1plot = Graphics[{Magenta,Thick, Arrow[{bpts[[1]], bpts[[1]] + firstdir}]}];
deriv2plot = Graphics[{Cyan,Thick, Arrow[{bpts[[1]], bpts[[1]]+seconddir}]}];
deriv2plot2 = Graphics[{RGBColor[0.7,0.7,0.7],Thick, Arrow[{bpts[[2]], bpts[[2]]+seconddir}], Line[{bpts[[1]],bpts[[2]]+seconddir,bpts[[3]]}]}];
Show[bpolyplot, curveplot,deriv1plot,deriv2plot,deriv2plot2]

Degree elevation:
For the polygon below, sketch the polygon for the degree elevated curve.
Label the control point ratios 
What do we mean by saying that “degree elevation converges”?

q0 = {0,0};
q1 = {1,1};
q2 = {3,1};
qpolyplot = Graphics[{Thick, Black, Line[{q0,q1,q2}], PointSize[0.03],Point[{q0,q1,q2}]}]

c0 = q0;
c1 = (1/3)q0 + (2/3)q1;
c2 = (2/3)q1 + (1/3)q2;
c3 = q2;
degelevpolyplot = Graphics[{Thick, Red, Line[{c0,c1,c2,c3}], PointSize[0.03],Point[{c0,c1,c2,c3}]}];
Print["The ratios are 2:1 and 1:2, respectively"];
Show[{qpolyplot,degelevpolyplot}]
Print["Degree elevation converges: as we repeatedly degree elevate, the refined polygons move closer to the curve."];

What is the parametric representation [x(t), y(t)]^T  for the function y = x^3?
Sketch the Bezier points for this function over the interval [0,1].

Print["The parametric representation is [t, t^3]^T"];
Print["The Bezier points must be evenly spaced."];
Print["Since t^3 is a Bernstein polynomial (B^3_3), we can easily construct the Bezier points."];
cpts = {{0,0},{(1/3),0},{(2/3),0},{1,1}};

cptspolyplot = Graphics[{Thick, Black, Line[cpts], PointSize[0.03],Point[cpts]}];

cptscurve = BezierFunction[cpts];
cptscurveplot = Graphics[ParametricPlot[cptscurve[u],{u,0,1}, PlotStyle->Directive[Thick,Blue]]];
Show[cptspolyplot, cptscurveplot]

Print["Now plot [t, t^3]^T over the plot to check they match"];
checkplot = ParametricPlot[{t,t^3},{t,0,1}, PlotStyle->Red];
Show[cptspolyplot, cptscurveplot, checkplot]

Interpolation:
Given two points and parameter pairs,
(p0, 0) and (p1, 1),
write down the linear system to find the interpolating Bezier curve.
(Give numerical values for the Vandermonde matrix.)

The linear system:
p0 = B^1_0(0) b_0 + B^1_1(0) b_1
p1 = B^1_0(1) b_0 + B^1_1(1) b_1
for unknown b_0 and b_1
Thus the Vandermonde matrix is
1 0
0 1

(* control points for B^1_0(t) *)
bb0 = {{0,1},{1,0}};
(* control points for B^1_1(t) *)
bb1 = {{0,0},{1,1}};

bbpolyplot = Graphics[{Thick, Black, Line[bb0], Line[bb1],PointSize[0.03],Point[bb0],Point[bb1]}];

bbcurve0 = BezierFunction[bb0];
bbcurve1 = BezierFunction[bb1];
bbcurveplot = Graphics[ParametricPlot[{bbcurve0[u], bbcurve1[u]},{u,0,1}, PlotStyle->Directive[Thick,Red]]];
Show[{bbpolyplot,bbcurveplot}]