home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / oblit / line.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  5.1 KB  |  294 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: line.c,v 4.2 88/07/07 10:57:06 sau Exp $
  9.     $Source: /tmp/mgrsrc/src/oblit/RCS/line.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/src/oblit/RCS/line.c,v $$Revision: 4.2 $";
  12.  
  13. /*  Draw a line 16 bit version */
  14.  
  15. #include "bitmap.h"
  16.  
  17. bit_line(dest, x0, y0, x1, y1, func)
  18. BITMAP *dest;
  19. int x0, y0, x1, y1;
  20. int func;
  21. {
  22.    register int r, rincr, rdecr, d_incr, count;
  23.    register unsigned short bit;
  24.    register unsigned short *dst;
  25.    int temp, dx, dy;
  26.  
  27.    /* clip here */
  28.  
  29. #define TOP        1
  30. #define BOTTOM    2
  31. #define LEFT    4
  32. #define RIGHT    8
  33.  
  34. #define crossings(x,y) \
  35.       (x<0 ? LEFT : x>= (b->wide) ? RIGHT : 0) + \
  36.       (y < 0 ? TOP : y >=  (b -> high) ? BOTTOM : 0)
  37.  
  38.    {
  39.       register BITMAP *b = dest;
  40.  
  41.       /* The classic clipping algorithm */
  42.  
  43.       int Cross0 = crossings(x0, y0);
  44.       int Cross1 = crossings(x1, y1);
  45.  
  46.       while (Cross0 || Cross1) {
  47.      int Cross, x, y;
  48.      if (Cross0 & Cross1)
  49.         return;
  50.      if (Cross0 != 0)
  51.         Cross = Cross0;
  52.      else
  53.         Cross = Cross1;
  54.      if (Cross & (LEFT | RIGHT)) {
  55.         int edge = (Cross & LEFT) ? 0 : b->wide - 1;
  56.         y = y0 + (y1 - y0) * (edge - x0) / (x1 - x0);
  57.         x = edge;
  58.      }
  59.      else if (Cross & (TOP | BOTTOM)) {
  60.         int edge = (Cross & TOP) ? 0 : b->high - 1;
  61.         x = x0 + (x1 - x0) * (edge - y0) / (y1 - y0);
  62.         y = edge;
  63.      }
  64.      if (Cross == Cross0) {
  65.         x0 = x;
  66.         y0 = y;
  67.         Cross0 = crossings(x, y);
  68.      }
  69.      else {
  70.         x1 = x;
  71.         y1 = y;
  72.         Cross1 = crossings(x, y);
  73.      }
  74.       }
  75.       x0 += b->x0;
  76.       y0 += b->y0;
  77.       x1 += b->x0;
  78.       y1 += b->y0;
  79.    }
  80.  
  81.    /* always left to right */
  82.  
  83.    if (x1 < x0) {
  84.       temp = x1, x1 = x0, x0 = temp;
  85.       temp = y1, y1 = y0, y0 = temp;
  86.    }
  87.    dx = x1 - x0;
  88.    dy = y1 - y0;
  89.    if (dy > 0)
  90.       d_incr = BIT_LINE(dest);
  91.    else
  92.       d_incr = -(BIT_LINE(dest)), dy = -dy;
  93.  
  94.    dst = (x0 >> 4) + y0 * (BIT_LINE(dest)) + (dest->data);    /*-*/
  95.    bit = (0x8000 >> (x0 & 15));
  96.  
  97.    /* */
  98.  
  99.    if (dx > dy)
  100.       switch (OPCODE(func)) {
  101.      case OPCODE(SRC):
  102.      case OPCODE(SRC | DST):
  103.      case OPCODE(SRC | ~DST):
  104.      case OPCODE(~0):
  105.         {
  106.            rincr = (dx - dy) << 1;
  107.            rdecr = -(dy << 1);
  108.            r = dx + rdecr;
  109.            for (count = dx; count >= 0; count--) {
  110.           *dst |= bit;
  111.           if ((bit >>= 1) == 0) {
  112.              bit = 0x8000;
  113.              dst++;
  114.           }
  115.  
  116.           ;
  117.           if (r < 0) {
  118.              dst += d_incr;
  119.              r += rincr;
  120.           }
  121.  
  122.           else {
  123.              r += rdecr;
  124.           }
  125.  
  126.            }
  127.  
  128.         }
  129.  
  130.         ;
  131.         break;
  132.     case OPCODE(~SRC):
  133.     case OPCODE(~(SRC|DST)):
  134.     case OPCODE(DST & ~SRC):
  135.     case OPCODE(0):
  136.         {
  137.            rincr = (dx - dy) << 1;
  138.            rdecr = -(dy << 1);
  139.            r = dx + rdecr;
  140.            for (count = dx; count >= 0; count--) {
  141.           *dst &= ~bit;
  142.           if ((bit >>= 1) == 0) {
  143.              bit = 0x8000;
  144.              dst++;
  145.           }
  146.  
  147.           ;
  148.           if (r < 0) {
  149.              dst += d_incr;
  150.              r += rincr;
  151.           }
  152.  
  153.           else {
  154.              r += rdecr;
  155.           }
  156.  
  157.            }
  158.  
  159.         }
  160.  
  161.         ;
  162.         break;
  163.     case OPCODE(SRC ^ DST):
  164.     case OPCODE(~DST):
  165.     case OPCODE(SRC & ~DST):
  166.     case OPCODE(~(SRC&DST)):
  167.         {
  168.            rincr = (dx - dy) << 1;
  169.            rdecr = -(dy << 1);
  170.            r = dx + rdecr;
  171.            for (count = dx; count >= 0; count--) {
  172.           *dst ^= bit;
  173.           if ((bit >>= 1) == 0) {
  174.              bit = 0x8000;
  175.              dst++;
  176.           }
  177.  
  178.           ;
  179.           if (r < 0) {
  180.              dst += d_incr;
  181.              r += rincr;
  182.           }
  183.  
  184.           else {
  185.              r += rdecr;
  186.           }
  187.  
  188.            }
  189.  
  190.         }
  191.  
  192.         ;
  193.         break;
  194.       }
  195.  
  196.    else
  197.       switch (OPCODE(func)) {
  198.     case OPCODE(SRC):
  199.     case OPCODE(SRC | DST):
  200.     case OPCODE(SRC | ~DST):
  201.     case OPCODE(~0):
  202.         {
  203.            rincr = (dy - dx) << 1;
  204.            rdecr = -(dx << 1);
  205.            r = dy + rdecr;
  206.            for (count = dy; count >= 0; count--) {
  207.           *dst |= bit;
  208.           dst += d_incr;
  209.           if (r < 0) {
  210.              if ((bit >>= 1) == 0) {
  211.             bit = 0x8000;
  212.             dst++;
  213.              }
  214.  
  215.              ;
  216.              r += rincr;
  217.           }
  218.  
  219.           else {
  220.              r += rdecr;
  221.           }
  222.  
  223.            }
  224.  
  225.         }
  226.  
  227.         ;
  228.         break;
  229.     case OPCODE(~SRC):
  230.     case OPCODE(~(SRC|DST)):
  231.     case OPCODE(DST & ~SRC):
  232.     case OPCODE(0):
  233.         {
  234.            rincr = (dy - dx) << 1;
  235.            rdecr = -(dx << 1);
  236.            r = dy + rdecr;
  237.            for (count = dy; count >= 0; count--) {
  238.           *dst &= ~bit;
  239.           dst += d_incr;
  240.           if (r < 0) {
  241.              if ((bit >>= 1) == 0) {
  242.             bit = 0x8000;
  243.             dst++;
  244.              }
  245.  
  246.              ;
  247.              r += rincr;
  248.           }
  249.  
  250.           else {
  251.              r += rdecr;
  252.           }
  253.  
  254.            }
  255.  
  256.         }
  257.  
  258.         ;
  259.         break;
  260.     case OPCODE(SRC ^ DST):
  261.     case OPCODE(~DST):
  262.     case OPCODE(SRC & ~DST):
  263.     case OPCODE(~(SRC&DST)):
  264.         {
  265.            rincr = (dy - dx) << 1;
  266.            rdecr = -(dx << 1);
  267.            r = dy + rdecr;
  268.            for (count = dy; count >= 0; count--) {
  269.           *dst ^= bit;
  270.           dst += d_incr;
  271.           if (r < 0) {
  272.              if ((bit >>= 1) == 0) {
  273.             bit = 0x8000;
  274.             dst++;
  275.              }
  276.  
  277.              ;
  278.              r += rincr;
  279.           }
  280.  
  281.           else {
  282.              r += rdecr;
  283.           }
  284.  
  285.            }
  286.  
  287.         }
  288.  
  289.         ;
  290.         break;
  291.       }
  292.  
  293. }
  294.