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

📄 newmorning.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 4 页
字号:
  x++;
  }
  y++;
  putpixel(x,y,color);
 }
  }
 }
 else if((float)(y1-y0)/(x1-x0)>-1)
 {
   dx=x1-x0;
 dy=y1-y0;
 d=-dx-2*dy;
 incrE=-2*dy;
 incrNE=-2*(dx+dy);
 x=x0,y=y0;
 putpixel(x,y,color);
 while(x<x1)
 {
  if(d<0)
  d+=incrE;
  else
  {
  d+=incrNE;
  y--;
  }
  x++;
  putpixel(x,y,color);
 }/*while*/
 }
 else
 {
  dx=x1-x0;
 dy=y1-y0;
 d=2*dx-dy;
 incrE=-2*dx;
 incrNE=-2*(dx+dy);
 x=x0,y=y0;
 putpixel(x,y,color);
 while(y>y1)
 {
  if(d>0)
  d+=incrE;
  else
  {
   d+=incrNE;
   x++;
  }
  y--;
  putpixel(x,y,color);
 }
 }
}
midpoint()
{
  int x0,y0,x1,y1,color;
  initgr(); /* BGI初始化 */
  midpointline(100,100,200,300,1);
  midpointline(200,100,50,150,2);
  midpointline(50,70,300,200,4);
  midpointline(250,40,80,350,3);
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  jingzhimenu();
} 

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



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

/*画圆*/
/****************************************/
/*******************************************/
 void circlepoints(int x,int y,int a,int b,int color)
{
 putpixel(x+a,y+b,color);
 putpixel(y+a,x+b,color);
 putpixel(-y+a,x+b,color);
 putpixel(-x+a,y+b,color);
 putpixel(y+a,-x+b,color);
 putpixel(x+a,-y+b,color);
 putpixel(-x+a,-y+b,color);
 putpixel(-y+a,-x+b,color);
}
void midpointcircle(int a,int b,int radius,int color)
{
 int x,y,deltaE,deltaSE;
 float d;
 x=0;
 y=radius;
 d=5-4*radius;
 circlepoints(x,y,a,b,color);
 deltaE=12;
 deltaSE=20-8*radius;
 while(y>x)
 {
  if(d<=0)
  {
   d+=deltaE;
   deltaSE+=8;
   }
  else
  {
   d+=deltaSE;
   deltaSE+=16;
   y--;
  }
  deltaE+=8;
  x++;
  circlepoints(x,y,a,b,color);
 }
}

/*中点算法*/
midyuan()
  {
   initgr(); /* BGI初始化 */
   midpointcircle(200,200,150,5);
   getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
   jingzhimenu();
}

void circlepoints1(int a,int b,int x,int y,int color)
{
 putpixel(x+a,y+b,color);
 putpixel(-x+a,y+b,color);
 putpixel(x+a,-y+b,color);
 putpixel(-x+a,-y+b,color);
}
void PositiveNegativeArc(int a,int b,int radius,int color)
{
 int x,y,d;
 x=0;
 y=radius;
 putpixel(x+a,y+b,color);
 x=1;
 putpixel(x+a,y+b,color);
 d=1;
 while(y>=0)
 {
  if(d>=0)
  {
   d=d-2*y+1;
   y--;
  }
  else
  {
   d=d+2*x+1;
   x++;
  }
circlepoints1(a,b,x,y,color);
 }
}
/* 正负算法*/
zhengfuyuan()
{
initgr();
 PositiveNegativeArc(200,200,150,2);
  getch();
   /* 暂停一下,看看前面绘图代码的运行结果 */
jingzhimenu();
}
/******************************************************************/
/******************************************************************/


/*椭圆*/
/******************************************************************/
/*****************************************************************/
void ellipsepoints(int a,int b,int x,int y,int color)
{
 putpixel(x+a,y+b,color);
  putpixel(-x+a,y+b,color);
   putpixel(-x+a,-y+b,color);
    putpixel(x+a,-y+b,color);
}

