计算机图形学实验报告 直线段的裁剪
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
直线段的裁剪
CaaaaView::CaaaaView()
{
m_pntCount=0;
xl=200;
xr=600;
yb=100;
yt=350;
}
void MidBresenhamLine(CDC *pDC,int x0,int y0,int x1,int y1){ int dx,dy,d,up,down,x,y;
if(x0>x1){
x=x1;x1=x0;x0=x;
y=y1;y1=y0;y0=y;
}
x=x0;y=y0;
dx=x1-x0;dy=y1-y0;
d=dx-2*dy;
up=2*x-2*y;down=-2*dy;
while(x<=x1){
pDC->SetPixel(x,y,355);
x++;
if(d<0){
y++;
d+=up;
}
else d+=down;
}
}
int LBLineClipTest(float p,float q,float &umax,float &umin) {
float r=0.0;
if(p<0.0){
r=q/p;
TRACE(_T("r=%.4f\n"),r);
if(r>umin) return 0;
else if(r>umax) umax=r;
}
else if(p>0.0){
r=q/p;
TRACE(_T("r=%.4f\n"),r);
if(r else if(r } else if(q<0.0) return 0; return 1; } void LBLineClip(CDC* pDC,float xl,float xr,float yb,float yt,float x1,float y1,float x2,float y2) { float umax,umin,deltax,deltay; deltax=x2-x1; deltay=y2-y1; umax=0.0; umin=1.0; if( LBLineClipTest(-deltax,x1-xl,umax,umin)){ if( LBLineClipTest(deltax,xr-x1,umax,umin)){ if( LBLineClipTest(-deltay,y1-yb,umax,umin)){ if( LBLineClipTest(deltay,yt-y1,umax,umin)){ x1=int(x1+umax*deltax+0.5); y1=int(y1+umax*deltay+0.5); x2=int(x1+umin*deltax+0.5); y2=int(y1+umin*deltay+0.5); } MidBresenhamLine(pDC,x1,y1,x2,y2); } } } } void CaaaaView::OnLButtonDown(UINT nFlags, CPoint point) { //AfxMessageBox(_T("click")); m_pnts[m_pntCount]=point; m_pntCount++; if(m_pntCount>1) { CDC *pDC=GetDC(); LBLineClip(pDC,xl,xr,yb,yt,m_pnts[m_pntCount-2].x,m_pnts[m_pntCount-2].y,m_pnts[m_pntCo unt-1].x,m_pnts[m_pntCount-1].y); ReleaseDC(pDC); m_pntCount=0; } CView::OnLButtonDown(nFlags, point); }