home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src / bresenhm.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  4KB  |  200 lines

  1. /* bresenhm.c */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Id: bresenhm.c,v 1.4 1995/06/09 17:45:58 brianp Exp $
  26.  
  27. $Log: bresenhm.c,v $
  28.  * Revision 1.4  1995/06/09  17:45:58  brianp
  29.  * renamed to bresenhm.[ch]
  30.  *
  31.  * Revision 1.3  1995/05/22  21:02:41  brianp
  32.  * Release 1.2
  33.  *
  34.  * Revision 1.2  1995/03/04  19:29:44  brianp
  35.  * 1.1 beta revision
  36.  *
  37.  * Revision 1.1  1995/02/24  14:18:04  brianp
  38.  * Initial revision
  39.  *
  40.  */
  41.  
  42.  
  43. #include "context.h"
  44.  
  45.  
  46. /*
  47.  * Evaluate Bresenham's integer line drawing algorithm.  Put each
  48.  * coordinate generated into x[] and y[] arrays.
  49.  *
  50.  * Input:  x1,y1 - coordinates of first endpoint
  51.  *         x2,y2 - coordinates of second endpoint
  52.  * Output:  x, y - array of coordinates generated by the algorithm
  53.  * Return:  number of values put into x[] and y[].
  54.  */
  55. GLuint gl_bresenham( GLint x1, GLint y1, GLint x2, GLint y2,
  56.              GLint x[], GLint y[] )
  57. {
  58.    register GLint dx, dy, xf, yf, a, b, c, i;
  59.  
  60.    if (x2>x1) {
  61.       dx = x2-x1;
  62.       xf = 1;
  63.    }
  64.    else {
  65.       dx = x1-x2;
  66.       xf = -1;
  67.    }
  68.  
  69.    if (y2>y1) {
  70.       dy = y2-y1;
  71.       yf = 1;
  72.    }
  73.    else {
  74.       dy = y1-y2;
  75.       yf = -1;
  76.    }
  77.  
  78. #define PLOT( X, Y )    x[i] = X;  y[i] = Y;
  79.  
  80.    if (dx>dy) {
  81.       a = dy+dy;
  82.       c = a-dx;
  83.       b = c-dx;
  84.       for (i=0;i<=dx;i++) {
  85.      PLOT( x1, y1 );
  86.          x1 += xf;
  87.          if (c<0) {
  88.             c += a;
  89.          }
  90.          else {
  91.             c += b;
  92.             y1 += yf;
  93.          }
  94.       }
  95.       return dx+1;
  96.    }
  97.    else {
  98.       a = dx+dx;
  99.       c = a-dy;
  100.       b = c-dy;
  101.       for (i=0;i<=dy;i++) {
  102.      PLOT( x1, y1 );
  103.          y1 += yf;
  104.          if (c<0) {
  105.             c += a;
  106.          }
  107.          else {
  108.             c += b;
  109.             x1 += xf;
  110.          }
  111.       }
  112.       return dy+1;
  113.    }
  114.  
  115. #undef PLOT
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /*
  122.  * Evaluate Bresenham's line algorithm with stippling.
  123.  * Input:  x1, y1, x2, y2 - endpoints of line segment
  124.  * Output:  x, y - arrays of pixels along the line
  125.  *          mask - indicates draw/don't draw for each pixel
  126.  */
  127. GLuint gl_stippled_bresenham( GLint x1, GLint y1, GLint x2, GLint y2,
  128.                   GLint x[], GLint y[], GLubyte mask[] )
  129. {
  130.    GLint dx, dy, xf, yf, a, b, c, i;
  131.    GLushort m;
  132.  
  133.    if (x2>x1) {
  134.       dx = x2-x1;
  135.       xf = 1;
  136.    }
  137.    else {
  138.       dx = x1-x2;
  139.       xf = -1;
  140.    }
  141.  
  142.    if (y2>y1) {
  143.       dy = y2-y1;
  144.       yf = 1;
  145.    }
  146.    else {
  147.       dy = y1-y2;
  148.       yf = -1;
  149.    }
  150.  
  151. #define PLOT( X, Y )                            \
  152.     m = 1 << ((CC.StippleCounter/CC.Line.StippleFactor) & 0xf);    \
  153.     if (CC.Line.StipplePattern & m) {                \
  154.         mask[i] = 1;                        \
  155.         x[i] = X;                        \
  156.         y[i] = Y;                        \
  157.     }                                \
  158.     else {                                \
  159.         mask[i] = 0;                        \
  160.     }                                \
  161.     CC.StippleCounter++;
  162.  
  163.    if (dx>dy) {
  164.       a = dy+dy;
  165.       c = a-dx;
  166.       b = c-dx;
  167.       for (i=0;i<=dx;i++) {
  168.      PLOT( x1, y1 );
  169.          x1 += xf;
  170.          if (c<0) {
  171.             c += a;
  172.          }
  173.          else {
  174.             c += b;
  175.             y1 += yf;
  176.          }
  177.       }
  178.       return dx+1;
  179.    }
  180.    else {
  181.       a = dx+dx;
  182.       c = a-dy;
  183.       b = c-dy;
  184.       for (i=0;i<=dy;i++) {
  185.      PLOT( x1, y1 );
  186.          y1 += yf;
  187.          if (c<0) {
  188.             c += a;
  189.          }
  190.          else {
  191.             c += b;
  192.             x1 += xf;
  193.          }
  194.       }
  195.       return dy+1;
  196.    }
  197.  
  198. #undef PLOT
  199. }
  200.