Title: Linear Interpolation
1Linear Interpolation
- Applying weighted averages to some graphics
problems animations and curve-drawing
2What does between mean?
B1
The green point G lies between the two blue
points B1 and B2 .
P
G
B2
The pink point P does NOT lie between the
two blue points B1 and B2.
3Using a weighted average
- Suppose (x1,y1) and (x2,y2) are points
- The point located half-way in-between
is midpoint (½)(x1,y1) (½)(x2,y2) - Its the average of (x1,y1) and (x2,y2)
- Heres another point on the line-segment that
lies between (x1,y1) and (x2,y2) (x,y)
(¼)(x1,y1) (¾)(x2,y2) - Its a weighted average of the endpoints
4The generalization
- Let B1 (x1,y1) and B2 (x2,y2) be the two
endpoints of a line-segment. Suppose w1 and
w2 are weights (i.e., neither is negative, and
their sum equals 1). Then the point P w1B1
w2B2 is called a weighted average of B1 and
B2 , and P will be located in-between B1 and
B2. - Here P is obtained by linear interpolation
5Describing a line-segment
- Mathematical description of line-segments
- Let B1 (x1,y1) and B2 (x2,y2) be the two
end-points of a given line-segment - Let t be a real number whose value can vary
continuously, from 0.0 to 1.0 - Then point P (1-t)B1 tB2 will vary
continuously over the entire line-segment,
starting at B1 (when t0.0) and ending up at B2
(when t1.0)
6Animating a line-segment
final position
initial position
in-between positions
7The programming idea
- We only need to specify the segments two
endpoints at the start and the finish - As the segment moves all its intermediate
endpoint locations are then calculated as linear
interpolations (weighted averages) - This idea can be simultaneously applied to lots
of different line-segments (e.g., to all the
sides of a polygon, or to all the edges in a
wire-frame model)
8The polyline structure
- typedef struct float x, y point_t
- typedef struct int numverts
- point_t vert MAXVERT polyline_t
- // declare two polylines (for start and finish)
- // and a variable polyline (for in-betweens)
- tweeni.x (1-t)B1i.x tB2i.x
- tweeni.y (1-t)B1i.y tB2i.y
9The tweening.cpp demo
- We illustrate this idea for animating simple
polygons, using random-numbers for the
coordinates of the starting vertices and the
ending vertices - We use linear interpolation to calculate the
sequence of the in-between vertices - We use Bresenhams line-drawing method to
connect-the-dots at each stage
10Stick man
11Drawing curves
- Another application of linear interpolation
- We can construct a so-called Bezier curve
- The curve is determined by specifying a small
number of control points - A recursive algorithm is then applied, to
generate locations along a smooth curve - This idea is deCasteljaus algorithm
- Kai Long has written an implementation
12Heres the idea
The same value of t is used for all of these
interpolations
P2
P3
P1
Only the red point actually is drawn
Start with some control points (Here we use
just four of them) Find their weighted averages
P4
13Kais Implementation
- typedef struct double h, v Point
- typedef struct int numcpts
- Point cpts MAXVERT BezierCruve
- // helper function
- void middle( Point p, Point q, Point mid )
- mid.x (p.x q.x)/2 mid.y (p.y q.y)/2
14Labels used in recursion
h1
a
h2
c2
d
c1
b2
b1
if ( very_near( p1, p2 ) // base case
draw_line_segment( p1, p2 ) else // recursion
case recursive_bezier( p1, b1, c1, d )
recursive_bezier( d, c2, b2, p2 )
p2
P1
15In-class exercise
- Can you combine these two applications?
- Create a bezier curve with 4 control-points
- Create another one with 4 control-points
- Construct some in-between Bezier curves by
applying linear-interpolation to pairs of
corresponding control-points - So first curve will morph into second one