The purpose of this assignment is to do a mini-project on modeling with spline curves. I used the de Casteljau Algorithm, and variations of it, to draw Bezier curves and B-Splines. The result program is a 2D curve editor.
README file:
WorkingScene::drag(int x, int y): this is a function that takes in the coordinates of the mouse and normalize them into [0, 1]. Then I call the function moveActivePoint() of theOnlyCurve to check whether the active point is dragged; if yes, then the active point is moved to a new location. After that, I save the coordinates to oldX and oldY and redraw the scene.
WorkingScene::mouse(int button, int state, int x, int y): this function will take in the information of the mouse. If the left button is clicked, it will call updateActivePoint() of theOnlyCurve to check whether the mouse clicks on the new point or not; it yes, then it will update the new point to active point; otherwise, there is no active point and isactive is set to false. Then, I call the function addPoint(newX, newY) to add new point if there is no active point, ie. the mouse is clicked in an empty area. After that, I save the coordinates to oldX and oldY, and redraw the scene.
Bezier::draw(int levelOfDetail):
Connect all the dots of the curve to create the green straight lines
If the degree is larger than 1, start drawing curve. I use a vector to store all new points that are interpolated from the control points so that I can call drawLine() function to connect all of them and create the blue curve.
The outer for loop goes from 0 to the levelOfDetail. For each value of detail j, I find the u value by j/levelOfDetail and then apply the deCasteljau to find the corresponding point.
Connect all the new points to create the blue curve.
Bspline::draw(int levelOfDetail):
Connect all the dots of the curve to create the green straight lines
Since the curve only appears when there are more than 3 control points, if there are more than 3 points, loop through the list of control points from the beginning to the end of the list of points (except the last 3 points since we need 4 points to create a segment)
For each set of 4 points, call Bspline::drawSegment(Pvector::iterator p1, Pvector::iterator p2, Pvector::iterator p3, Pvector::iterator p4, int levelOfDetail)
Bspline::drawSegment(Pvector::iterator p1, Pvector::iterator p2, Pvector::iterator p3, Pvector::iterator p4, int levelOfDetail):
Since we only have 4 points, the degree is 3
Use the 4x4 formula matrix for B-spline to find the new point and push back to the vector of points that we need to draw curve
Connect all the new points to create the blue curve.
Bezier2 (using Recursion): accumulateNextLevel(Pvector* accum, Pvector pts): This function takes an empty vector of points, accum. It also takes a set of control points, pts, and fills accum with. The control points that correspond to the next level of detail.
Bezier2::draw(int levelOfDetail):
If the levelOfDetail <= 1 and the number of points <= 2 then call the function drawLine() to create the blue curve
Otherwise, call the accumulateNextLevel() function to find the interpolate points; then create the left curve with half of the list of the accum and the right curve with the other half of the list of the accum; call left.draw() and right.draw() recursively with the decreased levelOfDetail.