📄 draw4view.cpp
字号:
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
// pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
}
x = a;
y = 0;
d = 4*(sa - sb*a) + sb;
if(y+y0>212)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
while(y <= yP)
{
if(d <= 0)
d += 4*sa*(2*y + 3);
else
{
d += 4*sa*(2*y + 3) - 8*sb*(x - 1);
x --;
}
y ++;
if(y+y0>212)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
}
}
void CDraw4View::Draw13(int x0, int x1, int y0, int y1)
{
int dx,dy,dxy,s1,s2,flag,i,d,x,y,color = 0;
CDC* pDC = GetDC();
x = x0;
y = y0;
dx = abs(x1 - x0);
dy = abs(y1 - y0);
s1 = (x1 - x0) > 0 ? 1:-1;
s2 = (y1 - y0) > 0 ? 1:-1;
if(dy > dx)
{
dxy = dx;dx = dy;dy = dxy;
flag = 1;
}
else
flag = 0;
d = 2*dy - dx;
for(i = 1;i <= dx;i++)
{
pDC->SetPixel(x,y,color);
while(d >= 0)
{
if(flag == 1)
x += s1;
else
y += s2;
d += -2*dx;
}
if(flag == 1)
y += s2;
else
x += s1;
d += 2*dy;
}
}
void CDraw4View::DisplaySquareBezier(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, int m)
{//三次Bezier曲线
float C[2][4],t,dx,Newx,Newy;
int Vx,Vy,Nx,Ny,i,j;
C[0][0]=x0;
C[0][1]=-3*x0+3*x1;
C[0][2]=3*x0-6*x1+3*x2;
C[0][3]=-1*x0+3*x1-3*x2+x3;
C[1][0]=y0;
C[1][1]=-3*y0+3*y1;
C[1][2]=3*y0-6*y1+3*y2;
C[1][3]=-1*y0+3*y1-3*y2+y3;
Vx=x0,Vy=y0;
dx=1.0/m,t=0.0;
for(i=1;i<=m;i++)
{t+=dx;
Newx=C[0][0]+t*(C[0][1]+t*(C[0][2]+t*C[0][3]));
Newy=C[1][0]+t*(C[1][1]+t*(C[1][2]+t*C[1][3]));
Nx=(int)(Newx+0.5),Ny=(int)(Newy+0.5);
LineDDA1(Vx,Vy,Nx,Ny);
Vx=Nx,Vy=Ny;
}
}
void CDraw4View::LineDDA1(int x0, int y0, int x1, int y1)
{int x,color = 0;
float dy,dx,y,m,t;
CDC*pDC=GetDC();
if(x0==x1) /*斜率不存在的情形*/
{if(y0>y1)
{t=y0;y0=y1;y1=t;} /*使得y0<y1*/
for(x=x0,y=y0;y<y1;y++)
{
pDC->SetPixel(x+272+100,y-5+13,color);
pDC->SetPixel(342-y+100,x+9+13,color);
pDC->SetPixel(-x+328+100,-y+79+13,color);
pDC->SetPixel(y+258+100,-x+65+13,color);
}
}
else /*有斜率的情形*/
{
dx=x1-x0;
dy=y1-y0;
m=dy/dx;
y=y0; /*计算斜率值*/
if(x0>x1)
{
t=x0;x0=x1,y=y1;x1=t;
} /*使得x0<x1*/
for(x=x0;x<=x1;x++)
{
pDC->SetPixel(x+272+100,(int)(y-5+0.5)+13,color);
pDC->SetPixel((int)(-y+342+0.5)+100,x+9+13,color);
pDC->SetPixel(-x+328+100,(int)(-y+79+0.5)+13,color);
pDC->SetPixel((int)(y+258+0.5)+100,-x+65+13,color);
y+=m;
}
}
}
void CDraw4View::LineDDA2(int x0, int y0, int x1, int y1)
{int x,color = 0;
float dy,dx,y,m,t;
CDC*pDC=GetDC();
if(x0==x1) /*斜率不存在的情形*/
{if(y0>y1)
{t=y0;y0=y1;y1=t;} /*使得y0<y1*/
for(x=x0,y=y0;y<y1;y++)
{
if(y<463)
{
pDC->SetPixel(x+20,y,color);
}
}
}
else /*有斜率的情形*/
{
dx=x1-x0;
dy=y1-y0;
m=dy/dx;
y=y0; /*计算斜率值*/
if(x0>x1)
{
t=x0;x0=x1,y=y1;x1=t;
} /*使得x0<x1*/
for(x=x0;x<=x1;x++)
{
if(y<463)
{
pDC->SetPixel(x+20,(int)(y+0.5),color);
}
y+=m;
}
}
}
void CDraw4View::DisplaySquareBezier2(int x0, int y0, int x1, int y1, int x2, int y2, int m)
{//三次Bezier曲线
float C[2][3],t,dx,Newx,Newy;
int Vx,Vy,Nx,Ny,i,j;
C[0][0]=x0;
C[0][1]=-3*x0+3*x1;
C[0][2]=3*x0-6*x1+3*x2;
C[1][0]=y0;
C[1][1]=-3*y0+3*y1;
C[1][2]=3*y0-6*y1+3*y2;
Vx=x0,Vy=y0;
dx=1.0/m,t=0.0;
for(i=1;i<=m;i++)
{t+=dx;
Newx=C[0][0]+t*(C[0][1]+t*(C[0][2]));
Newy=C[1][0]+t*(C[1][1]+t*(C[1][2]));
Nx=(int)(Newx+0.5),Ny=(int)(Newy+0.5);
LineDDA2(Vx,Vy,Nx,Ny);
Vx=Nx,Vy=Ny;
}
}
void CDraw4View::LineDDA3(int x0, int y0, int x1, int y1)
{int x,color = 0;
float dy,dx,y,m,t;
CDC*pDC=GetDC();
if(x0==x1) /*斜率不存在的情形*/
{if(y0>y1)
{t=y0;y0=y1;y1=t;} /*使得y0<y1*/
for(x=x0,y=y0;y<y1;y++)
{
if(y<450)
{
pDC->SetPixel(x-20,y,color);
}
}
}
else /*有斜率的情形*/
{
dx=x1-x0;
dy=y1-y0;
m=dy/dx;
y=y0; /*计算斜率值*/
if(x0>x1)
{
t=x0;x0=x1,y=y1;x1=t;
} /*使得x0<x1*/
for(x=x0;x<=x1;x++)
{
if(y<450)
{
pDC->SetPixel(x-20,(int)(y+0.5),color);
}
y+=m;
}
}
}
void CDraw4View::DisplaySquareBezier3(int x0, int y0, int x1, int y1, int x2, int y2, int m)
{//三次Bezier曲线
float C[2][3],t,dx,Newx,Newy;
int Vx,Vy,Nx,Ny,i,j;
C[0][0]=x0;
C[0][1]=-3*x0+3*x1;
C[0][2]=3*x0-6*x1+3*x2;
C[1][0]=y0;
C[1][1]=-3*y0+3*y1;
C[1][2]=3*y0-6*y1+3*y2;
Vx=x0,Vy=y0;
dx=1.0/m,t=0.0;
for(i=1;i<=m;i++)
{t+=dx;
Newx=C[0][0]+t*(C[0][1]+t*(C[0][2]));
Newy=C[1][0]+t*(C[1][1]+t*(C[1][2]));
Nx=(int)(Newx+0.5),Ny=(int)(Newy+0.5);
LineDDA3(Vx,Vy,Nx,Ny);
Vx=Nx,Vy=Ny;
}
}
void CDraw4View::Draw14(int a, int b)
{
int x,y,d,xP,yP,sa,sb,color = 0;
int x0 = 300,y0 = 210;
CDC* pDC = GetDC();
sa = a*a;sb = b*b;
xP = (int)(0.5 + (float)sa/sqrt((float)(sa + sb)));
yP = (int)(0.5 + (float)sb/sqrt((float)(sa + sb)));
x = 0;
y = b;
d = 4*(sb - sa*b) + sa;
if(y+y0>269)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
while(x <= xP)
{
if(d <= 0)
d += 4*sb*(2*x + 3);
else
{
d += 4*sb*(2*x + 3) - 8*sa*(y - 1);
y --;
}
x ++;
if(y+y0>269)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
}
x = a;
y = 0;
d = 4*(sa - sb*a) + sb;
if(y+y0>269)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
while(y <= yP)
{
if(d <= 0)
d += 4*sa*(2*y + 3);
else
{
d += 4*sa*(2*y + 3) - 8*sb*(x - 1);
x --;
}
y ++;
if(y+y0>269)
{
pDC->SetPixel(x + x0,y + y0,color);
pDC->SetPixel(-x + x0,y + y0,color);
//pDC->SetPixel(-x + x0,-y + y0,color);
//pDC->SetPixel(x + x0,-y + y0,color);
}
}
}
void CDraw4View::DisplaySquareBezier4(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, int m)
{//三次Bezier曲线
float C[2][4],t,dx,Newx,Newy;
int Vx,Vy,Nx,Ny,i,j;
C[0][0]=x0;
C[0][1]=-3*x0+3*x1;
C[0][2]=3*x0-6*x1+3*x2;
C[0][3]=-1*x0+3*x1-3*x2+x3;
C[1][0]=y0;
C[1][1]=-3*y0+3*y1;
C[1][2]=3*y0-6*y1+3*y2;
C[1][3]=-1*y0+3*y1-3*y2+y3;
Vx=x0,Vy=y0;
dx=1.0/m,t=0.0;
for(i=1;i<=m;i++)
{t+=dx;
Newx=C[0][0]+t*(C[0][1]+t*(C[0][2]+t*C[0][3]));
Newy=C[1][0]+t*(C[1][1]+t*(C[1][2]+t*C[1][3]));
Nx=(int)(Newx+0.5),Ny=(int)(Newy+0.5);
LineDDA4(Vx,Vy,Nx,Ny);
Vx=Nx,Vy=Ny;
}
}
void CDraw4View::LineDDA4(int x0, int y0, int x1, int y1)
{int x,color = 0;
float dy,dx,y,m,t;
CDC*pDC=GetDC();
if(x0==x1) /*斜率不存在的情形*/
{if(y0>y1)
{t=y0;y0=y1;y1=t;} /*使得y0<y1*/
for(x=x0,y=y0;y<y1;y++)
{
pDC->SetPixel(x+272,y-5,color);
}
}
else /*有斜率的情形*/
{
dx=x1-x0;
dy=y1-y0;
m=dy/dx;
y=y0; /*计算斜率值*/
if(x0>x1)
{
t=x0;x0=x1,y=y1;x1=t;
} /*使得x0<x1*/
for(x=x0;x<=x1;x++)
{
pDC->SetPixel(x+272,(int)(y-5+0.5),color);
y+=m;
}
}
}
void CDraw4View::drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
CDC *pDC = GetDC();
int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*测试为1的位则显示*/
pDC->SetPixel(x+i*8+k,y+j,color);
}
void CDraw4View::Scan(CPoint point, int oldColor, int newColor)
{
int i;
i=point.x;
CDC* pDC=GetDC();
while(pDC->GetPixel(point)==oldColor)
{pDC->SetPixel(point,newColor);
point.x++;
}
span.xRight=point.x-1;
point.x=i-1;
while(pDC->GetPixel(point)==oldColor)
{pDC->SetPixel(point,newColor);
point.x--;
}
span.xLeft=point.x+1;
span.y=point.y;
}
void CDraw4View::ScanNext(int oldColor, int newColor)
{
int i,xLeft,xRight,y;
bool isScan=FALSE;
CPoint point;
xLeft=span.xLeft;xRight=span.xRight;
y=span.y;
CDC* pDC=GetDC();
for(i=xLeft;i<=xRight;i++)
{if(pDC->GetPixel(i,y+1)==oldColor)
{isScan=TRUE;
point.x=i;point.y=y+1;
Scan(point, oldColor, newColor);
}
if(isScan)
ScanNext(oldColor,newColor);
isScan=FALSE;
if(pDC->GetPixel(i,y-1)==oldColor)
{isScan=TRUE;
point.x=i;point.y=y-1;
Scan(point, oldColor, newColor);
}
if(isScan)
ScanNext(oldColor,newColor);
}
}
void CDraw4View::Fill(CPoint point, int oldColor, int newColor)
{
Scan(point,oldColor,newColor);
ScanNext(oldColor,newColor);
}
void CDraw4View::Intersect(Vertex * s, Vertex * p, Edge clipBoundary, Vertex * I)
//求多边形的边SP与裁剪边的交点I
{
if(clipBoundary[0].y==clipBoundary[1].y) /*水平裁剪边*/
{
I->y=clipBoundary[0].y;
I->x=s->x+(clipBoundary[0].y-s->y)*(p->x-s->x)/(p->y-s->y);
}
else /*竖直裁剪边*/
{
I->x=clipBoundary[0].x;
I->y=s->y+(clipBoundary[0].x-s->x)*(p->y-s->y)/(p->x-s->x);
}
}
bool CDraw4View::Inside(Vertex * testVertex, Edge clipBoundary)
{
if(clipBoundary[1].x>clipBoundary[0].x) /*裁剪边为窗口的下边*/
{ if(testVertex->y>=clipBoundary[0].y)
return true;
}
else if(clipBoundary[1].x<clipBoundary[0].x) /*裁剪边为窗口的上边*/
{
if(testVertex->y<=clipBoundary[0].y)
return true;
}
else if(clipBoundary[1].y>clipBoundary[0].y) /*裁剪边为窗口的右边*/
{
if(testVertex->x<=clipBoundary[0].x)
return true;
}
else if(clipBoundary[1].y<clipBoundary[0].y) /*裁剪边为窗口的左边*/
{
if(testVertex->x>=clipBoundary[0].x)
return true;
}
return false;
}/*程序Inside()结束*/
void CDraw4View::Output(Vertex * newVertex, int * outLength, VertexArray outVertexArray)
/*将new Vertex加入到结果多边形顶点表outVertexArray*/
{
outVertexArray[* outLength].x=newVertex->x;
outVertexArray[* outLength].y=newVertex->y;
/*CString cs;int i=0;
cs.Format("%d",outVertexArray[* outLength].x);
CDC *pDC;
pDC=GetDC();
pDC->TextOut(100+10*i,50,cs);*/
(* outLength)++;
//i++;
}/*程序Output()结束*/
void CDraw4View::SutherlandHodgmanEllipseClip(int inLength, VertexArray inVertexArray,int * outLength, VertexArray outVertexArray, Edge clipBoundary)
/*inVertexArray为输入多边形顶点数组,outVertexArray为裁剪结果多边形顶点数组,clipBoundary为裁剪边*/
{
Vertex *s,*p,I;
int j;
*outLength=0;
s=&(inVertexArray[inLength-1]);
for(j=0;j<inLength;j++)
{
p=&(inVertexArray[j]);
if(Inside(p,clipBoundary))
{
if(Inside(s,clipBoundary))
Output(p,outLength,outVertexArray);/*p119书上的情况1*/
else
{
Intersect(s,p,clipBoundary,&I);/*p119书上的情况4*/
Output(&I,outLength,outVertexArray);
Output(p,outLength,outVertexArray);
}
}
else if(Inside(s,clipBoundary))
{
Intersect(s,p,clipBoundary,&I); /*p119书上的情况2*/
Output(&I,outLength,outVertexArray);
}/*p119书上的情况3,没有输出*/
s=p;
}
}/*程序SutherlandHodgmanEllipseClip()结束*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -