home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / GRAPH / GLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  2.3 KB  |  103 lines

  1. /* gline.c (emx+gcc) -- Copyright (c) 1987-1993 by Eberhard Mattes */
  2.  
  3. #include <stdlib.h>
  4. #include <graph.h>
  5. #define INCL_VIO
  6. #include <os2emx.h>
  7. #include "graph2.h"
  8.  
  9.  
  10. static int round (int n)
  11. {
  12.   if (n & 1)
  13.     return (n/2+1);
  14.   else
  15.     return (n/2);
  16. }
  17.  
  18.  
  19. static int clip (int x1, int y1, int *x2p, int *y2p)
  20. {
  21.   if (*y2p < _g_clipy0 && y1 >= _g_clipy0)
  22.     {
  23.       *x2p = x1 + round (2 * (_g_clipy0 - y1) * (*x2p - x1) / (*y2p - y1));
  24.       *y2p = _g_clipy0;
  25.     }
  26.   else if (*y2p > _g_clipy1 && y1 <= _g_clipy1)
  27.     {
  28.       *x2p = x1 + round (2 * (_g_clipy1 - y1) * (*x2p - x1) / (*y2p - y1));
  29.       *y2p = _g_clipy1;
  30.     }
  31.   if (*x2p < _g_clipx0 && x1 >= _g_clipx0)
  32.     {
  33.       *y2p = y1 + round (2 * (_g_clipx0 - x1) * (*y2p - y1) / (*x2p - x1));
  34.       *x2p = _g_clipx0;
  35.     }
  36.   else if (*x2p > _g_clipx1 && x1 <= _g_clipx1)
  37.     {
  38.       *y2p = y1 + round (2* (_g_clipx1 - x1) * (*y2p - y1) / (*x2p - x1));
  39.       *x2p = _g_clipx1;
  40.     }
  41.   return (*x2p >= _g_clipx0 && *x2p <= _g_clipx1
  42.           && *y2p >= _g_clipy0 && *y2p <= _g_clipy1);
  43. }
  44.  
  45.  
  46. void g_line (int x0, int y0, int x1,int y1, int color)
  47. {
  48.   if (clip (x0, y0, &x1, &y1) && clip (x1, y1, &x0, &y0))
  49.     {
  50.       int dx, dy, temp, iy, n, e;
  51.       unsigned char *p;
  52.  
  53.       dx = x1 - x0;
  54.       if (dx < 0)
  55.         {
  56.           dx = -dx;
  57.           temp = x1; x1 = x0; x0 = temp;
  58.           temp = y1; y1 = y0; y0 = temp;
  59.         }
  60.       ++dx;
  61.       dy = y1 - y0; iy = 320;
  62.       if (dy < 0)
  63.         {
  64.           dy = -dy;
  65.           iy = -iy;
  66.         }
  67.       ++dy;
  68.       GLOCK;
  69.       p = _g_mem + x0 + 320 * y0;
  70.       if (dx >= dy)
  71.         {
  72.           n = dx; e = dy / 2;
  73.           do
  74.             {
  75.               *p = (unsigned char)color;
  76.               ++p;
  77.               e += dy;
  78.               if (e >= dx)
  79.                 {
  80.                   e -= dx;
  81.                   p += iy;
  82.                 }
  83.             } while (--n != 0);
  84.         }
  85.       else
  86.         {
  87.           n = dy; e = dx / 2;
  88.           do
  89.             {
  90.               *p = (unsigned char)color;
  91.               p += iy;
  92.               e += dx;
  93.               if (e >= dy)
  94.                 {
  95.                   e -= dy;
  96.                   ++p;
  97.                 }
  98.             } while (--n != 0);
  99.         }
  100.       GUNLOCK;
  101.     }
  102. }
  103.