void ellipsepoints1(int x,int y,int color)
{
 putpixel(x+200,y+200,color);
  putpixel(-x+200,y+200,color);
   putpixel(-x+200,-y+200,color);
    putpixel(x+200,-y+200,color);
}
void tuoyuan1(int a,int b,int color)
{
 long xP,yP,squarea,squareb;
 int  x,y;
 int d,m,n=0;
 squarea=a*a;
 squareb=b*b;
 xP=(long)(0.5+(float)squarea/sqrt((float)(squarea+squareb)));  /*计算分界点是为了减少运算次数*/
 yP=(long)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));
 x=a;
 for(;x>=xP;x--) /*画出椭圆下半部分的圆弧*/
 {
 m=(int)(0.5+(float)b*sqrt((float)(a*a-(x-1)*(x-1)))/a);
 d=(int)(0.5+(float)b*sqrt((float)(a*a-(x-1)*(x-1)))/a-(float)b*sqrt((float)(a*a-x*x))/a);
 for(y=n;y<=m;y++)       /*描出每个横坐标对应的所有像素*/
 ellipsepoints1((int)(x-1),(int)(y),color);
  n+=d;
 }
 n=0;
 for(y=b;y>=yP;y--)  /*画出椭圆上半部分的圆弧*/
   {
   m=(int)(0.5+(float)a*sqrt((float)(b*b-(y-1)*(y-1)))/b);
  d=(int)(0.5+(float)a*sqrt((float)(b*b-(y-1)*(y-1)))/b-(float)a*sqrt((float)(b*b-y*y))/b);
  for(x=n;x<=m;x++)     /*描出每个纵坐标对应的所有像素*/
  ellipsepoints1((int)(x),(int)(y-1),color);
  n+=d;
  }
 }

weibutuoyuan()
{
  int a,b;
  initgr(); /* BGI初始化 */
  tuoyuan1(100,50,5);
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  jingzhimenu();
}


void midpointellipse(int a1,int a2,int a,int b,int color)
{

 long x,y,d,xP,yP,squarea,squareb;
 squarea=a*a;
 squareb=b*b;
 xP=(long)(0.5+(float)squarea/sqrt((float)(squarea+squareb)));
 yP=(long)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));
 x=0;y=b;
 d=4*(squareb-squarea*b)+squarea;
 ellipsepoints(a1,a2,x,y,color);
 while(x<=xP)
 {
  if(d<=0)
  d+=4*squareb*(2*x+3);
  else
  {
   d+=4*squareb*(2*x+3)-8*squarea*(y-1);
   y--;
  }
  x++;
  ellipsepoints(a1,a2,x,y,color);
 }
 x=a;y=0;
 d=4*(squarea-a*squareb)+squareb;
 ellipsepoints(a1,a2,x,y,color);
 while(y<yP)
 {
  if(d<=0)
  d+=4*squarea*(2*y+3);
  else
  {
   d+=4*squarea*(2*y+3)-8*squareb*(x-1);
   x--;
  }
  y++;
  ellipsepoints(a1,a2,x,y,color);
 }
}

midtuoyuan()
{initgr();
  midpointellipse(200,200,150,100,BLUE);
   midpointellipse(200,200,100,150,YELLOW);
getch();
jingzhimenu();
}
/***************************************************/
/*****************************************************/


/*抛物线*/
/*****************************************************/
/********************************************************/
void duicheng(x,y,color)
{
 putpixel(x+150,y+150,color);
 putpixel(x+150,-y+150,color);
}
void paowuxian(int y1,int color)
{
 int x,y,d;
 x=0,y=0;
 putpixel(x,y,color);
 x=1;
 putpixel(x,y,color);
 d=1;
 while(y<=y1)
 {
  if(d>=0)
  {
   d=d-2*y-1;
   y++;
  }
  else
  {
   d=d+1;
   x++;
  }
  duicheng(x,y,color);
 }
}

PaoWuxian()
{
 initgr();
 paowuxian(50,RED);
 paowuxian(100,BLUE);
 getch();
 jingzhimenu();
}
/********************************************************/
/**********************************************************/

/*2. 填充*/
/***************************************************/
/*******************************************************/
/* WIN-TC BGI 图形编程模板 */

void put_in_sides_list(int entry,int x1,int y1,int x2,int y2,int next_y)
{
 int maxy;
 float x2_temp,x_change_temp;
 x_change_temp=(float)(x2-x1)/(float)(y2-y1);
 x2_temp=x2;/*以下为退缩一点操作. */
 if((y2>y1)&&(y2>next_y))  /*多边形的上顶点*/
  {
   y2--;
   x2_temp-=x_change_temp;
   }
  else if((y2<y1)&&(y2<next_y))  /*多边形的下顶点*/
           {
            y2++;
            x2_temp+=x_change_temp;
           }

 maxy=(y1>y2)?y1:y2;
 while((entry>0)&&(maxy>sides[entry-1].y_top))  /*如果所交的顶点是左顶点或右顶点,所有左、右顶点作如下处理*/
         {
          sides[entry]=sides[entry-1];
          entry--;
         }
    sides[entry].y_top=maxy;
    sides[entry].delta_y=abs(y2-y1)+1;
    if(y1>y2)  sides[entry].x_int=x1;
    else {sides[entry].x_int=x2_temp;
          sides[entry].x_change_per_scan=x_change_temp;
          }

}

