Assignment 06: Submit Pair Preferecnes Today!
transition
propertysetInterval
window.requestAnimationFrame
Input:
Output:
X
sorted by $x$-coordinatestk
a stack, initially storing first two points in X
For each remaining C
in X
:
stk.size() == 1
, stk.push(C)
A
and B
are top two elements in stk
ABC
is not a right turn and stk.size() > 1
stk.pop()
, update A
, B
stk.push(C)
When Graham’s Scan completes, stk
stores the points along the upper boundary of the convex hull of $X$.
Why?
Must show:
stk
make only right turns.stk
If there are $n$ points, what is the running time of Graham’s scan?
X
sorted by $x$-coordinatestk
a stack, initially storing first two points in X
For each remaining C
in X
:
stk.size() == 1
, stk.push(C)
A
and B
are top two elements in stk
ABC
is not a right turn and stk.size() > 1
stk.pop()
, update A
, B
stk.push(C)
How to find the lower boundary of $CH(X)$?
Make an interactive visualization for Graham’s scan algorithm
Complete description coming soon!
Demo!
lec10-dfs-animated.zip
GraphVisualizer
changes
overlayGroup
<g></g>
element for “group”addOverlayVertex(vtx)
moveOverlayVertex(vtx1, vtx2)
removeOverlayVertex(vtx)
Dfs
changes
cur
vertexGoal 1. Show motion of highlighted vertex
For this animation:
circle
object
cx
and cy
changeSuch things can be animated with CSS!
overlay-vertex
/* list the properties to apply transition to */
transition-property: cx, cy;
/* how long should transition last?*/
transition-duration: 500ms;
/* how long
/* some options: ease (default), ease-in, ease-out, ease-in-out, linear*/
transition-timing-function: ease;
/* should we delay the start of the transition? */
transition-delay: 500ms;
Goal 2. Animate an entire execution of DFS without manually stepping through.
setInterval(method, time, arg1, ...)
method:
method
with arguments arg1, ...
every time
ms, until eternityInterval
clearInterval(id)
methodThe method
is called a callback method
Repeatedly call step()
function until algorithm terminates
I used three methods:
animate
to start the animationanimateStep
to decide what to do for a single animation step
step()
if the algorithm isn’t donestopAnimation
to stop the animation this.animate = function () {
if (this.curAnimation == null) {
this.start();
this.curAnimation = setInterval(() => {
this.animateStep();
}, 1000);
}
}
this.animateStep = function () {
if (this.active.length > 0) {
console.log("taking a step from vertex "
+ this.cur.id);
this.step();
} else {
this.stopAnimation();
}
}
this.stopAnimation = function () {
clearInterval(this.curAnimation);
this.curAnimation = null;
console.log("animation completed");
}
lec10-bouncing-ball.zip
Goal. Animate a bunch of dots that bounce around the screen!
window.requestAnimationFrame(callback)
:
requestAnimationFrame
for each frame
callback
recursively call the function making the requestEach dot
executes this code:
this.animate = function () {
this.updateLocation(this.cx + this.vx, this.cy + this.vy);
if(this.cx <= 0 || this.cx >= WIDTH) {
this.vx = -this.vx;
}
if(this.cy <= 0 || this.cy >= HEIGHT) {
this.vy = -this.vy;
}
window.requestAnimationFrame(() => this.animate());
}