CS3451. Fall 08. Midterm. NAME: ___SOLUTION__ Grade:______ / 30.
Individual test. Sit sufficiently far from, other students and do not look at other students’ work. Closed books. You can only ha ve one
page (your cheat-sh eet) of notes. Please type and write legibly. Each question is 10 points. You should answer 3 out of any of the
four questions. 24 points and above will correspond to an A at the midterm. You may answer the 4th question for extra credit.
1) Animation: You are given as input a planar polyloop P[] with vertex count n. Each
vertex P[i] (small red dots in the figure) represents the position of a poin t at keyframe
16i. Provide the details of an implementation (Processing code) for animat ing the
cyclic motion of a small disk (larger r ed dot) whose smooth trajectory has 16n frames
(shown as small red and green dots) and interpolates the n original keyfr ames, thus
using 15 intermediate frames (green dots) between each pair of consecutive red
keyframes. Formulate your solution in terms of two procedure: myInit(), which will
be executed once at initialization, and myAnimate(), which will be execu ted for each
frame, as shown b elow. The code for n() and p(), which access the next and previous
indices in P is provided. The code for linear interpolations S(A,t,B) is also provided.
To set a point A to be equal to a point B, use A.setTo(B). P(A) returns a n ew copy of
point A. If you call any other procedure please provide the Processing cod e for it. In
myInit(), you may change the value of n and the content of P[]. You do not need to
render the small red and green dots. MyAnimate must r ender the larger r ed dot for the
current frame f so that the red dot keeps traveling along the smooth loop. Provide
comments to facilitate reading.
pt [] P = new pt[10000]; // declares an array of vertices for the loop
int n; // number of vertices (initially = number of keyframes)
int f=0; // curren t frame number used for animation
void setup() { size(800, 800); setColors();
declarePoints(); // creates all points P[i]
loadPts(); // loads n and P[0]…P[n–1] from file
myInit(); // executed once to set up your animation
}
void draw() { background(121); myAnimate(); }; // repeated at each frame
pt S(pt A, float s, p t B) {return new pt(A.x+s*(B.x-A.x) ,A.y+s*(B.y-A.y)); }; // returns A+sAB
int n(int j) { if (j==n-1) {return (0);} else {return(j+1);} }; // next point in loop
int p(int j) { if (j==0) {return (n-1);} else {return(j-1);} }; // previous point in loop
void myAnimate() { // provide code here for running your animation (includ ing the display of the red dot)
if(f>n-1) f=0; // advan ce f in a cyclic manner
fill(red); P[f++].show(5); // show current frame as a red disk
}
void myInit() { // provide code h ere for setting your animation
for (int r=0; r<=4 ; r++) { // apply 4 four-point refinement steps to P
pt[] Q = new pt [2*n]; // temporary array of vertices twice longer than P
for (int i=0; i<n ; i++) { // for each edge (P[i],P[n(i)]) append 2 consecutive vertices to Q
Q[2*i]=P(P[ i]); // first a copy of P[i]
Q[2*i+1]=P( S( S(P[p(i)],1.125,P[i]) , 0.5 , S(P[n(n(i))],1.125,P[n(i)]) ) ); }; // then the bulged mid-edge point
n*=2; // double the vertex count in P
for (int i=0; i<n ; i++) P[i].setTo(Q[i]); // copy Q b ack to P overwriting previous values
}
} // end of myInit