⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lineclip.c

📁 nucleus 文件系统,内核和彩色图形系统,在小系统上非常好用
💻 C
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************/
/*                                                                       */
/*         Copyright (c) 1997 - 1999 Accelerated Technology, Inc.        */
/*                                                                       */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the      */
/* subject matter of this material.  All manufacturing, reproduction,    */
/* use, and sales rights pertaining to this subject matter are governed  */
/* by the license agreement.  The recipient of this software implicitly  */
/* accepts the terms of the license.                                     */
/*                                                                       */
/*************************************************************************/

/*************************************************************************/
/*                                                                       */
/* FILE NAME                                            VERSION          */
/*                                                                       */
/*      lineclip.c                                       1.9             */
/*                                                                       */
/* COMPONENT                                                             */
/*                                                                       */
/*      All                                                              */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This file contains the line clipping functions.                  */
/*                                                                       */
/* AUTHOR                                                                */
/*                                                                       */
/*      Robert G. Burrill, Accelerated Technology, Inc.                  */
/*                                                                       */
/* DATA STRUCTURES                                                       */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* FUNCTIONS                                                             */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* DEPENDENCIES                                                          */
/*                                                                       */
/*		None                                                             */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*		   NAME            DATE                    REMARKS               */
/*                                                                       */
/*		   BobB			  5/5/98		Corrected line encoding switch   */
/*		   BobB			  5/8/98		Corrected clipping function call */
/*		   BobB			 12/9/98		Corrected for zero length line   */
/*                                                                       */
/*************************************************************************/
#include "meta_wnd.h"
#include "metconst.h"    /* MetaWINDOW Constant & Stucture Definitions */
#include "metports.h"    /* MetaWINDOW Port & Bitmap Definitions */
#include "metaproc.h"
#include "grafdata.h"
#include "regclipv.h"
#include "fill_6.h"


