Bezier Curves
Overview
Bezier
curves are parametrically defined by a set of control points (p0
through pn) that determine the shape of the curve. You can take
advantage of this fact by deriving a Bezier class from
the VariableShape class, as you did for Polygons in the basic Lab 4.
The (x,y) coordinates of any point on a Bezier curve are determined by
evaluation of the parametric equations for the curve (which will be covered in
lecture). From the starting point of the curve to the endpoint, the
parameter u varies from 0 to 1. For u=0, the coordinates of the curve are p0,
and for u=1, the coordinates of the curve are pn. These are the only
points on the curve that coincide with any control points; the remaining
interior of the curve does not pass through any of the remaining control points
p1 through pn-1. For examples, see the sample curves in
the image to the right.
Implementation
As with polygons, you will need to create and store a series of points for each
Bezier object. Your VariableShape class should already
be able to do this for you; there should be a data member in VariableShape that
can hold this data. Where your previous Polygon class (which you derived from
VariableShape) interpreted this data as vertices, your Bezier class will
interpret this data as the control points for the curve.
The Bezier class should consequently be very
similar to your Polygon class; the main difference should be in the
Bezier ::Draw(...) function, where you render the
curve. You will render (i.e. draw) the curve by using the methods provided in a
partially-implemented Bezier class, which you can download
here. These methods
implement the parametric equations for Bezier curves of various order. (Note
that a QPointArray data format is required by the predefined methods of the
Bézier class) You must implement the rest of the class, including the virtual
Draw() method.
The getPoint() method will be the workhorse of the Draw() method. To render
the curve, your Draw() method must repeatedly call the getPoint() method in
order to evaluate the coordinates of points along the curve. Your Draw() method
must then draw a series of straight-line segments (using Qt's line drawing
methods) between these points, created a faceted approximation (an alias;
actually a polyline) to the theoretical curve. As you compute more points, the
faceted polyline will more closely approximate the true curve.
Detail Requirements:
- Add a Bezier Curve Mode button (example above) to your GUI that puts
your application in curve mode.
- Derive the Bezier class from VariableShape; use the behavior
encapsulated in VariableShape to manage the control point data.
- Similar to your polygon class from last week, much of the varshape
code will can be used as-is, without overriding. The main difference
from your polygon class will be in the bezier::Draw(...) function.
- The number of facets used to render the curve mjst be a Bezier
constructor parameter that defaults to 100.
- Optimized functions for Bézier curves with 3 or 4 control point
are provided, along with a general function that works for 2 or more
control points. You must use the optimized versions when you have
only 3 or 4 control points.
- A Bézier curve with 2 control points degenerates into a line,
while a Bézier curve with 1 control point degenerates into a point.
These cases should be handled optimally by your Draw function.
- Implement a mouse interface for drawing Bézier curves similar to your
Polygon drawing interface, where the curve definition is terminated with a
mouse double-click..
- It is NOT REQUIRED that you implement a command line
interface for drawing Bézier curves.
- The curve must exhibit the attributes (such as color) that the Bezier
class inherits from VariableShape.
- Your Draw method should also draw a polyline between the control points
that define the Bezier curve, so that your output is similar to that shown
above. The polylines should be drawn as the control points are being
entered, but the curve should not be displayed until the entry of control
points is terminated (with the double-click).
.