⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perfect.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 5 页
字号:
   C[j][3]=-P[0][j]+3*P[1][j]-3*P[2][j]+P[3][j];                           ;

   }

   V[X]=P[0][X],V[Y]=P[0][Y];
   deltat=1.0/count;
   t=0.0;
   for(i=1;i<=count;i++)
 {
  t+=deltat;

  newV[X]=C[X][0]+t*(C[X][1]+t*(C[X][2]+t*C[X][3]));
  newV[Y]=C[Y][0]+t*(C[Y][1]+t*(C[Y][2]+t*C[Y][3]));
  line(V[X],V[Y],newV[X],newV[Y]);

  V[X]=newV[X],V[Y]=newV[Y];
   }
}

/*------------------------------字符----------------------------------*/
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{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的位则显示*/
     putpixel(x+i*8+k,y+j,color);
}

 void Directory()
{
 int h;
 h=textheight('H');
 xMax=getmaxx();
 yMax=getmaxy();
 setcolor(WHITE);
 rectangle(0,2*h,xMax-1,yMax-1);
 line(0,yMax-3*h,xMax-1,yMax-3*h);
 outtextxy(xMax/2.0-30,h,"Menu");
 outtextxy(xMax/2.0-90,yMax-2*h,"Press any key to continue...");
}

void Bezier()
{
Vector P[4],Q[4],V;
 float t;
 P[0][X]=600;P[0][Y]=100;
 P[1][X]=300;P[1][Y]=200;
 P[2][X]=200;P[2][Y]=400;
 P[3][X]=100;P[3][Y]=400;

  setbkcolor(BLACK);
 setcolor(BLUE);
 outtextxy(250,10,menuStr[4]);
  setcolor(RED);
  line(P[0][X],P[0][Y],P[1][X],P[1][Y]);
  line(P[1][X],P[1][Y],P[2][X],P[2][Y]);
  line(P[2][X],P[2][Y],P[3][X],P[3][Y]);

  setcolor(GREEN);
  DisplayCubicBezierCurve(P,1000);

  setcolor(WHITE);
    putpixel(P[0][X],P[0][Y],BLUE);
  outtextxy(P[0][X],P[0][Y],"P0");
   putpixel(P[1][X],P[1][Y],BLUE);
  outtextxy(P[1][X],P[1][Y],"P1");
   putpixel(P[2][X],P[2][Y],BLUE);
  outtextxy(P[2][X],P[2][Y],"P2");
   putpixel(P[3][X],P[3][Y],BLUE);
  outtextxy(P[3][X],P[3][Y],"P3");

  Pause();
}


/*----------------------------------------多边形的填充-----------------------------------------*/
/*-----------------------------五星红旗-----------------------------------*/
Status SetStackEmpty(SqStack *s)
{s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
 if(!s->base) return overflow;
 s->top=s->base;
 s->stacksize=STACK_INIT_SIZE;
 return ok;
}

 Status PushStack(SqStack *s,SElemType e)
{if(s->top-s->base>=s->stacksize)
{s->base=(SElemType *)(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!s->base) return error;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;}
*s->top++=e;
return ok;
}