/* Thin line rect clipping functions.  Variables required are in regclipv.h.
Checks against clip rectangle and either trivially rejects or clips to the
rectangle.  This routine performs capLast by cutting the length and capFirst
by advancing the Bresenham's variables by one point, and will reject the
line if zero points are left after capping. The calling routine need not
worry about capFirst/capLast, and in fact should pay no attention to
drawStat after the clipping call, because all capping will already have
been performed.
This routine returns "1" if the line is not visible at all and therefore
cannot be drawn. Otherwise, it sets the dRect, initial errTerm,
majorAxisLengthM1, lineDir, errTermAdjUp, and errTermAdjDown variables
appropriately for the clipped line. Note that errTerm is -deltaMajor, the
unadjusted error term for the first point, and that errTermAdjDown is
2*deltaMajor, the full adjustment when the minor axis moves. This is ideal
for bitmapped drawing, where the error term adjust up can always be made
before drawing each point and the result tested.


 5 | 3 | 4	Line encoding on entry: end coord according to table at left,
 ---------	start coord from table at left * 9. So upper left to lower
 2 | 0 | 1	right is 5*9+7 = 52.
 ---------
 8 | 6 | 7
*/
int mwLC(void)
{
	int HandleFirstPointNull(void);
	int Clip01(void);
	int Clip02(void);
	int Clip06(void);
	int Clip07(void);
	int Clip08(void);
	int Clip10(void);
	int Clip12(void);
	int Clip16(void);
	int Clip18(void);
	int Clip20(void);
	int Clip21(void);
	int Clip26(void);
	int Clip27(void);
	int Clip30(void);
	int Clip31(void);
	int Clip32(void);
	int Clip36(void);
	int Clip37(void);
	int Clip38(void);
	int Clip40(void);
	int Clip42(void);
	int Clip46(void);
	int Clip48(void);
	int Clip50(void);
	int Clip51(void);
	int Clip56(void);
	int Clip57(void);
	int tem;
	short grafErrValue;
	int deltaX, deltaY;
    int ZERO = 0;

	/* force the line to run top->bottom */
	if (dRect.Ymin > dRect.Ymax)
	{
		tem = dRect.Ymin;
		dRect.Ymin = dRect.Ymax;
		dRect.Ymax = tem;
		tem = dRect.Xmin;
		dRect.Xmin = dRect.Xmax;
		dRect.Xmax = tem;
		drawStat = -drawStat;	/* cap the other end now */
		/* flip the start and end sense of the rough clip index */
		lineEncode = (lineEncode / 9) + ((lineEncode % 9) * 9);
	}

	deltaX = dRect.Xmax - dRect.Xmin;
	deltaY = dRect.Ymax - dRect.Ymin;
	if ((deltaX > 32767) || deltaY > 32767)
	{/*		The line ends are too long. Post an error and skip the line. */
		grafErrValue = c_LineTo + c_OfloLine;
		dstBmap->cbPostErr(grafErrValue);	/* call the error posting
											routine for the dest bitmap */
		return(1);
	}

	if (deltaX == 0)
	{/* vertical line (always top->bottom) */
		lineDir = 1;
		errTermAdjDownL = 0;	/* force the minor axis to never advance */
		errTermAdjUpL = 0;	
		errTermL = -1;
		if (lineEncode == 0)
		{	/* unclipped */
			if (drawStat < ZERO)
			{	/* first point null */
				dRect.Ymin++;	/* remove the first point */
			}
			else
			{
				if (drawStat > 0)
				{	/* last point null */
					dRect.Ymax--;	/* remove the last point */
				}
			}
			goto VertP3;
		}

		if (lineEncode == 6)
		{	/* clip bottom only */
			dRect.Ymax = cRect.Ymax;
			if (drawStat < ZERO)
			{	/* first point null */
				dRect.Ymin++;	/* remove the first point */
			}
			goto VertP3;
		}

		if (lineEncode == 33)
		{	/* clip top and bottom */
			dRect.Ymin = cRect.Ymin;
			dRect.Ymax = cRect.Ymax;
			/* (First/last point null don't matter when clipped) */
			goto VertP3;
		}


		if (lineEncode != 27) return(1);	/* reject the line */

		dRect.Ymin = cRect.Ymin;	/* clip to top */
		if (drawStat > 0)
		{	/* last point null */
			dRect.Ymax--;	/* remove the last point */
		}

VertP3:
		majorAxisLengthM1 = dRect.Ymax - dRect.Ymin;

/* BobB 12/9/98 - correct the following line for zero length lines
		if (majorAxisLengthM1 <= 0) return(1);*/	/* because of capping,
									line has become zero points long */
		if (majorAxisLengthM1 < 0) return(1);	/* because of capping,
									line has become zero points long */
		return(0);	/* vertical done */
	}

	if (deltaX > 0)
	{	/* line is L->R */
		if (deltaY == 0)
		{	/* L->R, horizontal line */
			lineDir = 5;
			errTermAdjDownL = 0;	/* force the minor axis to never
									advance */
			errTermAdjUpL = 0;	
			errTermL = -1;
			if (lineEncode == 0)
			{	/* unclipped */
				if (drawStat < ZERO)
				{	/* first point null */
					dRect.Xmin++;	/* remove the first point */
				}
				else
				{
					if (drawStat > 0)
					{	/* last point null */
						dRect.Xmax--;	/* remove the last point */
					}
				}
				goto LToRHorzP3;
			}

			if (lineEncode == 1)
			{	/* clip right only */
				dRect.Xmax = cRect.Xmax;
				if (drawStat < ZERO)
				{	/* first point null */
					dRect.Xmin++;	/* remove the first point */
				}
				goto LToRHorzP3;
			}

			if (lineEncode == 19)
			{	/* clip left and right */
				dRect.Xmin = cRect.Xmin;
				dRect.Xmax = cRect.Xmax;
				/* (First/last point null don't matter when clipped) */
				goto LToRHorzP3;
			}

			if (lineEncode != 18) return(1);	/* reject the line */

			dRect.Xmin = cRect.Xmin;	/* clip to left */
			if (drawStat > 0)
			{	/* last point null */
				dRect.Xmax--;	/* remove the last point */
			}

LToRHorzP3:
			majorAxisLengthM1 = dRect.Xmax - dRect.Xmin;

/* BobB 12/9/98 - correct the following line for zero length lines
			if (majorAxisLengthM1 <= 0) return(1);*/	/* because of capping,
										line has become zero points long */
			if (majorAxisLengthM1 < 0) return(1);	/* because of capping,
										line has become zero points long */
			return(0);	/* vertical done */
		}

		if (deltaX == deltaY)
		{	/* it's a L->R diagonal line */
			lineDir = 3;
			majorAxisLengthM1 = deltaX;	/* either axis can be the
										major axis */
			errTermAdjDownL = deltaX << 1;
			errTermL = -deltaX;	/* initial error term (no fixup
					needed because we always go top->bottom) */
			errTermAdjUpL = deltaY << 1;
			goto RoughClip;
		}

		if (deltaX > deltaY)
		{	/* L->R, Xmajor */
			lineDir = 4;
		}
		else
		{	/* L->R, Ymajor */
			lineDir = 2;
			tem = deltaX;
			deltaX = deltaY;
			deltaY = tem;
		}
		goto SetInitialVars;
	}

	/* line is R->L */
	deltaX = -deltaX;
	if (deltaY == 0)
	{	/* R->L, horizontal line */
		tem = dRect.Xmin;	/* switch endpoints */
		dRect.Xmin = dRect.Xmax;
		dRect.Xmax = tem;
		lineEncode = (lineEncode / 9) + ((lineEncode % 9) * 9);
		drawStat = -drawStat;
		lineDir = 5;
		errTermAdjDownL = 0;	/* force the minor axis to never
								advance */
		errTermAdjUpL = 0;	
		errTermL = -1;
		if (lineEncode == 0)
		{	/* unclipped */
			if (drawStat < ZERO)
			{	/* first point null */
				dRect.Xmin++;	/* remove the first point */
			}
			else
			{
				if (drawStat > 0)
				{	/* last point null */
					dRect.Xmax--;	/* remove the last point */
				}
			}
			goto RToLHorzP3;
		}

		if (lineEncode == 1)
		{	/* clip right only */
			dRect.Xmax = cRect.Xmax;
			if (drawStat < ZERO)
			{	/* first point null */
				dRect.Xmin++;	/* remove the first point */
			}
			goto RToLHorzP3;
		}

		if (lineEncode == 19)
		{	/* clip left and right */
			dRect.Xmin = cRect.Xmin;
			dRect.Xmax = cRect.Xmax;
			/* (First/last point null don't matter when clipped) */
			goto RToLHorzP3;
		}

		if (lineEncode != 18) return(1);	/* reject the line */

		dRect.Xmin = cRect.Xmin;	/* clip to left */
		if (drawStat > 0)
		{	/* last point null */
			dRect.Xmax--;	/* remove the last point */
		}

RToLHorzP3:
		majorAxisLengthM1 = dRect.Xmax - dRect.Xmin;

/* BobB 12/9/98 - correct the following line for zero length lines
		if (majorAxisLengthM1 <= 0) return(1);*/	/* because of capping,
									line has become zero points long */
		if (majorAxisLengthM1 < 0) return(1);	/* because of capping,
									line has become zero points long */
		return(0);	/* vertical done */
	}

	if (deltaX == deltaY)
	{	/* it's a R->L diagonal line */
		lineDir = 6;
		majorAxisLengthM1 = deltaX;	/* either axis can be the
									major axis */
		errTermAdjDownL = deltaX << 1;
		errTermL = -deltaX;	/* initial error term (no fixup
				needed because we always go top->bottom) */
		errTermAdjUpL = deltaY << 1;
		goto RoughClip;
	}


	if (deltaX > deltaY)
	{	/* R->L, Xmajor */
		lineDir = 7;
	}
	else
	{	/* R->L, Ymajor */
		lineDir = 0;
		tem = deltaX;
		deltaX = deltaY;
		deltaY = tem;
	}

SetInitialVars:
	majorAxisLengthM1 = deltaX;	/* this is deltaY if Ymajor */
	errTermAdjDownL = deltaX << 1;
	errTermL = - deltaX;
	errTermAdjUpL = deltaY << 1;

RoughClip:	/* Actual clipping is performed starting here, based on
			the rough clip info calculated above. */
	if (lineEncode >= 58) return(1);	/* reject */

/* BobB 5/5/98 - corrected the following line for line encoding.
	switch (lineEncode) */
	switch (10 * (lineEncode / 9) + (lineEncode % 9))
	{
	case 0: /* trivial accept */
		if (drawStat ==0) return(0);	/* no cap so return */
		if (drawStat > 0)
		{
			majorAxisLengthM1--;	/* count off the last point we're
									not going to draw */
			if (majorAxisLengthM1 < 0) return(1);
			return(0);
		}
		return(HandleFirstPointNull());
	case 1:
		return(Clip01());
	case 2:
		return(Clip02());
	case 6:
		return(Clip06());
	case 7:
		return(Clip07());
	case 8:
		return(Clip08());
	case 10:
		return(Clip10());
	case 12:
		return(Clip12());
	case 16:
		return(Clip16());
	case 18:
		return(Clip18());
	case 20:
		return(Clip20());
	case 21:
		return(Clip21());
	case 26:
		return(Clip26());
	case 27:
		return(Clip27());
	case 30:
		return(Clip30());
	case 31:
		return(Clip31());
	case 32:
		return(Clip32());
	case 36:
		return(Clip36());
	case 37:
		return(Clip37());
	case 38:
		return(Clip38());
	case 40:
		return(Clip40());
	case 42:
		return(Clip42());
	case 46:
		return(Clip46());
	case 48:
		return(Clip48());
	case 50:
		return(Clip50());
	case 51:
		return(Clip51());
	case 56:
		return(Clip56());
	case 57:
		return(Clip57());
	default:
		return (1);
	}
}


/* | |
  -----
   |S|E
  -----
   | |  */
int Clip01(void)
{
	void ClipRightTocXmax(void);
	int HandleFirstPointNull(void);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -