430 likes | 620 Views
CSC 308 – Graphics Programming. Curves Information modified from Ferguson’s “Computer Graphics via Java”, and “Principles of Three-Dimensional Animation, Third Ed.” by M. O’Rourke. Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC. First, a few announcements….
E N D
CSC 308 – Graphics Programming Curves Information modified from Ferguson’s “Computer Graphics via Java”, and “Principles of Three-Dimensional Animation, Third Ed.” by M. O’Rourke Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC
First, a few announcements… • Program Questions? • REMINDER: Midterm next Wednesday, 10/4 • What’s on it? • Review class notes and… • Shirley, Ch. 1 • Shirley, Ch. 2 • Shirley, Ch. 6.1 • Shirley, Ch. 15 • Java Coding Graphics Related Questions
What are we doing today? We’ve been working in the world of straight lines – this not being particularly natural, let’s take a look at what curves can do!
Types of Lines • Lines can be straight or curved • Differences between these? • Mathematical description • Behavior as they are used to model • Type of structures (2d and 3d) that are created • Visual appearence
Straight Lines • No curvature • Defined by 2 endpoints • Has slope, does not have change of angularity • Often called “Polygonal Lines”
Curved Lines • Important in the real world, esp. the natural world • Can be 2d or 3d • Approaches: • Linear approximation • Splines
Linear Approximation • aka Polyline technique • Curves are represented as a series of points – “Dot to Dot” • First degree curve
Linear Approximation • Advantages? • Disadvantages?
Linear Approximation • Advantages: SIMPLE • Disadvantages: • Awkward to edit • Number of points required for good approximation may be large • Curve is never truly smooth
Splines The term “spline” comes from the shipping era, when ships were made from wood. To build a plank of wood that would conform to the ship’s hull, shipbuilders would force the wooden plank between several fixed posts, called “ducks.” The result was a curved plank called a “spline.” The placement of the ducks determined how much curvature the finished spline would have.
Splines Computer graphics version: • Curve = wood • Control points = ducks • Hull = straight lines that connect the control points
Splines • Do they clear up the disadvantages of linear approximation? • (the answer is yes!) • How?
Splines • Easy to reshape – pulling a single control point can modify an entire section of the curve and do so smoothly
Splines • Truly, mathematically curved – no matter how close you “zoom” in, the curve doesn’t fall apart
Splines • Programming representation is compact and efficient – a handful of control points define the entire curve.
Categories of Splines • Interpolating • Approximating
Interpolating Splines • Spline passes through each of the control points
Interpolating Splines • Advantages? • Disadvantages?
Interpolating Splines • Advantages? • Disadvantages? There is a direct relationship between the control points and the final curve.
Interpolating Splines Cardinal Spline – type of interpolating spline where the curve passes through all the control points except the first and last; 2nd degree curve.
Approximating Splines Spline passes near but not through the control points Examples: B-spline (Basis Spline) Bezier Spline
B-Spline • Curve begins at approximately the 2nd control point and ends at approximately the 2nd to last control point • Third degree curve
Bezier Spline • Defined by 4 control points • Contains tangent vectors at its ends to direct the curve • Can increase by attaching more segments • Third degree curve
NURBS • Non-Uniform Rational B-Splines • Shares features of previous splines: • Goes through first and last control points (interpolating splines) • Does not go through intermediate control points (approximating splines) • Has a set of “edit points” or “knots” that lie exactly on the curve in addition to control points • Edit control points if need curve to be smooth • Edit knots if you need the curve in an exact position
Implementation of Curves in Java? • Let’s begin by defining the knots (some points along the curve), and then calculate the other points inbetween.
Polynomial parametric eqns • To represent a straight line - linear parametric equation (i.e. one where the highest power of t was 1). • For curves, we need polynomial equations. Why? Because the graphs of polynomial equations wiggle!
Cubic parametic eqns The general form of a cubic parametric equation (in t) is: xt = a0 + a1t + a2t2 + a3t3 similarly for y yt = b0 + b1t + b2t2 + b3t3
Why focus on cubic equations? • Quadratic equations are not usually “wiggly” enough • Higher powers are usually too “wiggly” and require much more computation.
How do we make it “wiggle” where we want it to? • Think about those (just consider the x eqn): • xt = a0 + a1t + a2t2 + a3t3 • We want xt • We control t • We therefore need values for a0 , a1, a2& a3 • Likewise for y
Ni+1 Ni Si+1 Si Ni-1 Obtaining a0, a1, a2 & a3 • How do we make it wiggle where we want? • Consider two contiguous segments: Si and Si+1
Eqn “A” • Each segment is represented by a separate cubic parametric eqns (only x shown): • In Si • In Si+1 • Consider what happens at Ni
3 Important Points: • The segments meet… • The last x and y values in Si are equal to the first x and y values in Si+1 • …the join is continuous… • The gradient at the end of Si is equal to the gradient at the start of Si+1 • ….and smooth • The gradient of the gradient at the end of Si is equal to the gradient of the gradient at the start of Si+1
1. The lines meet Thus: • At Ni in segment Si: t=1 • At Ni in segment Si+1: t=0 so
2. The joint is continuous Gradient at the end of segment Si = gradient at the beginning of Si+1. What is the gradient? To find it… • First differentiate eqn “A” w.r.t. t (giving eqn “B”)
2. The joint is continuous • First differentiate eqn “A” w.r.t. t (giving eqn “B”)
2. The joint is continuous • Again consider what happens at Ni • At Ni in segment Si: t=1 • At Ni in segment Si+1: t=0
3. The joint is smooth • Differentiate (eqn “B”) w.r.t. t
3. The joint is smooth • Again consider what happens at Ni • At Ni in segment Si: t=1 • At Ni in segment Si+1: t=0
Final boundary condition • Currently have 3 eqns in 4 unknowns • Need one more boundary condition: • From endpoints of whole line • Fixed gradient • give a value for end(s) of curve • Free end • 2nd derivative is 0 • Contour • Ends of line meet (loop) – gradient at 1st point = gradient at last point
Solve set of simultaneous eqns • To find the values of a0…a3 for each segment • Gaussian elimination x= 2(0.5-t)(1-t)xNi-1 + 4t(1-t)xNi + 2t(t-0.5)xNi+1 • Similarly for y
What have we achieved? • If we know three points on a curve we can calculate any others • Consider a curve 3 points at a time (red ones) • Interpolate to generate sufficient intermediate points (white) • Join with short straight lines