Sutherlang-Hodgman 多边形裁剪算法

http://blog.csdn.net/nanhaizhixin/article/details/6531896

#include<GL/glut.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct 
{
	float x,y;
}wcPt2D;

typedef enum
{
	Left,Right,Bottom,Top
}Boundary;

int Inside(wcPt2D p,Boundary b,wcPt2D wMin,wcPt2D wMax)
{
	switch(b)
	{
	case Left:
		if(p.x<wMin.x) return (false) ;
		break;
	case Right:
		if(p.x>wMax.x) return (false);
		break;
	case Bottom:
		if(p.y<wMin.y) return (false);
		break;
	case Top:
		if(p.y>wMax.y) return (false);
		break;
	}
	return true;
}

int Cross(wcPt2D p1,wcPt2D p2,Boundary b,wcPt2D wMin,wcPt2D wMax)
{
	if(Inside(p1,b,wMin,wMax)==Inside(p2,b,wMin,wMax))
		return (false);
	else
		return true;
}

wcPt2D Intersect(wcPt2D p1,wcPt2D p2,Boundary b,wcPt2D	wMin,wcPt2D wMax)
{
	wcPt2D iPt;
	float m;
	if(p1.x !=p2.x) m=(p2.y-p1.y)/(p2.x-p1.x);
	switch(b){
	case Left:
		iPt.x=wMin.x;
		iPt.y=p2.y+(wMin.x-p2.x)*m;
		break;
	case Right:
		iPt.x=wMax.x;
		iPt.y=p2.y+(wMax.x-p2.x)*m;
		break;
	case Bottom:
		iPt.y=wMin.y;
		if(p1.x!=p2.x)iPt.x=p2.x + (wMin.y-p2.y)/m;
		else iPt.x=p2.x;
		break;
	case Top:
		iPt.y=wMax.y;
		if(p1.x!=p2.x) iPt.x=p2.x+(wMax.y-p2.y)/m;
		else iPt.x=p2.x;
		break;
	}
	return iPt;
}
int edgeCliper(Boundary b,wcPt2D wMin,wcPt2D wMax,wcPt2D *pIn,int cnt,wcPt2D *pOut)
{
	wcPt2D s;
	int i,Outcnt=0;
	s=pIn[0];
	for(i=1;i<=cnt;i++)
	{
		if(!Inside(s,b,wMin,wMax)&&Inside(pIn[i],b,wMin,wMax))
		{
			pOut[Outcnt]=Intersect(s,pIn[i],b,wMin,wMax);
			Outcnt++;
			pOut[Outcnt]=pIn[i];
			Outcnt++;
		}
		else if(Inside(s,b,wMin,wMax)&&Inside(pIn[i],b,wMin,wMax))
		{
			pOut[Outcnt]=pIn[i];
			Outcnt++;
		}	
		else if(Inside(s,b,wMin,wMax)&&(!Inside(pIn[i],b,wMin,wMax)))
		{
			pOut[Outcnt]=Intersect(s, pIn[i], b,wMin, wMax);
			Outcnt++;
		}
		s=pIn[i];
	}
	return (Outcnt);
}

void  init (void)
{
	glClearColor(1.0,1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-400.0,400.0,-300.0,300.0);
}

void ClipPolygonSuthHodg(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	int i,cnt,Outcnt,b;
	wcPt2D points[8]={{-300,-100},{-300,150},{-100,150},{-100,100},{100,100},{100,0},{0,-100},{-300,-100}};
    cnt=7;
	wcPt2D pOut[28] ,pIn[28];	
	wcPt2D wMin={-200,0},wMax={50,200};
	for(i=0; i<4*cnt; i++)
	{
		pIn[i].x=0.0;
		pIn[i].y=0.0;
		pOut[i].x=0.0;
		pOut[i].y=0.0;
	}
    for(i=0; i<=cnt; i++) pIn[i]=points[i];
	glColor3f(0.0,0.0,0.0);
	glBegin(GL_LINE_LOOP);
	glVertex2f(wMin.x,wMin.y);
	glVertex2f(wMax.x,wMin.y);
	glVertex2f(wMax.x,wMax.y);
	glVertex2f(wMin.x,wMax.y);
	glEnd();
	glLineWidth(2.0);
	glBegin(GL_LINE_LOOP);
	for(i=0;i<cnt;i++)
		glVertex2f(pIn[i].x,pIn[i].y);
	glEnd();
	for(b=0;b<4;b++)
	{
		Outcnt=edgeCliper(Boundary(b),wMin,wMax,pIn,cnt,pOut);
		for(i=0;i<Outcnt;i++)
			pIn[i]=pOut[i];
		pIn[Outcnt]=pOut[0];
		cnt=Outcnt;
	}
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_LINE_LOOP);
	  for(i=0;i<cnt;i++)
		 glVertex2f(pOut[i].x,pOut[i].y);
	glEnd();
	glFlush();
}
void main(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowPosition(50,100);
	glutInitWindowSize (800, 600);
	glutCreateWindow ("An Example OpenGL Program");
	init();
	glutDisplayFunc (ClipPolygonSuthHodg);
	glutMainLoop ();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值