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

📄 newmorning.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 4 页
字号:
 int c;
 c=getpixel(x,y);
 if((c!=boundary_color)&&(c!=fill_color))
  {
    putpixel(x,y,fill_color);
    seedfilling(x+1,y,fill_color,boundary_color);
    seedfilling(x-1,y,fill_color,boundary_color);
    seedfilling(x,y+1,fill_color,boundary_color);
    seedfilling(x,y-1,fill_color,boundary_color);
   }
}
/*下面为线型控制*/
void putpoint(int i,int x,int y,int color)  /*设置线型*/
{ int LineStyle[]={1,1,1,0,0,0,1,1,0,0,1,0,0,1,1,1};
 if(LineStyle[i%16])
 putpixel(x,(int)(y+0.5),color);
}

void LineDDA(int x0,int y0,int x1,int y1,int color)   /*画直线*/
{int x,i;
float dy,dx,y,m;
dx=x1-x0;
dy=y1-y0;
m=dy/dx;
y=y0;
 i=0;
for(x=x0;x<=x1;x++)
{ i++;
 putpoint(i,x,(int)(y+0.5),color);
 putpixel(x+50,(int)(y+0.5),color);

 y+=m;
}
}

/*下面为图像填充*/

void picturefilling(int x0,int y0,int x,int y,int fill_color,int boundary_color)   /*将字符填充到指定区域里*/
{
 int c;
 c=getpixel(x,y);
 if((c!=fill_color)&&(c!=boundary_color))
  { if(pattern[abs((y-y0)%9)][abs((x-x0)%7)])
   {
    putpixel(x,y,fill_color);
    picturefilling(x0,y0,x+1,y,fill_color,boundary_color);
    picturefilling(x0,y0,x-1,y,fill_color,boundary_color);
    picturefilling(x0,y0,x,y+1,fill_color,boundary_color);
    picturefilling(x0,y0,x,y-1,fill_color,boundary_color);
    }
    else
    {
    putpixel(x,y,WHITE);
    picturefilling(x0,y0,x+1,y,fill_color,boundary_color);
    picturefilling(x0,y0,x-1,y,fill_color,boundary_color);
    picturefilling(x0,y0,x,y+1,fill_color,boundary_color);
    picturefilling(x0,y0,x,y-1,fill_color,boundary_color);
    }
   }
}

tianchong()
{int poly[22];int p[8];
 initgr();
 cleardevice();
 setbkcolor(BLUE);  /*设置背景颜色*/
  p[0]=x[0]=140;p[2]=x[1]=350;p[4]=x[2]=140;p[6]=x[3]=140;x[4]=x[1];       /*第一个点的x坐标以及y坐标*/
  p[1]=y[0]=370;p[3]=y[1]=440;p[5]=y[2]=470;p[7]=y[3]=370;y[4]=y[1];
  drawpoly(4,p);

  /*显示各点连接起来的多边形*/
    fill_area(3);
 circle(330,210,190); /*画最外面的圆作为头像的脸*/
 /*下面为递归填充*/
 poly[0]=getmaxx()/2+10;poly[1]=getmaxy()/2+70; /*A*/
 poly[2]=poly[0]+30;poly[3]=poly[1]+20;     /*B*/
 poly[4]=poly[2]+20;poly[5]=poly[3];         /*C*/
 poly[6]=poly[2]+10;poly[7]=poly[5]+20;     /*D*/
 poly[8]=poly[4];poly[9]=poly[7]+20;          /*E*/
 poly[10]=poly[0];poly[11]=poly[7];           /*F*/
 poly[12]=poly[0]-50;poly[13]=poly[9];         /*G*/
 poly[14]=poly[0]-40;poly[15]=poly[7];         /*H*/
 poly[16]=poly[12];poly[17]=poly[5];            /*I*/
 poly[18]=poly[0]-30;poly[19]=poly[5];
 poly[20]=poly[0];poly[21]=poly[1];     /*画五角星的各个顶点*/
 drawpoly(11,poly);
 seedfilling(310,330,RED,15);  /*填充五角星*/

 circle(230,140,35);
 seedfilling(230,140,YELLOW,15);
 circle(230,140,20);
 seedfilling(230,140,GREEN,15);
 circle(230,140,5);
 seedfilling(230,140,WHITE,15);   /*填充头像的左眼睛*/

 circle(410,140,35);
 seedfilling(410,140,YELLOW,15);
 circle(410,140,20);
 seedfilling(410,140,GREEN,15);
 circle(410,140,5);
 seedfilling(410,140,WHITE,15); /*填充头像的右眼睛*/

 ellipse(325,225,0,360,36,46);
 seedfilling(325,225,GREEN,15); /*填充头像的鼻子*/


 /*下面为线型算法*/
 LineDDA(12,18,130,250,WHITE);
 LineDDA(472,18,590,250,WHITE);
 LineDDA(222,18,370,250,WHITE);
  LineDDA(272,238,420,470,WHITE);
 /*下面为图形填充*/
/*circle(540,400,40);
picturefilling(540,400,540,400,RED,15);
  circle(100,320,40);
picturefilling(100,320,100,320,RED,15);*/
 /*下面为扫描线填充*/


  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
    /* 恢复TEXT屏幕模式 */

jingzhimenu();

}

/***************************************************/
/*******************************************************/



/*旋转*/
/***************************************************************/
/****************************************************************/
void setidentity( struct matrix *m)
 { int i,j;
   for (i=0;i<3;i++)
     for (j=0;j<3;j++)
     m->h[i][j]=(i==j);    
 }
 void show(struct matrix *m)
{ int i,j;
   for (i=0;i<3;i++)
      { printf("\n");        
      for(j=0;j<3;j++)
      printf("%f\t",m->h[i][j]);
      }
    printf("\n");
}
void  multiple(struct matrix *a,struct matrix *b)
 { int r,c,t; float ab,a1,b1;struct matrix *m;
    for (r=0;r<3;r++)
    for (c=0;c<3;c++)
    { ab=0.0;
     for (t=0;t<3;t++)
       { a1=(a->h[r][t]);  b1=(b->h[t][c]);  ab=a1*b1+ab; }   
        m->h[r][c]=ab;
     }
  for (r=0;r<3;r++)
  for (c=0;c<3;c++)
  b->h[r][c]=m->h[r][c];      
}
void translate(float tx,float ty,struct matrix *thematrix)
{ struct matrix *m;
   setidentity(m);
   m->h[0][2]=tx;   m->h[1][2]=ty;  
   multiple(m,thematrix);  
 }
 void translatee(float tx,float ty,struct matrix *thematrix)
{  struct matrix *m;  
   setidentity(m);
   m->h[0][2]=-tx;   m->h[1][2]=-ty;
   multiple(m,thematrix);  
 }
void scale(float tx,float ty,struct matrix *thematrix)
{ struct matrix *m;
 setidentity(m);
 m->h[0][0]=tx; m->h[1][1]=ty;      
  multiple(m,thematrix);     
 }
 void rotate(float t,struct matrix *thematrix)
 {  float a;struct matrix *m;
    setidentity(m);a=radius( t);
    m->h[0][0]=cos(a);m->h[0][1]=-sin(a);
    m->h[1][0]=sin(a);m->h[1][1]=cos(a);
    multiple(m,thematrix);     
  } 
 void transformpoints(int n,struct wc *pts,struct matrix *thematrix)
 {  int k,j;float a,b,ss1,tt1; 
    for(j=0;j<n;pts++,j++)
    {  a=pts->x; b=pts->y;
       tt1=thematrix->h[0][0]*a+thematrix->h[0][1]*b+thematrix->h[0][2];      
       ss1=thematrix->h[1][0]*a+thematrix->h[1][1]*b+thematrix->h[1][2];
       pts->x=tt1;pts->y=ss1;         
     }  
} 
void setarea(int num,struct wc *pts,int color)
   { int i; 
    for (i=0;i<num-1;pts++,i++)       
    line(pts->x,pts->y,(pts+1)->x,(pts+1)->y);      
    line(pts->x,pts->y,(pts-num+1)->x,(pts-num+1)->y);  
    }
