home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / BRESNHAM.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  4KB  |  187 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** bresnham.c: Implementation of Bresenham's algorithm
  25.  **
  26.  ** 91/01/04  V 1.00  HWW  Due to pseudocode in D.F. Rogers (1986) McGraw Hill
  27.  ** 91/10/15  V 1.01  HWW  ANSI_C
  28.  **/
  29.  
  30. #define    TEST    0
  31.  
  32.  
  33.  
  34. #include <stdio.h>
  35. #include "bresnham.h"
  36.  
  37.  
  38. static    DevPt    p_act;
  39. static        dx, dy, s1, s2, swapdir, err, i;
  40.  
  41.  
  42. DevPt    *bresenham_init (DevPt *pp1, DevPt *pp2)
  43. /**
  44.  ** Init. generation of a straight line between *pp1 & *pp2
  45.  **
  46.  ** Returns pointer to internally generated actual point of line.
  47.  ** Use this ptr for further reference! Example of use:
  48.  **
  49.  **    ..
  50.  **    #include <share/bresnham.h>
  51.  **    ...
  52.  **    DevPt    p1, p2, *pp;
  53.  **    ...
  54.  **    pp = bresenham_init (&p1, &p2);
  55.  **    do {
  56.  **        plot (pp);
  57.  **    } while (bresenham_next() != BRESENHAM_ERR);
  58.  **/
  59. {
  60.   p_act = *pp1;
  61.  
  62.   if ((dx = pp2->x - pp1->x) != 0)
  63.   {
  64.     if (dx < 0)
  65.     {
  66.         dx = -dx;
  67.         s1 = -1;
  68.     }
  69.     else
  70.         s1 = 1;
  71.   }
  72.   else
  73.     s1 = 0;    /* dx = abs(x2-x1), s1 = sign(x2-x1)    */
  74.  
  75.   if ((dy = pp2->y - pp1->y) != 0)
  76.   {
  77.     if (dy < 0)
  78.     {
  79.         dy = -dy;
  80.         s2 = -1;
  81.     }
  82.     else
  83.         s2 = 1;
  84.   }
  85.   else
  86.     s2 = 0;    /* dy = abs(y2-y1), s2 = sign(y2-y1)    */
  87.  
  88.   if (dy > dx)
  89.   {
  90.     swapdir = dx;    /* use swapdir as temp. var.    */
  91.     dx = dy;
  92.     dy = swapdir;
  93.     swapdir = 1;
  94.   }
  95.   else
  96.     swapdir = 0;
  97.  
  98.   i   = dx;        /* Init. of loop cnt    */
  99.   dy <<=1;
  100.   err = dy - dx;    /* Init. of error term    */
  101.   dx <<=1;
  102.  
  103.   return &p_act;
  104. }
  105.  
  106.  
  107.  
  108. int    bresenham_next (void)
  109. /**
  110.  ** Move actual point to next position (if possible)
  111.  **
  112.  ** Returns 0         if ok,
  113.  **       BRESENHAM_EOL if last point reached (p_act == *pp2),
  114.  **       BRESENHAM_ERR else (e.g. if moving past EOL attempted)
  115.  **/
  116. {
  117.   if (i<=0)
  118.     return (BRESENHAM_ERR);    /* Beyond last point! */
  119.  
  120.   while (err >= 0)
  121.   {
  122.     if (swapdir)
  123.         p_act.x += s1;
  124.     else
  125.         p_act.y += s2;
  126.     err -=  dx;
  127.   }
  128.   if (swapdir)
  129.     p_act.y += s2;
  130.   else
  131.     p_act.x += s1;
  132.   err +=  dy;
  133.  
  134.   i--;    /* i==0 indicates "last point reached"    */
  135.   return ((i) ? 0 : BRESENHAM_EOL);
  136. }
  137.  
  138.  
  139.  
  140.     /* Test module */
  141. #if TEST
  142. #ifdef __TURBOC__ && __MSDOS__
  143.  
  144.  
  145. #include <graphics.h>
  146.  
  147.  
  148. void    b_line (DevPt *pp1, DevPt *pp2, int col)
  149. {
  150. DevPt    *pp;
  151.  
  152.  pp = bresenham_init (pp1, pp2);
  153.  do {
  154.     putpixel (pp->x, pp->y, col);
  155.  } while (bresenham_next() != BRESENHAM_ERR);
  156. }
  157.  
  158.  
  159. void    main(void)
  160. {
  161. int    gdriver=DETECT, gmode, MaxX, MaxY;
  162. DevPt    pa, pc;
  163.  
  164.   initgraph (&gdriver, &gmode, "");
  165.  
  166.   MaxX = getmaxx();
  167.   MaxY = getmaxy();
  168.  
  169.   pc.x = (MaxX + 1) >> 1;
  170.   pc.y = (MaxY + 1) >> 1;
  171.  
  172.   for (pa.x=0, pa.y=0; pa.x < MaxX ; pa.x += 4)
  173.     b_line (&pc, &pa, RED);
  174.   for (pa.x=MaxX, pa.y=0; pa.y < MaxY ; pa.y += 3)
  175.     b_line (&pc, &pa, GREEN);
  176.  
  177.   for (pa.x=MaxX, pa.y=MaxY; pa.x >= 0 ; pa.x -= 4)
  178.     b_line (&pc, &pa, LIGHTRED);
  179.   for (pa.x=0, pa.y=MaxY; pa.y >= 0 ; pa.y -= 3)
  180.     b_line (&pc, &pa, LIGHTGREEN);
  181.  
  182.   getchar();
  183. }
  184.  
  185. #endif
  186. #endif
  187.