home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / lemming / part02 / lemtick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-05  |  2.4 KB  |  107 lines

  1. /*
  2.  * lemtick.c - perform ticking
  3.  *
  4.  * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
  5.  */
  6.  
  7. #include "lem.h"
  8. #include "lemfont.h"
  9.  
  10. tickalign(x, y)
  11.     int *x, *y;
  12.     {
  13.     if (tickflag && (ticksize < 6))
  14.     {
  15.     tx = 1<<ticksize;
  16.     ty = 1<<ticksize;
  17.     *x = ((*x-txoff+tx/2) / tx) * tx + txoff;
  18.         *y = ((*y-tyoff+ty/2) / ty) * ty + tyoff;
  19.     }
  20.     if (tickflag && (ticksize >= 6))
  21.     {
  22.     int xl, yi, yl, d1, d2, d3, d4;
  23.     tx = 1<<(ticksize-3);
  24.     ty = (tx*14+7)/16;
  25.         yi = ((*y-tyoff)/ty);
  26.         yl = yi*ty + tyoff;
  27.     xl = (((*x-txoff+((yi&0x1)?tx/2:0))/tx)*tx-((yi&0x1)?tx/2:0))+txoff;
  28.     
  29.     d1 = dist(*x,*y,xl       ,yl);
  30.     d2 = dist(*x,*y,xl+tx/2  ,yl+ty);
  31.     d3 = dist(*x,*y,xl+tx    ,yl);
  32.     d4 = dist(*x,*y,xl+3*tx/2,yl+ty);
  33.     if ((d1<=d2) && (d1<=d3) && (d1<=d4)) { *x = xl;        *y = yl; }
  34.     if ((d2<=d1) && (d2<=d3) && (d2<=d4)) { *x = xl+tx/2;   *y = yl+ty; }
  35.     if ((d3<=d1) && (d3<=d2) && (d3<=d4)) { *x = xl+tx;     *y = yl; }
  36.     if ((d4<=d1) && (d4<=d2) && (d4<=d3)) { *x = xl+3*tx/2; *y = yl+ty; }
  37.     }
  38.     }
  39.  
  40. tickset()
  41.     {
  42.     char ch;
  43.     int tmpx, tmpy;
  44.     tickflag = !tickflag;
  45.     if (tickflag)
  46.     {
  47.     msgpost("ticksize[1-5/6-9]: ");
  48.     ch = getstroke();
  49.     msgclear();
  50.     if ((ch < '0') || (ch > '9'))
  51.         {
  52.         tickflag = 0;
  53.         return;
  54.         }
  55.     ticksize = ch - '0';
  56.     tmpx = markon ? markx : 0;
  57.     tmpy = markon ? marky : 0;
  58.         txoff = 0;
  59.         tyoff = 0;
  60.     tickalign(&tmpx, &tmpy);    /* sets tx and ty as well */
  61.     txoff = (markon ? markx : 0) - tmpx;
  62.     tyoff = (markon ? marky : 0) - tmpy;
  63.     while (tyoff > 0) tyoff -= ty;
  64.     while (tyoff < 0) tyoff += ty;
  65.     while (txoff > 0) txoff -= tx;
  66.     while (txoff < 0) txoff += tx;
  67.     }
  68.     tickdraw();
  69.     if (!tickflag)
  70.     {
  71.         txoff = 0;
  72.         tyoff = 0;
  73.     }
  74.     }
  75.     
  76. tickdraw()
  77.     {
  78.     int x, y, xstep, ystep;
  79.     xstep = tx;
  80.     ystep = ty;
  81.     while (xstep<MINTICK) xstep *= 2;
  82.     while (ystep<MINTICK) ystep *= 2;
  83.     if (ticksize < 6)
  84.     {
  85.     for(y = tyoff ; y<screenh; y+=ystep)
  86.         for (x = txoff; x<screenw; x+=xstep)
  87.             {
  88.         drawvec(x, y, x + tickdot, y, tickflag ? TICKONCOL:TICKOFFCOL,
  89.             1,EMPHNONE); /* tickdot sets 0/1 width dot */
  90.         }
  91.     }
  92.     else
  93.         {
  94.     int ylim, iy;
  95.     ylim = screenh/ty;
  96.     y = tyoff;
  97.     for(iy=0; iy<ylim; iy++)
  98.         {
  99.         y += ty;
  100.         for (x = txoff + ((iy&0x1) ? 0 : (tx/2)); x<screenw; x+=tx)
  101.         if ((x>0) && (y>0))
  102.                 drawvec(x, y, x + tickdot, y,
  103.             tickflag ? TICKONCOL:TICKOFFCOL,1,EMPHNONE);
  104.         }
  105.     }
  106.     }
  107.