//Sutherland_Hodgman算法的多边形裁剪
void CPaintView::Sutherland_Hodgman_Clipping(vector<CPoint> &invertexlist,vector<CPoint>&outvertexlist,int inlength,int*outlength,CPoint*clipboundary)
{
CPoint s,p,i;
int j;
*outlength = 0;
s=invertexlist[inlength-1];//存放输入多边形顶点的数据,长度为inlength
for(j=0;j<inlength;j++)//j=0,......,j=inlength-1.共循环inlength次,依次处理多边形各顶点
{
p=invertexlist[j];//p为当前要处理的顶点,前面已处理过的顶点为s
if(inside(p,clipboundary))//若p在裁剪边clipboundary的内侧,则inside()返回值为真
{ if(inside(s,clipboundary))//如果s在裁剪边clipboundary的内侧,则inside()返回值为真
outputvertex(p,outlength,outvertexlist);
else//s在裁剪边clipboundary的外侧
{
intersect(s,p,clipboundary,&i);//求交点i
outputvertex(i,outlength,outvertexlist);//保存i点
outputvertex(p,outlength,outvertexlist);//保存p点
}
}
else//p在裁剪边clipboundary的外侧
{
trunc函数怎么切除小数点后几位if(inside(s,clipboundary))
{
intersect(s,p,clipboundary,&i);
outputvertex(i,outlength,outvertexlist);
}
}
s=p;
}
}
bool CPaintView::inside(CPoint testvertex,CPoint* clipboundary)
{
if(clipboundary[1].x<clipboundary[0].x)
if(testvertex.y >=clipboundary[0].y)
return true;
if(clipboundary[1].x>clipboundary[0].x)
if(testvertex.y<=clipboundary[0].y)
return true;
if(clipboundary[1].y<clipboundary[0].y)
if(testvertex.x<=clipboundary[0].x)
return true;
if(clipboundary[1].y>clipboundary[0].y)
if(testvertex.x>=clipboundary[0].x)
return true;
return false;
}
void CPaintView::outputvertex(CPoint outvertex,int *outlength,vector<CPoint>&outvertexlist)
{
// outvertexlist[*outlength]=outvertex;//======++++++++++++++++++++++++++++++==
outvertexlist.insert(outvertexlist.begin( )+(*outlength),outvertex);
(*outlength)++;
}
void CPaintView::intersect(CPoint p1,CPoint p2,CPoint *clipboundary,CPoint *intersectpt)
{
if(clipboundary[0].y==clipboundary[1].y)
{
intersectpt->y = clipboundary[0].y;
intersectpt->x = p1.x + (int)((clipboundary[0].y-p1.y)*((float)(p2.x-p1.x)/(float)(p2.y-p1.y)));
}
else
{
intersectpt->x=clipboundary[0].x;
intersectpt->y=p1.y+(int)((clipboundary[0].x-p1.x)*((float)(p2.y-p1.y)/(float)(p2.x-p1.x)));
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。