Status PopStack(SqStack *s,SElemType *e)
{ if(s->top==s->base) return error;

*e=*(--s->top);
 return ok;
}
Status IsStackEmpty(SqStack *s)
{if(s->base==s->top) return true;
 else return  false;
}
void ScanLineFill4(int x,int y,int oldColor,int newColor)
{ int xLeft,xRight,i;
 bool isLeftEndSet,spanNeedFill;
 SElemType *span;
  SqStack *s;
 /*填充并确定种子点(x,y)所在的区段*/
 i=x;
 while(getpixel(i,y)==oldColor)  /*向右填充*/
    {putpixel(i,y,newColor);
    i++;
    }
 span->xRight=i-1;   /*确定区段右边界*/
 i=x-1;
 while(getpixel(i,y)==oldColor)  /*向左填充*/
    {putpixel(i,y,newColor);
    i--;
    }
 span->xLeft=i+1;   /*确定区段左边界*/
 /*初始化*/
 SetStackEmpty(s);
 span->y=y;
 PushStack(s,*span); /*将前面生成的区段压入堆栈*/
 while(!IsStackEmpty(s)) /*终止判断*/
   {/*出栈*/
    PopStack(s,span);
    /*处理上面扫描线*/
    y=span->y+1;
    xRight=span->xRight;
    i=span->xLeft;
    isLeftEndSet=false;
    while(getpixel(i,y)==oldColor)  /*向左填充*/
       {/*if(getpixel(i+1,y)==color) break;*/
        putpixel(i,y,newColor);
        i--;
        }
    if(i!=span->xLeft)  /*确定区段左边界*/
      {isLeftEndSet=true;
       xLeft=i+1;
       }
    i=span->xLeft;
    while(i<=xRight)
       {spanNeedFill=false;
        while(getpixel(i,y)==oldColor) /*向右填充*/
          {if(!spanNeedFill)
             {spanNeedFill=true;
             if(!isLeftEndSet)
               {isLeftEndSet=true;
                xLeft=i;
                }
              }
           putpixel(i,y,newColor);
           i++;
          }
        if(spanNeedFill)
         {span->y=y;
          span->xLeft=xLeft;
          span->xRight=i-1;
          PushStack(s,*span);    /*将区段压入堆栈*/
isLeftEndSet=false;
          spanNeedFill=false;
         }
        while(getpixel(i,y)!=oldColor)
          i++;
        }

        /*end of while (i<xRight)*/
   /*处理下面的扫描线*/

        /*y=y-2;*/
        /* ->->->->->-> */
   } /*end of while(!isStackEmpty())*/
 }/*end of ScanLineBoundaryFill4()*/
 void ScanLineFill3(int x,int y,int oldColor,int newColor)
{ int xLeft,xRight,i;
 bool isLeftEndSet,spanNeedFill;
 SElemType *span;
  SqStack *s;
 /*填充并确定种子点(x,y)所在的区段*/
 i=x;
 while(getpixel(i,y)==oldColor)  /*向右填充*/
    {putpixel(i,y,newColor);
    i++;
    }
 span->xRight=i-1;   /*确定区段右边界*/
 i=x-1;
 while(getpixel(i,y)==oldColor)  /*向左填充*/
    {putpixel(i,y,newColor);
    i--;
    }
 span->xLeft=i+1;   /*确定区段左边界*/
 /*初始化*/
 SetStackEmpty(s);
 span->y=y;
 PushStack(s,*span); /*将前面生成的区段压入堆栈*/
 while(!IsStackEmpty(s)) /*终止判断*/
   {/*出栈*/
    PopStack(s,span);
    /*处理上面扫描线*/
    y=span->y-1;
    xRight=span->xRight;
    i=span->xLeft;
    isLeftEndSet=false;
    while(getpixel(i,y)==oldColor)  /*向左填充*/
       {
        putpixel(i,y,newColor);
        i--;
        }
    if(i!=span->xLeft)  /*确定区段左边界*/
      {isLeftEndSet=true;
       xLeft=i+1;
       }
    i=span->xLeft;
    while(i<=xRight)
       {spanNeedFill=false;
        while(getpixel(i,y)==oldColor) /*向右填充*/
          {if(!spanNeedFill)
             {spanNeedFill=true;
             if(!isLeftEndSet)
               {isLeftEndSet=true;
                xLeft=i;
                }
              }
           putpixel(i,y,newColor);
           i++;
          }
        if(spanNeedFill)
         {span->y=y;
          span->xLeft=xLeft;
          span->xRight=i-1;
          PushStack(s,*span);    /*将区段压入堆栈*/
          isLeftEndSet=false;
          spanNeedFill=false;
         }
        while(getpixel(i,y)!=oldColor)
          i++;
        }

        /*end of while (i<xRight)*/
   /*处理下面的扫描线*/

   } /*end of while(!isStackEmpty())*/
 }/*end of ScanLineBoundaryFill4()*/

void GetST(float x[5],float y[5],float a[5],float b[5],float radius)
{int i;
float r;
 for(i=0;i<5;i++)
 {x[i]=radius*cos(i*0.4*PI+0.05*PI)-radius*sin(i*0.4*PI+0.05*PI);
  y[i]=radius*sin(i*0.4*PI+0.05*PI)+radius*cos(i*0.4*PI+0.05*PI);
 }
 r=sin(0.1*PI)*radius/sin(126.0/180.0*PI);
  for(i=0;i<5;i++)
 {a[i]=r*cos(i*0.4*PI+0.25*PI)-r*sin(i*0.4*PI+0.25*PI);
  b[i]=r*sin(i*0.4*PI+0.25*PI)+r*cos(i*0.4*PI+0.25*PI);
  }
  /*得到五角星的10个顶点*/
  }

 /*平移旋转*/

void move(float x[5],float y[5],float a[5],float b[5],float cx,float cy)
{ int i;
 for(i=0;i<5;i++)
 {x[i]=x[i]+cx;a[i]=a[i]+cx;
  y[i]=y[i]+cy;b[i]=b[i]+cy;
 }
}

