图形裁剪代码.docx
《图形裁剪代码.docx》由会员分享,可在线阅读,更多相关《图形裁剪代码.docx(14页珍藏版)》请在冰豆网上搜索。
图形裁剪代码
ͼÐβüô´úÂë.txt7ÎÂůÊÇƮƮÈ÷È÷µÄ´ºÓꣻÎÂůÊÇдÔÚÁ³ÉϵÄЦӰ£»ÎÂůÊÇÒåÎÞ·´¹ËµÄÏìÓ¦£»ÎÂůÊÇһ˿²»¹¶µÄÅäºÏ¡£8×ðÖØÊÇÒ»ÂÆ´º·ç£¬Ò»ãüÇåȪ£¬Ò»¿Å¸øÈËÎÂůµÄÊæÐÄÍ裬һ¼Á´ßÈ˷ܽøµÄÇ¿ÐļÁCPointptset[N],pt[N],pts[N],ptse[N],p[N],ptset1[N];
#defineLEFT1
#defineRIGHT2
#defineBOTTOM4
#defineTOP8
#defineXL100
#defineXR300
#defineYT100
#defineYB250
CPennewpen(PS_SOLID,1,RGB(255,0,0));
CPen*old=pDC->SelectObject(&newpen);
pDC->Rectangle(CRect(XL,YT,XR,YB));//¼ôÇд°¿Ú
ptset[0]=CPoint(120,150);
ptset[1]=CPoint(170,110);
ptset[2]=CPoint(0,190);
ptset[3]=CPoint(350,150);
ptset[4]=CPoint(0,250);
ptset[5]=CPoint(150,230);
ptset[6]=CPoint(200,50);
ptset[7]=CPoint(120,150);
ptset1[0]=CPoint(20,150);
ptset1[1]=CPoint(170,110);
ptset1[2]=CPoint(250,150);
ptset1[3]=CPoint(200,230);
ptset1[4]=CPoint(20,150);
pDC->TextOut(0,20,"Ë«»÷Êó±ê×ó¼ü£¬³öÏÖÒª¼ôÇеÄÏ߶Î");
pDC->TextOut(0,50,"Ë«»÷Êó±êÓÒ¼ü£¬³öÏÖÒª¼ôÇеĶà±ßÐÎ");
pDC->SelectObject(old);
}
CDC*pDC=GetDC();
CPennewpen(PS_SOLID,1,RGB(255,0,0));
CPen*old=pDC->SelectObject(&newpen);
for(inti=0;i{
pDC->MoveTo(ptset[i]);
pDC->LineTo(ptset[i+1]);
i++;
}
CView:
:
OnLButtonDblClk(nFlags,point);
}
CDC*pDC=GetDC();
CPennewpen(PS_SOLID,1,RGB(0,255,0));
CPen*old=pDC->SelectObject(&newpen);
floatx,y;
inti;
intcode1,code2;
RedrawWindow();
//ÇóÁ½¶ËµãËùÔÚÇøºÅcode
for(i=0;i{
intc=0;
if(ptset[i].xelseif(ptset[i].x>XR)c=c|RIGHT;
if(ptset[i].y>YB)c=c|BOTTOM;
elseif(ptset[i].ycode1=c;
c=0;
if(ptset[i+1].xelseif(ptset[i+1].x>XR)c=c|RIGHT;
if(ptset[i+1].y>YB)c=c|BOTTOM;
elseif(ptset[i+1].ycode2=c;
//Ï߶ÎÓëÇøÓòµÄÏཻÇé¿ö
if(code1!
=0&&code2!
=0&&(code1&code2)==0)
{
if((LEFT&code1)!
=0)//Ï߶ÎÓë×ó±ß½çÏཻ
{
x=XL;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((RIGHT&code1)!
=0)//Ï߶ÎÓëÓұ߽çÏཻ
{
x=XR;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((BOTTOM&code1)!
=0)//Ï߶ÎÓëϱ߽çÏཻ
{
y=YB;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y);
}
elseif((TOP&code1)!
=0)//Ï߶ÎÓëÉϱ߽çÏཻ
{
y=YT;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
}
ptset[i].x=(long)x;
ptset[i].y=(long)y;
if((LEFT&code2)!
=0)//Ï߶ÎÓë×ó±ß½çÏཻ
{
x=XL;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((RIGHT&code2)!
=0)//Ï߶ÎÓëÓұ߽çÏཻ
{
x=XR;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((BOTTOM&code2)!
=0)//Ï߶ÎÓëϱ߽çÏཻ
{
y=YB;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y);
}
elseif((TOP&code2)!
=0)//Ï߶ÎÓëÉϱ߽çÏཻ
{
y=YT;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
}
ptset[i+1].x=(long)x;
ptset[i+1].y=(long)y;
pDC->MoveTo(ptset[i].x,ptset[i].y);
pDC->LineTo(ptset[i+1].x,ptset[i+1].y);
}
if(code1==0&&code2==0)
{pDC->MoveTo(ptset[i].x,ptset[i].y);
pDC->LineTo(ptset[i+1].x,ptset[i+1].y);
}
if(code1==0&&code2!
=0)
{
pDC->MoveTo(ptset[0].x,ptset[0].y);
if((LEFT&code2)!
=0)//Ï߶ÎÓë×ó±ß½çÏཻ
{
x=XL;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((RIGHT&code2)!
=0)//Ï߶ÎÓëÓұ߽çÏཻ
{
x=XR;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((BOTTOM&code2)!
=0)//Ï߶ÎÓëϱ߽çÏཻ
{
y=YB;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y);
}
elseif((TOP&code2)!
=0)//Ï߶ÎÓëÉϱ߽çÏཻ
{
y=YT;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
}
ptset[i+1].x=(long)x;
ptset[i+1].y=(long)y;
pDC->LineTo(ptset[i+1].x,ptset[i+1].y);
}
if(code1!
=0&&code2==0)
{
pDC->MoveTo(ptset[i+1].x,ptset[i+1].y);
if((LEFT&code1)!
=0)//Ï߶ÎÓë×ó±ß½çÏཻ
{
x=XL;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((RIGHT&code1)!
=0)//Ï߶ÎÓëÓұ߽çÏཻ
{
x=XR;
y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x);
}
elseif((BOTTOM&code1)!
=0)//Ï߶ÎÓëϱ߽çÏཻ
{
y=YB;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y);
}
elseif((TOP&code1)!
=0)//Ï߶ÎÓëÉϱ߽çÏཻ
{
y=YT;
x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
}
ptset[i].x=(long)x;
ptset[i].y=(long)y;
pDC-