home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / VOGLE / EXAMPLES / CURVES.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  4KB  |  187 lines

  1. #include <stdio.h>
  2. #include "vogle.h"
  3.  
  4. /*
  5.  * curve basis types
  6.  */
  7. Matrix    bezier = {
  8.     {-1.0,    3.0,    -3.0,    1.0},
  9.     {3.0,    -6.0,    3.0,    0.0},
  10.     {-3.0,    3.0,    0.0,    0.0},
  11.     {1.0,    0.0,    0.0,    0.0} 
  12. };
  13.  
  14. Matrix    cardinal = {
  15.     {-0.5,    1.5,    -1.5,    0.5},
  16.     {1.0,    -2.5,    2.0,    -0.5},
  17.     {-0.5,    0.0,    0.5,    0.0},
  18.     {0.0,    1.0,    0.0,    0.0}
  19. };
  20.  
  21. Matrix    bspline = {
  22.     {-1.0 / 6.0,    3.0 / 6.0,    -3.0 / 6.0,    1.0 / 6.0},
  23.     {3.0 / 6.0,    -6.0 / 6.0,    3.0 / 6.0,    0.0},
  24.     {-3.0 / 6.0,    0.0,        3.0 / 6.0,    0.0},
  25.     {1.0 / 6.0,    4.0 / 6.0,    1.0 / 6.0,    0.0}    
  26. };
  27.  
  28. /*
  29.  *     Geometry matrix to demonstrate basic spline segments
  30.  */
  31. float   geom1[4][3] = {
  32.     { -180.0, 10.0, 0.0 },
  33.     { -100.0, 110.0, 0.0 },
  34.     { -100.0, -90.0, 0.0 },
  35.     { 0.0, 50.0, 0.0 }
  36. };
  37.  
  38. /*
  39.  *     Geometry matrix to demonstrate overlapping control points to
  40.  *    produce continuous (Well, except for the bezier ones) curves
  41.  *    from spline segments
  42.  */
  43. float    geom2[6][3] = {
  44.     { 200.0, 480.0, 0.0 },
  45.     { 380.0, 180.0, 0.0 },
  46.     { 250.0, 430.0, 0.0 },
  47.     { 100.0, 130.0, 0.0 },
  48.     { 50.0,  280.0, 0.0 },
  49.     { 150.0, 380.0, 0.0 }
  50. };
  51.  
  52. /*
  53.  * using curves
  54.  */
  55. main()
  56. {
  57.     char    dev[20];
  58.     int    i;
  59.  
  60.     fprintf(stderr,"Enter device: ");
  61.  
  62.     gets(dev);
  63.     vinit(dev);
  64.  
  65.     ortho2(-200.0, 400.0, -100.0, 500.0);
  66.  
  67.     color(BLACK);
  68.     clear();
  69.  
  70.     color(YELLOW);
  71.  
  72.     textsize(10.0, 10.0);
  73.  
  74.     /*
  75.      * label the control points in geom1
  76.      */
  77.         for (i = 0; i < 4; i++) {
  78.         move2(geom1[i][0], geom1[i][1]);
  79.         sprintf(dev, "%d", i);
  80.         drawstr(dev);
  81.     }
  82.                                  
  83.     /*
  84.      * label the control points in geom2
  85.      */
  86.     for (i = 0; i < 6; i++) {
  87.         move2(geom2[i][0], geom2[i][1]);
  88.         sprintf(dev, "%d", i);
  89.         drawstr(dev);
  90.     }
  91.  
  92.     /*
  93.      * scale the current font so that 30 of the largest characters
  94.      * in the current font will fit in a region 300 world units wide,
  95.      * 20 high.
  96.      */
  97.     boxfit(300.0, 20.0, 30);
  98.  
  99.     /*
  100.      * set the number of line segments appearing in each curve to 20
  101.      */
  102.     curveprecision(20);
  103.  
  104.     /*
  105.      * copy the bezier basis matrix into the curve basis matrix.
  106.      */
  107.     curvebasis(bezier);
  108.  
  109.     color(RED);
  110.  
  111.     /*
  112.      * draw a curve using the current basis matrix (bezier in this case)
  113.      * and the control points in geom1
  114.      */
  115.     curve(geom1);
  116.  
  117.     move2(70.0, 60.0);
  118.     drawstr("Bezier Curve Segment");
  119.  
  120.     move2(-190.0, 450.0);
  121.     drawstr("Three overlapping Bezier Curves");
  122.  
  123.     /*
  124.      * curven draws overlapping curve segments according to geom2, the
  125.      * number of curve segments drawn is three less than the number of
  126.      * points passed, assuming there are a least four points in the
  127.      * geometry matrix (in this case geom2). This call will draw 3
  128.      * overlapping curve segments in the current basis matrix - still
  129.      * bezier.
  130.      */
  131.     curven(6, geom2);
  132.  
  133.     getkey();
  134.  
  135.     /*
  136.      * load in the cardinal basis matrix
  137.      */
  138.     curvebasis(cardinal);
  139.  
  140.     color(MAGENTA);
  141.  
  142.     move2(70.0, 10.0);
  143.     drawstr("Cardinal Curve Segment");
  144.  
  145.     /*
  146.      * plot out a curve segment using the cardinal basis matrix
  147.      */
  148.     curve(geom1);
  149.  
  150.     move2(-190.0, 400.0);
  151.     drawstr("Three overlapping Cardinal Curves");
  152.  
  153.     /*
  154.      * now draw a bunch of them again.
  155.      */
  156.     curven(6, geom2);
  157.  
  158.     getkey();
  159.  
  160.     /*
  161.      * change the basis matrix again
  162.      */
  163.     curvebasis(bspline);
  164.  
  165.     color(GREEN);
  166.  
  167.     move2(70.0, -40.0);
  168.     drawstr("Bspline Curve Segment");
  169.  
  170.     /*
  171.      * now draw our curve segment in the new basis...
  172.      */
  173.     curve(geom1);
  174.  
  175.     move2(-190.0, 350.0);
  176.     drawstr("Three overlapping Bspline Curves");
  177.  
  178.     /*
  179.      * ...and do some overlapping ones
  180.      */
  181.     curven(6, geom2);
  182.  
  183.     getkey();
  184.  
  185.     vexit();
  186. }
  187.