home *** CD-ROM | disk | FTP | other *** search
/ Monster Disc 2: The Best of 1992 / MONSTER2.ISO / prog / djgpp / cbgrx102.a01 / CONTRIB / LIBGRX / SRC / SCANCNVX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-12  |  3.6 KB  |  129 lines

  1. /**
  2.  ** SCANCNVX.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27. #include "scale.h"
  28.  
  29. void _GrScanConvexPoly(int n,int pt[][2],_GrScanLineProc lnproc,void *lnarg)
  30. {
  31.     int  *lp1,*lp2,*rp1,*rp2,*last;
  32.     int  lxx,ldx,ldy,lslope,lerror;
  33.     int  rxx,rdx,rdy,rslope,rerror;
  34.     int  x1,x2,ymin,ymax,ynext;
  35.     MOUSE_FLAG;
  36.  
  37.     lxx = ymin = 32000;
  38.     rxx = ymax = (-32000);
  39.     for(last = pt[0]; --n >= 0; last += 2) {
  40.         if(lxx  > last[0]) lxx  = last[0];
  41.         if(rxx  < last[0]) rxx  = last[0];
  42.         if(ymin > last[1]) ymin = (lp2 = rp2 = last)[1];
  43.         if(ymax < last[1]) ymax = last[1];
  44.     }
  45.     if(ymin == ymax) {
  46.         CLIPHLINE(CURC,lxx,rxx,ymin);
  47.         MOUSE_BLOCK(CURC,lxx,ymin,rxx,ymin);
  48.         (*lnproc)(lxx,rxx,ymin,lnarg);
  49.         MOUSE_UNBLOCK();
  50.         return;
  51.     }
  52.     CLIPSORTEDBOX(CURC,lxx,ymin,rxx,ymax);
  53.     MOUSE_BLOCK(CURC,lxx,ymin,rxx,ymax);
  54.     last -= 2;
  55.     for( ; ; ) {
  56.         lp1 = lp2;
  57.         if((lp2 -= 2) < pt[0]) lp2 = last;
  58.         if((lp1[1] != lp2[1]) && (lp2[1] >= _GrLoY)) {
  59.         lxx = lp1[0];
  60.         ldy = lp2[1] - lp1[1];
  61.         if(lp1[1] < _GrLoY) {
  62.             SCALE(ldx,(lxx - lp2[0]),(lp2[1] - _GrLoY),ldy);
  63.             lxx = lp2[0] + ldx;
  64.             ldy = lp2[1] - _GrLoY;
  65.         }
  66.         break;
  67.         }
  68.     }
  69.     for( ; ; ) {
  70.         rp1 = rp2;
  71.         if((rp2 += 2) > last) rp2 = pt[0];
  72.         if((rp1[1] != rp2[1]) && (rp2[1] >= _GrLoY)) {
  73.         rxx = rp1[0];
  74.         rdy = rp2[1] - rp1[1];
  75.         if(rp1[1] < _GrLoY) {
  76.             SCALE(rdx,(rxx - rp2[0]),(rp2[1] - _GrLoY),rdy);
  77.             rxx = rp2[0] + rdx;
  78.             rdy = rp2[1] - _GrLoY;
  79.         }
  80.         break;
  81.         }
  82.     }
  83.     for( ; ; ) {
  84.         if(ldy > 0) {
  85.         lslope = (ldx = lp2[0] - lxx) / ldy;
  86.         if((ldx %= ldy) < 0) { lslope--; ldx += ldy; }
  87.         lerror = (ldy >> 1);
  88.         ldy = (-ldy);
  89.         }
  90.         if(rdy > 0) {
  91.         rslope = (rdx = rp2[0] - rxx) / rdy;
  92.         if((rdx %= rdy) < 0) { rslope--; rdx += rdy; }
  93.         rerror = (rdy >> 1);
  94.         rdy = (-rdy);
  95.         }
  96.         ynext = lp2[1];
  97.         if(ynext >= rp2[1]) ynext = rp2[1];
  98.         if(ynext >= ymax)    ynext = ymax + 1;
  99.         while(ymin < ynext) {
  100.         if(lxx < rxx) x1 = lxx,x2 = rxx; else x1 = rxx,x2 = lxx;
  101.         if((x1 <= _GrHiX) && (x2 >= _GrLoX)) {
  102.             if(x1 < _GrLoX) x1 = _GrLoX;
  103.             if(x2 > _GrHiX) x2 = _GrHiX;
  104.             (*lnproc)(x1,x2,ymin,lnarg);
  105.         }
  106.         if((lerror -= ldx) < 0) lxx++,lerror -= ldy;
  107.         if((rerror -= rdx) < 0) rxx++,rerror -= rdy;
  108.         lxx += lslope;
  109.         rxx += rslope;
  110.         ymin++;
  111.         }
  112.         if(ymin >= ymax) break;
  113.         while(ynext == lp2[1]) {
  114.         lp1 = lp2;
  115.         if((lp2 -= 2) < pt[0]) lp2 = last;
  116.         lxx = lp1[0];
  117.         ldy = lp2[1] - lp1[1];
  118.         }
  119.         while(ynext == rp2[1]) {
  120.         rp1 = rp2;
  121.         if((rp2 += 2) > last) rp2 = pt[0];
  122.         rxx = rp1[0];
  123.         rdy = rp2[1] - rp1[1];
  124.         }
  125.     }
  126.     MOUSE_UNBLOCK();
  127. }
  128.  
  129.