void sort_on_bigger_y(int n)   /*按照输入的多边形,建立起活性边表*/
{
 int k,x1,y1;
 side_count=0;
 y1=y[n-1];
 x1=x[n-1];
 bottomscan=y[n-1];
 for(k=0;k<n;k++)
  {
   if(y1!=y[k])    /*判断边是否水平*/
   {
    side_count++;
    put_in_sides_list(side_count,x1,y1,x[k],y[k],y[k+1]); /*非水平边调用put_in_side_list子程序放入活性边来*/

    }
    else {

           line(x1,y1,x[k],y[k]); /*是水平边则直接画出*/

          }
   if(y[k]<bottomscan)bottomscan=y[k];
   y1=y[k];x1=x[k];
  }
}

void update_first_and_last(int count2,int scan1)  /*刷新活性边表的first和last两根指针的所指位置,以保证指针指出激活边的范围*/
{
 while((sides[last_s+1].y_top>=scan1)&&(last_s<count2))last_s++;
 while(sides[first_s].delta_y==0)first_s++;

}

void swap(EACH_ENTRY x,EACH_ENTRY y)  /*交换活性边表中两条相邻位置边的彼此位置*/
{
 int i_temp;
 float f_temp;
 i_temp=x.y_top;x.y_top=y.y_top;y.y_top=i_temp;
 f_temp=x.x_int;x.x_int=y.x_int;y.x_int=f_temp;
 i_temp=x.delta_y;x.delta_y=y.delta_y;y.delta_y=i_temp;
 f_temp=x.x_change_per_scan;x.x_change_per_scan=y.x_change_per_scan;y.x_change_per_scan=f_temp;

}


void sort_on_x(int entry)   /*将一条边side[entry],在活性边表的first到entry之间按x_int的大小插入合适位置*/

{
 while((entry>=first_s)&&(sides[entry].x_int<sides[entry-1].x_int))
 {
  swap(sides[entry],sides[entry-1]);  /*调用swap子程序交换两条边的彼此位置*/
  entry--;
 }

}

void process_x_intersections(int first_s1,int last_s1) /*对活性边表中的激活边按照x_int的大小排序 */
{
 int k;
 x_int_count=0;
 for(k=first_s1;k<last_s1+1;k++)
 {
  if(sides[k].delta_y>0)
   {
    x_int_count++;
    sort_on_x(k);   /*从first到last,对每一根 △y> 0的边,调用sort_on_x子程序排入活性边表中合适位置*/
   }
 }
}

void draw_lines(int scan3,int x_int_count1,int index) /*在一条扫描线位于多边形内的部分,填上指定的色彩*/
{
 int k,x,x1,x2;
 for(k=1;k<(int)(x_int_count1/2+1.5);k++)
 {
  while(sides[index].delta_y==0)index++;
  x1=(int)(sides[index].x_int+0.5);index++;
  while(sides[index].delta_y==0)index++;
  x2=(int)(sides[index].x_int+0.5);/*在活性边表的激活边范围内,依次取出Δy'= 0两边的x_int*/
  line(x1,scan3,x2,scan3);   /*得到两个端点(x1, scan),(x2, scan),画一条水平线*/
  index++;
 }

}

void update_sides_list()
{
 int k;
 for(k=first_s;k<last_s+1;k++)
 {
  if(sides[k].delta_y>0)
  {
   sides[k].delta_y--;
   sides[k].x_int-=sides[k].x_change_per_scan;   /*刷新活性边表内激活边的值:Δy=Dy-1*/
  }
 }
}

void fill_area(int count1) /*主函数,调用其它8个函数*/
  /*所填充多边形的顶点个数*/
{
 sort_on_bigger_y(count1);
 first_s=0;last_s=0;
 for(scan=sides[0].y_top;scan>bottomscan;scan--)
 {
  update_first_and_last(count1,scan);
  process_x_intersections(first_s,last_s);
  draw_lines(scan,x_int_count,first_s);
  update_sides_list();
 }
}

/*下面为递归填充*/
void seedfilling(int x,int y,int fill_color,int boundary_color)
{
 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*/

⌨️ 快捷键说明

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