/* curve2.c
This program demonstrates the use of joined curve segments. It draws three curves. One with a Bezier basis, one with a Cardinal spline basis, and one with a B-spline basis.
The array geom2 contains 6 control points. With the Bezier basis matrix, 3 sets of overlapping control points result in 3 separate curve segments. With the Cardinal spline and B-spline matrices, the same overlapping sets of control points result in 3 joined curve segments. */
#include <gl/gl.h> #include <gl/device.h>
Matrix beziermatrix = { { -1, 3, -3, 1 } , { 3, -6, 3, 0 }, { -3, 3, 0, 0 }, { 1, 0, 0, 0 }};
Matrix cardinalmatrix = { { -0.5, 1.5, -1.5, 0.5 }, { 1.0, -2.5, 2.0, -0.5 }, { -0.5, 0.0, 0.5, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }};
Matrix bsplinematrix = { { -1.0/6.0, 3.0/6.0, -3.0/6.0, 1.0/6.0 }, { 3.0/6.0, -6.0/6.0, 3.0/6.0, 0.0 }, { -3.0/6.0, 0.0, 3.0/6.0, 0.0 }, { 1.0/6.0, 4.0/6.0, 1.0/6.0, 0.0 }};
#define BEZIER 1
#define CARDINAL 2
#define BSPLINE 3
Coord geom2[6][3] = { { 150.0, 400.0, 0.0}, { 350.0, 100.0, 0.0}, { 200.0, 350.0, 0.0}, { 50.0, 0.0, 0.0}, { 0.0, 200.0, 0.0}, { 100.0, 300.0, 0.0}};
main() { int dev,val;
initialize(); while (TRUE) { if (qtest()) { dev = qread(&val); if (dev == ESCKEY) { gexit(); exit(); } else if (dev == REDRAW) { reshapeviewport(); drawcurve(); }
}
}
}
initialize() { int gid;
prefposition(200, 650, 200, 800); gid = winopen("curve2"); winconstraints(); /* Allows resizing */ qdevice(ESCKEY); qdevice(REDRAW); qenter(REDRAW,gid);
}
drawcurve() { short i,xx,yy;
color(BLACK); clear(); pushmatrix(); /* Copy the top matrix */ translate (50.0,100.0,0.0); /* Center the splines */ defbasis(BEZIER,beziermatrix); /* define a basis matrix called BEZIER */ defbasis(CARDINAL,cardinalmatrix); /* a new basis is defined */ defbasis(BSPLINE,bsplinematrix); /* a new basis is defined */ curvebasis(BEZIER); /* the Bezier matrix becomes the current basis */ curveprecision(20); /* the precision is set to 20 */ color(RED); crvn(6, geom2); /* the curvs command called with a Bezier basis causes 3 separate curve segments to be drawn */ curvebasis(CARDINAL); /* the Cardinal basis becomes the current basis */ color(GREEN); crvn(6, geom2); /* the crvs command called with a Cardinal spline basis causes a smooth curve to be drawn */ curvebasis(BSPLINE); /* the B-spline basis becomes the current basis */ color(BLUE); crvn(6, geom2); /* the curvs command called with a B-spline basis causes the smoothest curve to be drawn */
/* show the control points */ color(WHITE); for ( i = 0 ; i < 6 ; i++ ) { for ( xx = -2 ; xx < 2 ; xx++) { for ( yy = -2 ; yy < 2 ; yy++) { pnt2( geom2[i][0] + (Coord) xx , geom2[i][1] + (Coord) yy); } } } popmatrix();
}
/* Changes: - Now the control points are displayed - The window can be resized */
The crvn subroutine .
Drawing Curves in GL3.2 Version 4 for AIX: Programming Concepts.