[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference

curve1.c Example C Language Program

/* Example C Language Program curve1.c */
/*
This program draws 3 curve segments. The "horizontal" one is drawn
with a Bezier basis matrix, the "vertical" one with a Cardinal
basis matrix, and the "diagonal" one with a B-spline basis matrix.
All use the same set of 4 control points, contained in the array
geom1.
Before crv (rcrv) is called, a basis and precision matrix must be
defined.
*/
#include <gl/gl.h>
#include <gl/device.h>
#define BEZIER 1
#define CARDINAL 2
#define BSPLINE 3
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     }
};
Coord geom1[4][3] = {
  { 100.0, 200.0, 0.0},
  { 200.0, 300.0, 0.0},
  { 200.0, 100.0, 0.0},
  { 300.0, 200.0, 0.0}
};
main()
{    
  int dev,val;    
  initialize();    
  while (TRUE) {        
    if (qtest()) {            
      dev = qread(&val);            
      if (dev == ESCKEY) {
        gexit();
        exit(1);
      }
      else if (dev == REDRAW) {
        reshapeviewport();
        drawcurve();
      }
    }
  }
}
initialize()
{    
  int gid;    
  prefposition(200, 600, 100, 500);
  gid = winopen("curve1");    
  qdevice(ESCKEY);
  qdevice(REDRAW);
  qenter(REDRAW,gid);
}
drawcurve()
{    
  int i,xx,yy,x,y;
  color(BLACK);
  clear();
  defbasis(BEZIER,beziermatrix);
    /* define a basis matrix called BEZIER */
  curvebasis(BEZIER);
    /* identify the BEZIER matrix as the current basis matrix */
  curveprecision(20);
    /* set the current precisionto 20 (the curve segment will be
    drawn using 20 line segments) */
  color(RED);
  crv(geom1);
    /* a new curve segment is drawn */
  defbasis(CARDINAL,cardinalmatrix);
    /* a new basis is defined */
  curvebasis(CARDINAL);
    /* the current basis is reset.  note that the curveprecision
    does not have to be restated unless it is to be changed */
  color(BLUE);
  crv(geom1);
    /* a new curve segment is drawn */
  defbasis(BSPLINE,bsplinematrix);
    /* a new basis is defined */
  curvebasis(BSPLINE);
    /* the current basis is reset */
  color(GREEN);
  crv(geom1);
    /* a new curve segment is drawn */
    /* show the control points */
   color(WHITE);
  for ( i = 0 ; i < 4 ; i++ )  {
    for ( xx = -2 ; xx < 2 ; xx++) {
      for ( yy = -2 ; yy < 2 ; yy++) {
        pnt2( geom1[i][0] + (Coord) xx , geom1[i][1] + (Coord) yy);
      }
    }
  }
}
/*Changes:
  - showing the control points (added variables i,xx,yy)
  - the size of the window with
      from prefposition (200,500,100,400); to
      prefposition(200, 600, 100, 500);
   - remove the translate(150.0, 150.0, 0.0); because when the
      window redraws it successively moves the curves away
   - change the colors of the curves from all red to each
      of the primary colors of light
   - added 100.0 to each of the y components of the curve's geomerty
*/

Related Information

The crv subroutine , curvebasis subroutine , curveprecision subroutine , defbasis subroutine .

Drawing Curves in GL3.2 Version 4 for AIX: Programming Concepts.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]