home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / bits / bitblit / lline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-23  |  2.8 KB  |  123 lines

  1. /* src/lline.c @finen */
  2.  
  3. /*
  4.  * file:         lline.c
  5.  *
  6.  * sccs-id:      @(#)lline.c  1.3  85/03/24
  7.  *
  8.  * description: this file contains a generalised line drawing routine.
  9.  *
  10.  *
  11.  * author:       simon kenyon.
  12.  *
  13.  * history:      sck  1.1  85/03/01  created.
  14.  *              sck   1.2  85/03/08  tidied up for release.
  15.  *              sck   1.3  85/03/24  changed the include files around.
  16.  */
  17.  
  18. #include "layers.h"
  19.  
  20. unsigned short penmap[] = {
  21.    0xffff
  22. };
  23. struct bitmap  pen = {
  24.    penmap, 1, {{
  25.            0, 0
  26.    }, {
  27.        1, 1
  28.    }
  29.    }, 0, 0
  30. };
  31.  
  32. /*
  33.  * name:         sign
  34.  *
  35.  * description: return the sign of the argument a.
  36.  *
  37.  * synopsis:     int     sign (a)
  38.  *              int      a;
  39.  *
  40.  * globals:      none.
  41.  *
  42.  * calls:        nothing.
  43.  *
  44.  * called by:    lline  (lline.c)
  45.  */
  46. int    sign (a)
  47. int    a;
  48. {
  49.     if (a < 0)
  50.        return (-1);
  51.    else
  52.        if (a > 0)
  53.            return (1);
  54.        else
  55.            return (0);
  56. }
  57.  
  58. /*
  59.  * name:         lline
  60.  *
  61.  * description: draw a line from p0 to p1 in a layer lp, using code f.
  62.  *              (this was snarfed from the 1st smalltalk-80 book. it
  63.  *              is nowhere as efficient as the restartable dda described
  64.  *              in rob's original paper, but i was having so much hassle
  65.  *              with the clipping in that, and i wanted to ship this...)
  66.  *
  67.  * synopsis:     lline (lp, p0, p1, f)
  68.  *              struct layer    *lp;
  69.  *              struct point    *p0;
  70.  *              struct point    *p1;
  71.  *              int      f;
  72.  *
  73.  * globals:      pen  (r)
  74.  *
  75.  * calls:        lblt (lblt.c)
  76.  *
  77.  * called by:    this is a top level routine.
  78.  */
  79. lline (lp, p0, p1, f)
  80. struct layer  *lp;
  81. struct point  *p0;
  82. struct point  *p1;
  83. int    f;
  84. {
  85.    int      dx;
  86.    int      dy;
  87.    int      px;
  88.    int      py;
  89.    int      p;
  90.    struct point     pt;
  91.    int      i;
  92.  
  93.    dx = sign ((p1 -> x) - (p0 -> x));
  94.    dy = sign ((p1 -> y) - (p0 -> y));
  95.    px = abs ((p1 -> y) - (p0 -> y));
  96.    py = abs ((p1 -> x) - (p0 -> x));
  97.    pt.x = (p0 -> x) + (lp -> ly_rect.origin.x);
  98.    pt.y = (p0 -> y) + (lp -> ly_rect.origin.y);
  99.    lblt (lp, &pen, &pen.rect, null, &pt, f);/* first point */
  100.    if (py > px) {                      /* more horizontal */
  101.        p = py / 2;
  102.        for (i = 1; i < py; i++) {
  103.            pt.x += dx;
  104.            if ((p -= px) < 0) {
  105.                pt.y += dy;
  106.                p += py;
  107.            }
  108.            lblt (lp, &pen, &pen.rect, null, &pt, f);
  109.        }
  110.    }
  111.    else {                              /* more horizontal */
  112.        p = px / 2;
  113.        for (i = 1; i <= px; i++) {
  114.            pt.y += dy;
  115.            if ((p -= py) < 0) {
  116.                pt.x += dx;
  117.                p += px;
  118.            }
  119.            lblt (lp, &pen, &pen.rect, null, &pt, f);
  120.        }
  121.    }
  122. }
  123.