xuanzhuan()
{  int gdriver=DETECT,gmode;
   float angle,x,y,s,t;
   struct wc *pts;
   struct wc a[3]={20,300,120,300,70,230};  
   struct wc re={20,300}; 
   setidentity(thematrix);
    initgraph(&gdriver,&gmode,"");
    printf("please input the angle and s,t,x1,y1");
  scanf("%f,%f,%f",&angle,&s,&t,&x,&y);
   line(20,300,220,300);   line(20,300,20,100); 
   line(20,300,70,230);outtextxy(20,300,"0");
   outtextxy(20,100,"y");outtextxy(220,300,"x");
   line(70,230,120,300);
   translatee(20,300,thematrix);
   rotate(angle,thematrix);
   scale(s,t,thematrix);
   translate(20,300,thematrix);    
   pts=a; transformpoints(3,pts,thematrix);
   setarea(3,pts,BLUE);
   putpixel(20,300,BLUE);
   setidentity(thematrix);   
   getch();
  jingzhimenu();
}

/*************************************************************************/
struct matrix1{ float h[4][4]; };
 struct point3{float x,y,z;  };
 struct point2 {float x,y;};                           
 struct matrix1 *thematrix1;
 struct point2 *pp;                      
 float radius1(float a)
 { return a*3.14/180; } 
 void setidentity1(struct matrix1 *m)
 {
    int i,j;
    for (i=0;i<4;i++)
    for (j=0;j<4;j++)
    m->h[i][j]=(i==j);
}
 void show1(struct matrix1 *m)
{   int i,j;
   for (i=0;i<4;i++)
   { printf("\n");
     for (j=0;j<4;j++)
     printf("%f\t",m->h[i][j]);
   }
 }
void multiple1(struct matrix1 * a,struct matrix1 *b)
{   int r,c,t;float ab,a1,b1;
    struct matrix1 *m;
     setidentity1(m);
   for (r=0;r<4;r++)
    for (c=0;c<4;c++)
    {ab=0.0;
    for (t=0;t<4;t++)
    { a1=(a->h[r][t]);  b1=(b->h[t][c]);   ab=a1*b1+ab;  }   
      m->h[r][c]=ab;
    }
  for (r=0;r<4;r++)
  for (c=0;c<4;c++)
  b->h[r][c]=m->h[r][c];       
}     
 void translate1(float tx,float ty,float tz,struct matrix1 *thematrix1)
 {struct  matrix1 *m;
   setidentity1(m);
   m->h[0][3]=tx;
   m->h[1][3]=ty;
   m->h[2][3]=tz;
   multiple1(m,thematrix1);
 }
 void rotate1(float t,struct matrix1 *thematrix1)
 {float a;
   struct matrix1 *m;
    setidentity(m);
    a=radius(t);
    m->h[0][0]=cos(a);  m->h[0][2]=sin(a);
    m->h[2][0]=-sin(a);    m->h[2][2]=cos(a);
    multiple1(m,thematrix1);
  }
  void toushi(float  distance)                                      
  {  struct  matrix1 *m;
     setidentity1 (m);
     m->h[2][2]=0;   m->h[3][2]=-1/distance;
     show(m);
     multiple1(m,thematrix1);
   }
 void transformpoints1(int n,struct point3 *pts,struct matrix1 *thematrix1)
 {  int k,j;
    float a,b,c,ss1,tt1,qq1,pp1; 
     for(j=0;j<n;pts++,j++)
      {   a=pts->x;  b=pts->y;  c=pts->z;     
          tt1=thematrix1->h[0][0]*a+thematrix1->h[0][1]*b+thematrix1->h[0][2]*c+thematrix1->h[0][3];
          ss1=thematrix1->h[1][0]*a+thematrix1->h[1][1]*b+thematrix1->h[1][2]*c+thematrix1->h[1][3];
          qq1=thematrix1->h[2][0]*a+thematrix1->h[2][1]*b+thematrix1->h[2][2]*c+thematrix1->h[2][3];
          pp1=thematrix1->h[3][0]*a+thematrix1->h[3][1]*b+thematrix1->h[3][2]*c+thematrix1->h[3][3];
          pts->x=tt1/pp1;pts->y=ss1/pp1;
      }     
} 
void draw()
{ line(150,150,250,250);line(150,150,150,30);line(150,150,50,250);                /*画立方体*/
 line(150,90,200,140); line(200,140,200,200);line(200,200,150,250);
 line(150,90,100,140);line(100,140,100,200);line(100,200,150,250);
 line(100,140,150,190);line(150,190,200,140);line(150,190,150,250);
 outtextxy(250,250,"x");outtextxy(150,30,"y");outtextxy(50,250,"z");
 outtextxy(150,150,"o");
 }
void draw1()                                                     /*画透视图*/
{line(300,200,500,200);outtextxy(350,195,"50");outtextxy(400,195,"100");                line(300,500,300,100);outtextxy(295,250,"-50");outtextxy(295,300,"-100");
  outtextxy(295,350,"-150");outtextxy(295,400,"-200");outtextxy(300,200,"0");
  outtextxy(500,200,"X");outtextxy(300,100,"Y");line(350,257,350,314);
  line(300,333,347,386);line(347,386,347,293);line(347,293,300,267);
  line(350,314,404,350);line(404,350,404,276);line(404,276,350,257);
  line(300,333,350,314);line(347,386,404,350);line(300,267,350,257);
  line(347,293,404,276);
 }  

touying()
{int i=DETECT,j;
  struct point3 *p;
 struct point3 a[8]={0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,0,1,1};
 initgr();
 setidentity1(thematrix1); rotate1(60.0,thematrix1);
 translate1(0,-2,1,thematrix1);
 toushi(-2); 
 p=a;
 transformpoints1(8,p,thematrix1);
 initgraph(&i,&j,"");
draw();
draw1();
 getch();
 free(p);
  free(thematrix1);
  jingzhimenu();
   }

  /*平移*/
/***********************************************************************/
/**************************************************************************/
 struct matrix2{    float h[3][3];   };            /*定义一个结构体表示矩阵*/
struct wc2{    float x,y; };                      /*定义一个结构体表示点的坐标*/
float radius2(float a)                            /*将角度转化为弧度的函数*/
{ return a*3.14/180;}  
void setidentity2( struct matrix2 *m)                 /*将矩阵单位化*/
 { int i,j;
     for (i=0;i<3;i++)
     for (j=0;j<3;j++)
     m->h[i][j]=(i==j);    
 }
 void  multiple2(struct matrix2 *a,struct matrix2 *b)     /*两个矩阵相乘*/
 { int r,c,t; float ab,a1,b1; struct matrix2 *m;
    for (r=0;r<3;r++)
    for (c=0;c<3;c++)
    {ab=0.0;
           for (t=0;t<3;t++)
            { a1=(a->h[r][t]);  b1=(b->h[t][c]);  ab=a1*b1+ab; }   
           m->h[r][c]=ab;
     }
  for (r=0;r<3;r++)
  for (c=0;c<3;c++)
  b->h[r][c]=m->h[r][c];      
}
void translate2(float tx,float ty,struct matrix2 *thematrix2)    /*平移*/
{ struct matrix2 *m;
   setidentity2(m);
   m->h[0][2]=tx;   m->h[1][2]=ty;  
   multiple2(m,thematrix2);
 }
