- 博客(0)
- 资源 (2)
空空如也
mfc梁友栋算法切割
计算机图形学中的梁友栋算法实现
CClientDC dc(this);
int x0=m_nStart.x, y0=m_nStart.y; //起点
int x1=m_nEnd.x,y1=m_nEnd.y; //终点
int x=x0,y=y0;
dc.SetPixel(x0,y0,RGB(255,0,255));
double m;
m=1.0*(y0-y1)/(x1-x0);
if (x0<x1)
{
if((m>=0&&m<=1)||(m>=-1&&m<0))
{
double a=y;
for(int i=0;i<abs(x1-x0)-1;i++)
{
x+=1; a=a-m;
dc.SetPixel(x,int(a+0.5),RGB(255,0,255));
}
}
else if(m>1)
{
double a=x;
for (int i=0;i<abs(y1-y0)-1;i++)
{
y-=1;a=a+1/m;
dc.SetPixel(int(a+0.5),y,RGB(255,0,255));
}
}
else if(m<-1)
{
double a=x;
for (int i=0;i<abs(y1-y0)-1;i++)
{
y+=1;a=a-1/m;
dc.SetPixel(int(a+0.5),y,RGB(255,0,255));
}
}
}
if(x0==x1)
if(y0<y1)
for (int i=0;i<abs(y1-y0)-1;i++)
{
y+=1;
dc.SetPixel(x0,y,RGB(255,0,255));
}
else
for (int i=0;i<abs(y1-y0)-1;i++)
{
y-=1;
dc.SetPixel(x0,y,RGB(255,0,255));
}
if (x0>x1)
{
if((m>=0&&m<=1)||(m>=-1&&m<0))
{
double a=y;
for(int i=0;i<abs(x1-x0)-1;i++)
{
x-=1; a=a+m;
dc.SetPixel(x,int(a+0.5),RGB(255,0,255));
}
}
else if(m>1)
{
double a=x;
for (int i=0;i<abs(y1-y0)-1;i++)
{
y+=1; a=a-1/m;
dc.SetPixel(int(a+0.5),y,RGB(255,0,255));
}
}
else if(m<-1)
{
double a=x;
for (int i=0;i<abs(y1-y0)-1;i++)
{
y-=1; a=a+1/m;
dc.SetPixel(int(a+0.5),y,RGB(255,0,255));
}
}
}
CView::OnLButtonUp(nFlags, point);
}
void CDDAView::LiangBarsky(CPoint Ps,CPoint Pe,CPoint& Us,CPoint& Ue)
{
double u1,u2; //要求的u值
int pL,pR,pB,pT,qL,qR,qB,qT;
//求pL,pR,pB,pT
pL=Ps.x-Pe.x;
pR=-pL;
pB=Ps.y-Pe.y;
pT=-pB;
//求qL,qR,qB,qT
qL=Ps.x-A.x;
qR=D.x-Ps.x;
qB=Ps.y-A.y;
qT=B.y-Ps.y;
double uL,uB,uR,uT;
//求uL,uB,uR,uT;
if (pL<0)
{
if(pB<0)
{
uL=double(qL)/pL;
uB=double(qB)/pB;
u1=GetMaxU(uL,uB,0.0);
uR=double(qR)/pR;
uT=double(qT)/pT;
u2=GetMinU(uR,uT,1.0);
}
else
{
uL=double(qL)/pL;
uT=double(qT)/pT;
u1=GetMaxU(uL,uT,0.0);
uR=double(qR)/pR;
uB=double(qB)/pB;
u2=GetMinU(uR,uB,1.0);
}
}
else
{
if (pB>0)
{
uL=double(qL)/pL;
uB=double(qB)/pB;
u2=GetMinU(uL,uB,1);
uR=double(qR)/pR;
uT=double(qT)/pT;
u1=GetMaxU(uR,uT,0);
}
else
{
uL=double(qL)/pL;
uT=double(qT)/pT;
u2=GetMinU(uL,uT,1);
uR=double(qR)/pR;
uB=double(qB)/pB;
u1=GetMaxU(uR,uB,0);
}
}
double x1,x2,y1,y2;
x1=Ps.x+u1*(Pe.x-Ps.x);
y1=Ps.y+u1*(Pe.y-Ps.y);
x2=Ps.x+u2*(Pe.x-Ps.x);
y2=Ps.y+u2*(Pe.y-Ps.y);
Us.x=int(x1+0.5);
Us.y=int(y1+0.5);
Ue.x=int(x2+0.5);
Ue.y=int(y2+0.5);
}
2012-12-23
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人