home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / x / x11p-13.zip / do_lines.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  7KB  |  288 lines

  1. /*****************************************************************************
  2. Copyright 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of Digital not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21.  
  22. ******************************************************************************/
  23.  
  24. #include "x11perf.h"
  25.  
  26. static XPoint   *points;
  27. static GC       pgc;
  28.  
  29. int InitLines(xp, p, reps)
  30.     XParms  xp;
  31.     Parms   p;
  32.     int     reps;
  33. {
  34.     int size;
  35.     int half;        /* Half of width if wide line                */
  36.     int i;
  37.     int     rows;       /* Number of rows filled in current column      */
  38.     int x, y;        /* Next point                    */
  39.     int xdir, ydir;    /* Which direction x, y are going        */
  40.     int bigxdir;    
  41.     int x1, y1;        /* offsets to compute next point from current    */
  42.     int phase;        /* how far into 0..4*(size_1) we are        */
  43.     float phasef;       /* how far we are in real numbers        */
  44.     float phaseinc;     /* how much to increment phasef at each segment */
  45.     int size4;        /* 4 * size                    */
  46.  
  47.     pgc = xp->fggc;
  48.  
  49.     size = p->special;
  50.     size4 = 4 * (size+1);
  51.     half = (size + 19) / 20;
  52.  
  53.     points = (XPoint *)malloc((p->objects+1) * sizeof(XPoint));
  54.  
  55.     /* All this x, x1, x1inc, etc. stuff is to create a pattern that
  56.     (1) scans down the screen vertically
  57.  
  58.     (2) rotates the endpoints through all possible orientations
  59.  
  60.     (3) bounces off bottom and top of window as needed
  61.  
  62.     (4) moves left or right at each bounce to make things less boring
  63.     */
  64.  
  65.     x     = half;
  66.     y     = half;
  67.     xdir  = 1;
  68.     ydir  = 1;
  69.     bigxdir = 1;
  70.     phasef = 0.0;
  71.     phaseinc = ((float)size4) / ((float)p->objects);
  72.     if (phaseinc < 1.0) phaseinc = 1.0;
  73.     rows = 0;
  74.  
  75.     points[0].x = x;
  76.     points[0].y = y;
  77.  
  78.     for (i = 1; i != (p->objects+1); i++) {    
  79.     phase = phasef;
  80.     switch (phase / (size+1)) {
  81.     case 0:
  82.         x1 = size;            
  83.         y1 = phase;
  84.         break;
  85.  
  86.     case 1:
  87.         x1 = size - phase % (size+1);
  88.         y1 = size;
  89.         break;
  90.  
  91.     case 2:
  92.         x1 = phase % (size+1);
  93.         y1 = size;
  94.         break;
  95.  
  96.     case 3:
  97.         x1 = size;
  98.         y1 = size - phase % (size+1);
  99.         break;
  100.     } /* end switch */
  101.  
  102.     /* Move down or up the screen */
  103.     y += (ydir * y1);
  104.  
  105.     /* If off either top or bottom, backtrack to previous position and go
  106.        the other way instead.  Also move in bigxdir if not already. */
  107.     rows++;
  108.     if (y < half || y >= (HEIGHT-half) || rows > MAXROWS) {
  109.         rows = 0;
  110.         if (bigxdir > 0) {
  111.         if (x + size < WIDTH - half) {
  112.             xdir = 1;
  113.         } else {
  114.             bigxdir = -1;
  115.         }
  116.         } else {
  117.         if (x - size > half) {
  118.             xdir = -1;
  119.         } else {
  120.             bigxdir = 1;
  121.         }
  122.         }
  123.         ydir = -ydir;
  124.         y += (2 * ydir * y1);
  125.         /* If still off top or bottom, we can't do the line we want.
  126.            This will happen infrequently if the window is not twice the
  127.            length of the line.  So instead, let's draw a line that puts
  128.            the line after this approximately through the center of the
  129.            window.   Since it is very unlikely that both x and y will
  130.            need such adjustment, line length (in pixels) should not
  131.            change...we just can't get the slope we want for this line. */
  132.         if (y < half) {
  133.         y = (HEIGHT - y1)/2;
  134.         ydir = 1;
  135.         } else if (y > (HEIGHT - half)) {
  136.         y = (HEIGHT + y1)/2;
  137.         ydir = -1;
  138.         }
  139.     }
  140.  
  141.     /* Move x left or right by x1 */
  142.     x += (xdir * x1);
  143.     xdir = -xdir;
  144.     /* Again, if we are off the bottom then we can't really draw the line
  145.        we want.  */
  146.     if (x < half) {
  147.         x = (WIDTH - x1)/2;
  148.         xdir = 1;
  149.     } else if (x > (WIDTH - half)) {
  150.         x = (WIDTH + x1)/2;
  151.         xdir = -1;
  152.     }
  153.     points[i].x = x;
  154.     points[i].y = y;
  155.  
  156.     /* Increment phasef */
  157.     phasef += phaseinc;
  158.     if (phasef >= size4) phasef -= size4;
  159.  
  160.     }
  161.     return reps;
  162. }
  163.  
  164. int InitWideLines(xp, p, reps)
  165.     XParms  xp;
  166.     Parms   p;
  167.     int     reps;
  168. {
  169.     int size;
  170.  
  171.     (void)InitLines(xp, p, reps);
  172.  
  173.     size = p->special;
  174.     XSetLineAttributes(xp->d, xp->bggc, (int) ((size + 9) / 10),
  175.     LineSolid, CapRound, JoinRound);
  176.     XSetLineAttributes(xp->d, xp->fggc, (int) ((size + 9) / 10),
  177.     LineSolid, CapRound, JoinRound);
  178.  
  179.     return reps;
  180. }
  181.  
  182. int InitDashedLines(xp, p, reps)
  183.     XParms  xp;
  184.     Parms   p;
  185.     int     reps;
  186. {
  187.     char dashes[2];
  188.  
  189.     (void)InitLines(xp, p, reps);
  190.  
  191.     /* Modify GCs to draw dashed */
  192.     XSetLineAttributes(xp->d, xp->bggc, 0, LineOnOffDash, CapButt, JoinMiter);
  193.     XSetLineAttributes(xp->d, xp->fggc, 0, LineOnOffDash, CapButt, JoinMiter);
  194.     dashes[0] = 3;   dashes[1] = 2;
  195.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  196.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  197.     return reps;
  198. }
  199.  
  200. int InitWideDashedLines(xp, p, reps)
  201.     XParms  xp;
  202.     Parms   p;
  203.     int     reps;
  204. {
  205.     int        size;
  206.     XGCValues   gcv;
  207.     char    dashes[2];
  208.  
  209.     (void)InitWideLines(xp, p, reps);
  210.     size = p->special;
  211.     size = (size + 9) / 10;
  212.  
  213.     /* Modify GCs to draw dashed */
  214.     dashes[0] = 2*size;   dashes[1] = 2*size;
  215.     gcv.line_style = LineOnOffDash;
  216.     XChangeGC(xp->d, xp->fggc, GCLineStyle, &gcv);
  217.     XChangeGC(xp->d, xp->bggc, GCLineStyle, &gcv);
  218.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  219.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  220.     return reps;
  221. }
  222.  
  223. int InitDoubleDashedLines(xp, p, reps)
  224.     XParms  xp;
  225.     Parms   p;
  226.     int     reps;
  227. {
  228.     char dashes[2];
  229.  
  230.     (void)InitLines(xp, p, reps);
  231.  
  232.     /* Modify GCs to draw dashed */
  233.     XSetLineAttributes(xp->d, xp->bggc, 0, LineDoubleDash, CapButt, JoinMiter);
  234.     XSetLineAttributes(xp->d, xp->fggc, 0, LineDoubleDash, CapButt, JoinMiter);
  235.     dashes[0] = 3;   dashes[1] = 2;
  236.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  237.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  238.     return reps;
  239. }
  240.  
  241. int InitWideDoubleDashedLines(xp, p, reps)
  242.     XParms  xp;
  243.     Parms   p;
  244.     int     reps;
  245. {
  246.     int        size;
  247.     XGCValues   gcv;
  248.     char    dashes[2];
  249.  
  250.     (void)InitWideLines(xp, p, reps);
  251.     size = p->special;
  252.     size = (size + 9) / 10;
  253.  
  254.     /* Modify GCs to draw dashed */
  255.     dashes[0] = 2*size;   dashes[1] = 2*size;
  256.     gcv.line_style = LineDoubleDash;
  257.     XChangeGC(xp->d, xp->fggc, GCLineStyle, &gcv);
  258.     XChangeGC(xp->d, xp->bggc, GCLineStyle, &gcv);
  259.     XSetDashes(xp->d, xp->fggc, 0, dashes, 2);
  260.     XSetDashes(xp->d, xp->bggc, 0, dashes, 2);
  261.     return reps;
  262. }
  263.  
  264. void DoLines(xp, p, reps)
  265.     XParms  xp;
  266.     Parms   p;
  267.     int     reps;
  268. {
  269.     int i;
  270.  
  271.     for (i = 0; i != reps; i++)
  272.     {
  273.         XDrawLines(xp->d, xp->w, pgc, points, p->objects+1, CoordModeOrigin);
  274.         if (pgc == xp->bggc)
  275.             pgc = xp->fggc;
  276.         else
  277.             pgc = xp->bggc;
  278.     }
  279. }
  280.  
  281. void EndLines(xp, p)
  282.     XParms  xp;
  283.     Parms   p;
  284. {
  285.     free(points);
  286. }
  287.  
  288.