void scale2(float sx,float sy,struct matrix2 *thematrix2,struct wc2 re)  /*放缩*/
{  struct matrix2 *m;
   setidentity2(m);
      m->h[0][0]=sx; m->h[1][1]=sy;
      m->h[0][2]=(1-sx)*re.x;
      m->h[1][2]=(1-sy)*re.y;      
      multiple2(m,thematrix2);
}
 void rotate2(float t,struct matrix2 *thematrix2,struct wc2 re)    /*旋转*/
 {  float a;
    struct matrix2 *m;
    setidentity2(m);
    a=radius2( t);
    m->h[0][0]=cos(a);m->h[0][1]=-sin(a);
    m->h[1][0]=sin(a);m->h[1][1]=cos(a);
    m->h[0][2]=re.x*(1-cos(a))+re.y*sin(a);
    m->h[1][2]=re.y*(1-cos(a))+re.x*sin(a);  
    multiple2(m,thematrix2);
  } 
 void transformpoints2(int n,struct wc2 *pts,struct matrix2 *thematrix2)   /*最终的坐标点*/
 {  int k,j; float a,b,ss1,tt1;     
    for(j=0;j<n;pts++,j++)
    {  a=pts->x; b=pts->y;
       tt1=thematrix2->h[0][0]*a+thematrix2->h[0][1]*b+thematrix2->h[0][2];      
       ss1=thematrix2->h[1][0]*a+thematrix2->h[1][1]*b+thematrix2->h[1][2];
       pts->x=tt1;pts->y=ss1;     
     }  
} 
void setarea2(int num,struct wc2 *pts,int color)            /*画出图形*/
   { int i; 
       for (i=0;i<num-1;pts++,i++)
            line(pts->x,pts->y,(pts+1)->x,(pts+1)->y);
            line(pts->x,pts->y,(pts-num+1)->x,(pts-num+1)->y);
     }                         
pingyi()
{  int gdriver=DETECT,gmode;
   char c;
   float angle,x,y,s,t;
   struct wc2 *pts;
   struct wc2 a[3]={50.0,50.0,150.0,50.0,100.0,150.0};
   struct wc2 re={100.0,100.0};
   struct matrix2 *thematrix2;
   setidentity2(thematrix2);initgraph(&gdriver,&gmode,"");
   printf("please choose the choice :1. xuanzhuan 2.pingyi 3.fangsuo\n");
   c=getch();
   if (c=='1')
   {printf("please input the angle\n");
   scanf("%f",&angle);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   rotate2(angle,thematrix2,re);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }
   if (c=='2')
   { printf("please input the x,y\n");
   scanf("%f,%f",&x,&y);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   translate2(x,y,thematrix2);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }
   if (c=='3')
   {printf("please input the s,t\n");
   scanf("%f,%f",&s,&t);
   line(100,150,50,50);
   line(50,50,150,50);                
   line(150,50,100,150); 
   scale2(s,t,thematrix2,re);
   pts=a;
   transformpoints2(3,pts,thematrix2);
   setarea2(3,pts,BLUE);
   putpixel(100,100,BLUE);
   outtextxy(100,100,"(100,100)");
   getch();
   closegraph();
   jingzhimenu();
   }

   getch();
   closegraph();

  }

/*主程序开始*/
/***************************************************/
/*******************************************************/
main()
{
char a,b;
float angle,x,y;
menu();
a=getch();
while(a!='e'&&a!='E')
{if(a=='Z'||a=='z')
DDAline1();
else if(a=='y'||a=='Y')
{ b=getch();
 if(b=='m'||b=='M')
midyuan();
else if(b=='z'||b=='Z')
zhengfuyuan();
}
else if(a=='t'||a=='T')
{ b=getch();
 if(b=='m'||b=='M')
midtuoyuan();
else if(b=='w'||b=='W')
weibutuoyuan();
}
else if(a=='c'||a=='C')
caijian();
else if(a=='D'||a=='d')
zifu();
else if(a=='p'||a=='P')
PaoWuxian();
else if(a=='A'||a=='a')
tianchong();
else if(a=='q'||a=='Q')
{b=getch();
if(b=='b'||b=='B')
brzie();
if(b=='h'||b=='H')
hemit();}
else if(a=='x'||a=='X')
{
xuanzhuan();
}

else if(a=='s'||a=='S')
touying();
else if(a=='l'||a=='L')
pingyi();
else
{
settextstyle(3,1,6);
outtextxy(500,250,"ERROR!");
}
a=getch();
}
end();
}

⌨️ 快捷键说明

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