实现了两种二维图形裁剪算法:Sutherland-Hodgeman单边裁剪法和Weiler-Atherton边界裁剪法,关于这两个算法的具体可以参看这个网址:http://course.cug.edu.cn/cugFirst/computer_graphics/class/start.asp
- #include <stdio.h>
- #include <alloc.h>
- #include <graphics.h>
- #include <math.h>
- typedef enum
- {
- FALSE = 0,
- TRUE = 1
- } BOOL;
- typedef enum
- {
- INSIDE,
- EDGE_IN,
- EDGE_OUT,
- EDGE_CORNER,
- OUTSIDE,
- UNKNOWN
- } VectorType;
- typedef struct
- {
- int x;
- int y;
- VectorType type;
- } Vector;
- typedef struct
- {
- Vector v1;
- Vector v2;
- } Edge;
- typedef struct _Node
- {
- Vector v;
- struct _Node *next;
- } Node, *VectorList;
- const Vector AREA_LEFT_TOP = { 145, 125 };
- const Vector AREA_RIGHT_BOTTOM = { 260, 190 };
- typedef enum
- {
- LEFT,
- TOP,
- RIGHT,
- BOTTOM,
- AROUND
- } EdgeType;
- #define VECTOR_NUM 7
- const Vector PRIMITIVE_LEFT_TOP = { 100, 100 };
- BOOL Clockwise( Vector v1, Vector v2 )
- {
- int n = v1.x * v2.y - v1.y * v2.x;
- if ( n > 0 )
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- void Push( VectorList *vList, Node *frontNode, Node *pushingNode )
- {
- Node *n;
- if ( *vList == NULL || frontNode == NULL )
- {
- printf( "error in Push;" );
- return;
- }
- pushingNode->next = frontNode->next;
- frontNode->next = pushingNode;
- }
- void PushBack( VectorList *vList, Node *pushingNode )
- {
- Node *n;
- pushingNode->next = NULL;
- if ( *vList == NULL )
- {
- *vList = pushingNode;
- }
- else
- {
- for ( n = *vList; n->next != NULL; n = n->next );
- n->next = pushingNode;
- }
- }
- void Erase( VectorList *vList, Node **erasingNode )
- {
- Node *n;
- if ( *vList == NULL )
- {
- printf( "error in Erase" );
- return;
- }
- if ( *erasingNode == *vList )
- {
- *vList = ( *vList )->next;
- free( *erasingNode );
- return;
- }
- for ( n = *vList; n->next != *erasingNode && n->next != NULL; n = n->next );
- if ( n->next == NULL )
- {
- printf( "can not find node in Erase" );
- return;
- }
- n->next = ( *erasingNode )->next;
- free( *erasingNode );
- }
- /* 获得顶点列表的索引为i的元素, 0 <= i < numVector */
- Node* GetVector( VectorList vList, int i )
- {
- int j;
- Node *n;
- for ( j = 0, n = vList; j < i && n->next != NULL; i++, n = n->next );
- return n;
- }
- BOOL InsideEdge( Vector v, EdgeType type )
- {
- switch ( type )
- {
- case ( LEFT ) :
- return v.x >= AREA_LEFT_TOP.x;
- case ( RIGHT ) :
- return v.x <= AREA_RIGHT_BOTTOM.x;
- case ( TOP ) :
- return v.y >= AREA_LEFT_TOP.y;
- case ( BOTTOM ) :
- return v.y <= AREA_RIGHT_BOTTOM.y;
- case ( AROUND ):
- return v.x >= AREA_LEFT_TOP.x &
- v.x <= AREA_RIGHT_BOTTOM.x &
- v.y >= AREA_LEFT_TOP.y &
- v.y <= AREA_RIGHT_BOTTOM.y;
- default :
- return FALSE;
- }
- }
- /* 检测一条边与某个边界的交点 */
- Vector CalcIntersectPoint( Edge e, EdgeType type )
- {
- Vector res = { -1, -1 };
- float x0, x1, y0, y1;
- x0 = e.v1.x;
- x1 = e.v2.x;
- y0 = e.v1.y;
- y1 = e.v2.y;
- switch ( type )
- {
- case ( LEFT ) :
- if ( x1 - x0 != 0 )
- {
- res.x = AREA_LEFT_TOP.x;
- res.y = floor( res.x * ( y1 - y0 ) / ( x1 - x0 ) + ( x1 * y0 - x0 * y1 ) / ( x1 - x0 ) + 0.5f );
- return res;
- }
- else
- {
- printf( "error LEFT" );
- return res;
- }
- case ( RIGHT ) :
- if ( x1 - x0 != 0 )
- {
- res.x = AREA_RIGHT_BOTTOM.x;
- res.y = floor( res.x * ( y1 - y0 ) / ( x1 - x0 ) + ( x1 * y0 - x0 * y1 ) / ( x1 - x0 ) + 0.5f );
- return res;
- }
- else
- {
- printf( "error RIGHT" );
- return res;
- }
- case ( TOP ) :
- if ( y1 - y0 != 0 )
- {
- res.y = AREA_LEFT_TOP.y;
- res.x = floor( res.y * ( x1 - x0 ) / ( y1 - y0 ) + ( x0 * y1 - x1 * y0 ) / ( y1 - y0 ) + 0.5f );
- return res;
- }
- else
- {
- printf( "error TOP" );
- return res;
- }
- case ( BOTTOM ) :
- if ( y1 - y0 != 0 )
- {
- res.y = AREA_RIGHT_BOTTOM.y;
- res.x = floor( res.y * ( x1 - x0 ) / ( y1 - y0 ) + ( x0 * y1 - x1 * y0 ) / ( y1 - y0 ) + 0.5f );
- return res;
- }
- else
- {
- printf( "error BOTTOM" );
- return res;
- }
- default :
- printf( "unknown type" );
- }
- /* should never be here */
- return res;
- }
- /* 单边裁剪 */
- void SingleEdgeClipping( Vector primitive[], int numVector )
- {
- int i;
- VectorList vList = NULL;
- Node *n = NULL, *m = NULL;
- Vector vTemp, vNext;
- Vector v1, v2;
- Edge eTemp;
- BOOL in1, in2;
- EdgeType type;
- /* 顺时针序列转为逆时针序列 */
- for ( i = numVector - 1; i >= 0; i-- )
- {
- n = ( Node* )malloc( sizeof( Node ) );
- n->v = primitive[i];
- n->next = NULL;
- PushBack( &vList, n );
- }
- /* 分别对左右上下四个边界作单边裁剪 */
- for ( type = LEFT; type <= BOTTOM; type++ )
- {
- n = vList;
- /* 遍历顶点列表 */
- while ( n != NULL )
- {
- /*printf( "cur(%d,%d) ", n->v.x - PRIMITIVE_LEFT_TOP.x, n->v.y - PRIMITIVE_LEFT_TOP.y );*/
- in1 = InsideEdge( n->v, type );
- if ( n->next == NULL )
- {
- vNext = vList->v;
- }
- else
- {
- vNext = n->next->v;
- }
- in2 = InsideEdge( vNext, type );
- if ( in1 == TRUE ) /* 当前遍历的顶点在边界以内 */
- {
- if ( in2 == TRUE ) /* 下一个顶点在边界以内 */
- {
- n = n->next;;
- }
- else
- {
- /* 求与边界的交点 */
- eTemp.v1 = n->v;
- eTemp.v2 = vNext;
- vTemp = CalcIntersectPoint( eTemp, type );
- /* 将交点添加到List,添加到当前遍历顶点的后面 */
- m = ( Node* )malloc( sizeof( Node ) );
- m->v = vTemp;
- m->next = NULL;
- Push( &vList, n, m);
- /*printf("add(%d,%d) ", m->v.x - PRIMITIVE_LEFT_TOP.x, m->v.y - PRIMITIVE_LEFT_TOP.y );*/
- /* 跳过当前新添加的节点 */
- n = m->next;
- }
- }
- else
- {
- if ( in2 == TRUE )
- {
- /* 求与边界的交点 */
- eTemp.v1 = n->v;
- eTemp.v2 = vNext;
- vTemp = CalcIntersectPoint( eTemp, type );
- /* 将交点添加到List,添加到当前遍历顶点的后面 */
- m = ( Node* )malloc( sizeof( Node ) );
- m->v = vTemp;
- m->next = NULL;
- Push( &vList, n, m);
- /*printf("add(%d,%d) ", m->v.x - PRIMITIVE_LEFT_TOP.x, m->v.y - PRIMITIVE_LEFT_TOP.y );*/
- m = n;
- /* 跳过当前新添加的节点 */
- n = n->next->next;
- /* 从顶点列表中删除当前节点 */
- /*printf("del(%d,%d) ", m->v.x - PRIMITIVE_LEFT_TOP.x, m->v.y - PRIMITIVE_LEFT_TOP.y );*/
- Erase( &vList, &m );
- }
- else
- {
- m = n;
- n = n->next;
- /* 从顶点列表中删除当前节点 */
- /*printf("del(%d,%d) ", m->v.x - PRIMITIVE_LEFT_TOP.x, m->v.y - PRIMITIVE_LEFT_TOP.y );*/
- Erase( &vList, &m );
- }
- }
- }
- /*
- n = vList;
- while ( n != NULL )
- {
- printf("(%d,%d)", n->v.x - PRIMITIVE_LEFT_TOP.x, n->v.y - PRIMITIVE_LEFT_TOP.y);
- n = n->next;
- }
- printf("/n");
- */
- }
- n = vList;
- setlinestyle( SOLID_LINE, 0, 3 );
- setcolor( RED );
- while ( n->next != NULL )
- {
- line( n->v.x, n->v.y, n->next->v.x, n->next->v.y );
- n = n->next;
- }
- line( n->v.x - PRIMITIVE_LEFT_TOP.x, n->v.y - PRIMITIVE_LEFT_TOP.y, vList->v.x - PRIMITIVE_LEFT_TOP.x, vList->v.y - PRIMITIVE_LEFT_TOP.y );
- }
- /* 检测一条边与所有边界的交点,交点可能有1个或两个,函数返回交点数目 */
- int CalcIntersectPoints( Edge e, Vector vRes[] )
- {
- int n = 0;
- EdgeType type;
- BOOL b1, b2;
- Vector v;
- float t1, t2;
- for ( type = LEFT; type <= BOTTOM; type++ )
- {
- b1 = InsideEdge( e.v1, type );
- b2 = InsideEdge( e.v2, type );
- if ( b1 ^ b2 == 1 ) /* 在边缘的两侧才有可能有交点 */
- {
- v = CalcIntersectPoint( e, type );
- /* 必须保证交点在区域内 */
- if ( InsideEdge( v, AROUND ) == TRUE )
- {
- vRes[n] = v;
- n++;
- }
- }
- }
- if ( n == 2 )
- {
- t1 = fabs( vRes[0].x - e.v1.x );
- t2 = fabs( vRes[1].x - e.v1.x );
- /*printf("e.v1(%d,%d)", e.v1.x - PRIMITIVE_LEFT_TOP.x, e.v1.y - PRIMITIVE_LEFT_TOP.y );
- printf("sec1(%d,%d)", vRes[0].x - PRIMITIVE_LEFT_TOP.x, vRes[0].y - PRIMITIVE_LEFT_TOP.y );
- printf("(t1:%f, t2:%f)", t1, t2 );*/
- /* 必须保证第一个交点比第二个交点更接近起点(即e的第一个顶点)*/
- if ( t1 > t2 )
- {
- v = vRes[0];
- vRes[0] = vRes[1];
- vRes[1] = v;
- }
- }
- if ( n > 2 )
- {
- printf( "(%d,%d)num wrong(%d)", e.v1.x, e.v1.y, n );
- }
- return n;
- }
- /* 对一边的顶点进行排序 */
- void SortEdgeVectors( Vector vList[], int numVector, EdgeType type )
- {
- int i, j;
- Vector vTemp;
- switch ( type )
- {
- case LEFT:
- for ( i = 1; i <= numVector - 1; i++ )
- {
- for ( j = 0; j < numVector - i; j++ )
- {
- if ( vList[j].y > vList[j + 1].y )
- {
- /* 交换vList[j]和vList[j + 1] */
- vTemp = vList[j];
- vList[j] = vList[j + 1];
- vList[j + 1] = vTemp;
- }
- }
- }
- break;
- case TOP:
- for ( i = 1; i <= numVector - 1; i++ )
- {
- for ( j = 0; j < numVector - i; j++ )
- {
- if ( vList[j].x < vList[j + 1].x )
- {
- /* 交换vList[j]和vList[j + 1] */
- vTemp = vList[j];
- vList[j] = vList[j + 1];
- vList[j + 1] = vTemp;
- }
- }
- }
- break;
- case RIGHT:
- for ( i = 1; i <= numVector - 1; i++ )
- {
- for ( j = 0; j < numVector - i; j++ )
- {
- if ( vList[j].y < vList[j + 1].y )
- {
- /* 交换vList[j]和vList[j + 1] */
- vTemp = vList[j];
- vList[j] = vList[j + 1];
- vList[j + 1] = vTemp;
- }
- }
- }
- break;
- case BOTTOM:
- for ( i = 1; i <= numVector - 1; i++ )
- {
- for ( j = 0; j < numVector - i; j++ )
- {
- if ( vList[j].x > vList[j + 1].x )
- {
- /* 交换vList[j]和vList[j + 1] */
- vTemp = vList[j];
- vList[j] = vList[j + 1];
- vList[j + 1] = vTemp;
- }
- }
- }
- break;
- default:
- printf( "error in Sort" );
- break;
- }
- }
- /* 边界裁剪 */
- void EdgeClipping( Vector primitive[], int numVector )
- {
- int i = 0, j = 0, k = 0, l = 0;
- /* vList是多边形顶点及插入交点的序列
- vEdgeList是区域ABCD四个点及插入交点的序列 */
- VectorList vList = NULL;
- Node *n = NULL, *m = NULL;
- Vector vTemp, vNext;
- Vector vIntersect[2];
- int num = 0, numIntersect = 0, numEdge = 0;
- Vector v1, v2;
- Edge eTemp;
- VectorType vType;
- EdgeType eType;
- Vector *polyList = NULL, *edgeList = NULL, *tempList = NULL;
- Vector curStart;
- Vector *clippedList = NULL;
- int numClipped = 0;
- /* 顺时针序列转为逆时针序列 */
- for ( i = numVector - 1; i >= 0; i-- )
- {
- n = ( Node* )malloc( sizeof( Node ) );
- n->v = primitive[i];
- n->next = NULL;
- if ( InsideEdge( n->v, AROUND ) )
- {
- n->v.type = INSIDE;
- }
- else
- {
- n->v.type = OUTSIDE;
- }
- PushBack( &vList, n );
- }
- /* 求各个交点,初始化vList */
- n = vList;
- while ( n != NULL )
- {
- if ( n->next == NULL )
- {
- vNext = vList->v;
- }
- else
- {
- vNext = n->next->v;
- }
- eTemp.v1 = n->v;
- eTemp.v2 = vNext;
- num = CalcIntersectPoints( eTemp, vIntersect );
- numIntersect += num;
- if ( num == 0 )
- {
- n = n->next;
- }
- if ( num == 1 )
- {
- if ( n->v.type == OUTSIDE )
- {
- /* 交点是入点 */
- vType = EDGE_IN;
- }
- else
- {
- /* 交点是出点 */
- vType = EDGE_OUT;
- }
- /* 添加到vList */
- m = ( Node* )malloc( sizeof( Node ) );
- m->v = vIntersect[0];
- m->v.type = vType;
- m->next = NULL;
- Push( &vList, n, m );
- n = n->next->next;
- }
- else if ( num == 2 )
- {
- /* 必定第一个交点是入点,第二个交点是出点 */
- /* 添加第一个交点到vList */
- m = ( Node* )malloc( sizeof( Node ) );
- m->v = vIntersect[0];
- m->v.type = EDGE_IN;
- m->next = NULL;
- Push( &vList, n, m );
- n = m;
- /* 添加第二个交点到vList */
- m = ( Node* )malloc( sizeof( Node ) );
- m->v = vIntersect[1];
- m->v.type = EDGE_OUT;
- m->next = NULL;
- Push( &vList, n, m );
- n = m;
- n = n->next;
- }
- }
- polyList = ( Vector* )malloc( sizeof( Vector ) * ( numVector + numIntersect ) );
- edgeList = ( Vector* )malloc( sizeof( Vector ) * ( 4 + numIntersect ) );
- tempList = ( Vector* )malloc( sizeof( Vector ) * numIntersect );
- /* 初始化polyList */
- i = 0;
- n = vList;
- while ( n != NULL )
- {
- polyList[i].x = n->v.x;
- polyList[i].y = n->v.y;
- polyList[i].type = n->v.type;
- n = n->next;
- i++;
- }
- /*
- for ( i = 0; i < numVector + numIntersect; i++)
- {
- printf("[%d](%d,%d) ", polyList[i].type, polyList[i].x - PRIMITIVE_LEFT_TOP.x, polyList[i].y - PRIMITIVE_LEFT_TOP.y );
- }
- printf("/n");
- */
- /* 初始化edgeList */
- numEdge = 0;
- /*
- 按逆时针DCBA
- A----------B
- | |
- | |
- | |
- D----------C
- */
- /* 插入D点 */
- edgeList[numEdge].x = AREA_LEFT_TOP.x;
- edgeList[numEdge].y = AREA_RIGHT_BOTTOM.y;
- edgeList[numEdge].type = EDGE_CORNER;
- numEdge++;
- /* 寻找DC边上的点并排序 */
- num = 0;
- for ( i = 0; i < numVector + numIntersect; i++ )
- {
- if ( polyList[i].y == AREA_RIGHT_BOTTOM.y )
- {
- tempList[num] = polyList[i];
- num++;
- }
- }
- SortEdgeVectors( tempList, num, BOTTOM );
- /* 插入DC上的点 */
- for ( i = 0; i < num; i++ )
- {
- edgeList[numEdge] = tempList[i];
- numEdge++;
- }
- /* 插入C点 */
- edgeList[numEdge].x = AREA_RIGHT_BOTTOM.x;
- edgeList[numEdge].y = AREA_RIGHT_BOTTOM.y;
- edgeList[numEdge].type = EDGE_CORNER;
- numEdge++;
- /* 寻找CB边上的点并排序 */
- num = 0;
- for ( i = 0; i < numVector + numIntersect; i++ )
- {
- if ( polyList[i].x == AREA_RIGHT_BOTTOM.x )
- {
- tempList[num] = polyList[i];
- num++;
- }
- }
- SortEdgeVectors( tempList, num, RIGHT );
- /* 插入CB上的点 */
- for ( i = 0; i < num; i++ )
- {
- edgeList[numEdge] = tempList[i];
- numEdge++;
- }
- /* 插入B点 */
- edgeList[numEdge].x = AREA_RIGHT_BOTTOM.x;
- edgeList[numEdge].y = AREA_LEFT_TOP.y;
- edgeList[numEdge].type = EDGE_CORNER;
- numEdge++;
- /* 寻找BA边上的点并排序 */
- num = 0;
- for ( i = 0; i < numVector + numIntersect; i++ )
- {
- if ( polyList[i].y == AREA_LEFT_TOP.y )
- {
- tempList[num] = polyList[i];
- num++;
- }
- }
- SortEdgeVectors( tempList, num, TOP );
- /* 插入BA上的点 */
- for ( i = 0; i < num; i++ )
- {
- edgeList[numEdge] = tempList[i];
- numEdge++;
- }
- /* 插入A点 */
- edgeList[numEdge].x = AREA_LEFT_TOP.x;
- edgeList[numEdge].y = AREA_LEFT_TOP.y;
- edgeList[numEdge].type = EDGE_CORNER;
- numEdge++;
- /* 寻找AD边上的点并排序 */
- num = 0;
- for ( i = 0; i < numVector + numIntersect; i++ )
- {
- if ( polyList[i].x == AREA_LEFT_TOP.x )
- {
- tempList[num] = polyList[i];
- num++;
- }
- }
- SortEdgeVectors( tempList, num, LEFT );
- /* 插入AD上的点 */
- for ( i = 0; i < num; i++ )
- {
- edgeList[numEdge] = tempList[i];
- numEdge++;
- }
- /*
- for ( i = 0; i < numEdge; i++)
- {
- printf("[%d](%d,%d) ", edgeList[i].type, edgeList[i].x - PRIMITIVE_LEFT_TOP.x, edgeList[i].y - PRIMITIVE_LEFT_TOP.y );
- }
- printf("/n");
- */
- /* 边界裁剪算法开始进行 */
- while ( TRUE )
- {
- /* 初始化输出数组 */
- clippedList = ( Vector* )malloc( sizeof( Vector ) * ( numVector + numIntersect ) );
- numClipped = 0;
- /* 从polyList中找入点 */
- for ( i = 0; polyList[i].type != EDGE_IN && i < numVector + numIntersect; i++ );
- if ( i == numVector + numIntersect ) /* 没有找到入点,算法终止 */
- {
- /*printf( "End" );*/
- break;
- }
- /* 将入点记为起始点 */
- curStart = polyList[i];
- while ( TRUE )
- {
- /* 入点进clippedList,并将该入点的入点标记删去 */
- clippedList[numClipped++] = polyList[i];
- /*printf("in(%d, %d)",clippedList[numClipped-1].x- PRIMITIVE_LEFT_TOP.x,clippedList[numClipped-1].y - PRIMITIVE_LEFT_TOP.y );*/
- polyList[i].type = UNKNOWN;
- /* 寻找出点 */
- if ( i == numVector + numIntersect - 1 )
- {
- j = 0;
- }
- else
- {
- j = i + 1;
- }
- while ( TRUE )
- {
- if ( polyList[j].type != EDGE_OUT )
- {
- /* 非出点进clippedList */
- clippedList[numClipped++] = polyList[j];
- /*printf("in(%d, %d)",clippedList[numClipped-1].x- PRIMITIVE_LEFT_TOP.x,clippedList[numClipped-1].y - PRIMITIVE_LEFT_TOP.y );*/
- }
- else
- {
- /* 找到了出点, 先把出点也进clippedList */
- clippedList[numClipped++] = polyList[j];
- /*printf("in(%d, %d)",clippedList[numClipped-1].x- PRIMITIVE_LEFT_TOP.x,clippedList[numClipped-1].y - PRIMITIVE_LEFT_TOP.y );*/
- break;
- }
- if ( j == numVector + numIntersect - 1 )
- {
- /* 到了polyList的末尾则调到开始继续寻找 */
- j = 0;
- }
- else if ( j == i )
- {
- /* 错误——没有找到出点!*/
- printf( "error-can not find out point" );
- return;
- }
- else
- {
- j++;
- }
- }
- /* 从polyList中找到了出点,接着从edgeList中找入点 */
- /* 先从edgeList中找到与当前出点相同的点 */
- for ( k = 0; edgeList[k].x != polyList[j].x || edgeList[k].y != polyList[j].y && k < numEdge; k++ );
- /*printf("eOut(%d,%d)", edgeList[k].x- PRIMITIVE_LEFT_TOP.x,edgeList[k].y - PRIMITIVE_LEFT_TOP.y );*/
- if ( k == numEdge )
- {
- /* 错误——没有找到与polyList中出点相对应的边缘点! */
- printf( "error-can not find out point in edgeList/n" );
- return;
- }
- /* 接着寻找入点 */
- if ( k == numEdge - 1 )
- {
- l = 0;
- }
- else
- {
- l = k + 1;
- }
- while ( TRUE )
- {
- if ( edgeList[l].type != EDGE_IN )
- {
- /* 非入点进clippedList */
- clippedList[numClipped++] = edgeList[l];
- /*printf("in(%d, %d)",clippedList[numClipped-1].x- PRIMITIVE_LEFT_TOP.x,clippedList[numClipped-1].y - PRIMITIVE_LEFT_TOP.y );*/
- }
- else
- {
- /* 找到了入点 */
- break;
- }
- if ( l == numEdge - 1 )
- {
- /* 到了edgeList的末尾则调到开始继续寻找 */
- l = 0;
- }
- else if ( l == k )
- {
- /* 错误——没有找到入点! */
- printf( "error-can not find in point in edgeList/n" );
- }
- else
- {
- l++;
- }
- }
- /* 从edgeList中找到了入点, 判断是否与起点相等 */
- if ( edgeList[l].x == curStart.x && edgeList[l].y == curStart.y )
- {
- /* 与起点相等,哈哈~一个多边形区域被确定了! */
- clippedList[numClipped++] = edgeList[l];
- /*printf(" DONE " );*/
- break;
- }
- else
- {
- clippedList[numClipped++] = edgeList[l];
- /* 与起点不相等 */
- /* 从polyList中找到与当前入点相同的点 */
- for ( i = 0; polyList[i].x != edgeList[l].x || polyList[i].y != edgeList[l].y && i < numVector + numIntersect; i++ );
- if ( i == numVector + numIntersect )
- {
- /* 错误——没有找到与edgeList中出点相对应的边缘点! */
- printf( "error-can not find out point in edgeList/n" );
- return;
- }
- continue;
- }
- }
- /* 上面这个while循环确定一个多边形区域,接着绘制这份多边形区域 */
- setcolor( RED );
- setlinestyle( SOLID_LINE, 0, 3 );
- for ( i = 0; i < numClipped - 1; i++ )
- {
- line( clippedList[i].x, clippedList[i].y, clippedList[i + 1].x, clippedList[i + 1].y );
- }
- /*
- for ( i = 0; i < numClipped; i++ )
- {
- printf( "(%d,%d)", clippedList[i].x - PRIMITIVE_LEFT_TOP.x,clippedList[i].y - PRIMITIVE_LEFT_TOP.x );
- }
- printf("/n");
- */
- free( clippedList );
- }
- free( polyList );
- free( edgeList );
- }
- int main()
- {
- int gdriver = VGA , gmode = VGAHI;
- Vector primitive[VECTOR_NUM + 1];
- int i = 0;
- char cmd = ' ';
- primitive[0].x = PRIMITIVE_LEFT_TOP.x + 35;
- primitive[0].y = PRIMITIVE_LEFT_TOP.y;
- primitive[1].x = PRIMITIVE_LEFT_TOP.x + 200;
- primitive[1].y = PRIMITIVE_LEFT_TOP.y + 60;
- primitive[2].x = PRIMITIVE_LEFT_TOP.x + 130;
- primitive[2].y = PRIMITIVE_LEFT_TOP.y + 140;
- primitive[3].x = PRIMITIVE_LEFT_TOP.x;
- primitive[3].y = PRIMITIVE_LEFT_TOP.y + 80;
- primitive[4].x = PRIMITIVE_LEFT_TOP.x + 60;
- primitive[4].y = PRIMITIVE_LEFT_TOP.y + 60;
- primitive[5].x = PRIMITIVE_LEFT_TOP.x + 60;
- primitive[5].y = PRIMITIVE_LEFT_TOP.y + 20;
- primitive[6].x = PRIMITIVE_LEFT_TOP.x + 40;
- primitive[6].y = PRIMITIVE_LEFT_TOP.y + 40;
- primitive[7] = primitive[0];
- initgraph (&gdriver , &gmode , "d://");
- while ( TRUE )
- {
- cleardevice();
- setcolor( WHITE );
- setlinestyle( SOLID_LINE, 0, 1 );
- for ( i = 0; i < VECTOR_NUM; i++ )
- {
- line( primitive[i].x, primitive[i].y, primitive[i + 1].x, primitive[i + 1].y );
- }
- setlinestyle( DOTTED_LINE, 0, 1 );
- rectangle( AREA_LEFT_TOP.x, AREA_LEFT_TOP.y, AREA_RIGHT_BOTTOM.x, AREA_RIGHT_BOTTOM.y );
- outtext( "Enter the key:" );
- outtextxy( 20, 10, "'1': Sutherland - Hodgeman Clipping." );
- outtextxy( 20, 20, "'2': Weiler - Atherton Clipping." );
- outtextxy( 20, 30, "'q': Quit." );
- cmd = getch();
- if ( cmd == '1' )
- {
- SingleEdgeClipping( primitive, VECTOR_NUM );
- getch();
- }
- else if ( cmd == '2' )
- {
- EdgeClipping( primitive, VECTOR_NUM );
- getch();
- }
- else if ( cmd == 'q' )
- {
- closegraph();
- return 0;
- }
- }
- /* should never be here */
- closegraph();
- return 0;
- }