void rotate(float x[5],float y[5],float a[5],float b[5],float theta)
{ int i;
   float t1,t2;
 for(i=0;i<5;i++)
 {t1=x[i];
 x[i]=x[i]*cos(theta)-y[i]*sin(theta);
  y[i]=t1*sin(theta)+y[i]*cos(theta);
  t2=a[i];
  a[i]=a[i]*cos(theta)-b[i]*sin(theta);
  b[i]=t2*sin(theta)+b[i]*cos(theta);
 }
}
 void CreatET(float x[5],float y[5],float a[5],float b[5])
 {int n;
 Edge *e,*h;
 n=(int)(y[3]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(b[3]+0.5);e->x=x[3];e->deltax=(a[3]-x[3])/(b[3]-y[3]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(b[2]+0.5);h->x=x[3];h->deltax=(a[2]-x[3])/(b[3]-y[3]);
 e->nextEdge=h;h->nextEdge=NULL;
 EL[n]=e;


 n=(int)(b[2]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(b[1]+0.5);e->x=x[2];e->deltax=(a[1]-x[2])/(b[1]-y[2]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(b[4]+0.5);h->x=x[4];h->deltax=(a[4]-x[4])/(b[4]-y[4]);
 e->nextEdge=h;h->nextEdge=NULL;
 EL[n]=e;


  n=(int)(b[1]+0.5);
e=(Edge*)malloc(sizeof(Edge));
e->ymax=(int)(y[1]+0.5);e->x=a[1];e->deltax=(x[1]-a[1])/(y[1]-b[1]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(y[0]+0.5);h->x=a[4];h->deltax=(x[0]-a[4])/(y[0]-b[4]);
 e->nextEdge=h;h->nextEdge=NULL;
 EL[n]=e;


  n=(int)(b[0]+0.5);
 e=(Edge*)malloc(sizeof(Edge));
 e->ymax=(int)(y[1]+0.5);e->x=a[0];e->deltax=(x[1]-a[0])/(y[1]-b[0]);
 h=(Edge*)malloc(sizeof(Edge));
 h->ymax=(int)(y[0]+0.5);h->x=a[0];h->deltax=(x[0]-a[0])/(y[0]-b[0]);
 e->nextEdge=h;h->nextEdge=NULL;
 EL[n]=e;
 }

 void Free(float y[5],float b[5])
 {int n;
 n=(int)(y[3]+0.5);
 EL[n]=NULL;
 n=(int)(b[2]+0.5);
 EL[n]=NULL;
 n=(int)(b[1]+0.5);
 EL[n]=NULL;
 n=(int)(b[0]+0.5);
 EL[n]=NULL;
}

 void Fillin(Edge *AEL)
 {Edge *e;
  Edge *p,*q;
  int n,m;
  setcolor(YELLOW);
 AEL=(Edge*)malloc(sizeof(Edge));AEL->x=-1;
AEL->nextEdge=NULL;

for(n=49;n<120;n++)
{p=EL[n];
 while(p)
 {
e=AEL; q=AEL->nextEdge;
while(q)
  {if(p->x<q->x)break;
   q=q->nextEdge;
   e=e->nextEdge;}
   q=p->nextEdge;
   p->nextEdge=e->nextEdge;
   e->nextEdge=p;
   p=q;

}
q=AEL->nextEdge;
p=AEL;q=AEL->nextEdge;
m=1;

 while(q)
 { if(q->ymax==n)
   {p->nextEdge=q->nextEdge;
   e=q;
   q=q->nextEdge;free(e);
   }
 else{
   if(p!=AEL&&m%2==0)
   {line((int)(p->x+0.5),n,(int)(q->x+0.5),n);
    p->x+=p->deltax;
    q->x+=q->deltax;}
    p=p->nextEdge;
    q=q->nextEdge;
    m++;}
 }
 }
 }


int  Edge_code(point node,point start,point end)
 {int w,v,s=0;
 point p;
 float x0,y0;
   if(start.x>node.x&&start.y>node.y) v=0;
  if(start.x<=node.x&&start.y>node.y) v=1;
  if(start.x<=node.x&&start.y<=node.y) v=2;
  if(start.x>node.x&&start.y<=node.y) v=3;

   if(end.x>node.x&&end.y>node.y) w=0;
  if(end.x<=node.x&&end.y>node.y) w=1;
  if(end.x<=node.x&&end.y<=node.y) w=2;
  if(end.x>node.x&&end.y<=node.y) w=3;
   s=w-v;
   if(s==2||s==-2)
   {
      x0=(end.x+start.x)/2;
      y0=(end.y+start.y)/2;
      p.x=x0;p.y=y0;
   s=Edge_code(node,start,p)+Edge_code(node,p,end);
   }
   if(s==3)  s=-1;
   if(s==-3) s=1;
   return s;

 }
  point Getmax(point points[10])
  { int i;
   float maxx,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -