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

📄 0303.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 2 页
字号:
/* WIN-TC BGI 图形编程模板 */

#include "Conio.h"
#include "graphics.h"
#include "math.h"
#include <malloc.h>
#define closegr closegraph
#define pi 3.1415926
#define X 0
#define Y 1
#define LEN sizeof (struct node)

void initgr(void) /* BGI初始化 */
{
  int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */
  registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
  initgraph(&gd, &gm, "");
}
/*--------------------------Pro.1   Line---------------------------*/
void line2(int x0,int y0,int x1,int y1,int color)
{
   int dx,dy,incrE,incrNE,d,x,y;
    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);
    }

}
void Line1(int x1,int y1,int x2,int y2,int color)
{
 int x;
float increy,increx,i,steps;
increx=x2-x1;
increy=y2-y1;
steps=increy/increx;
i=y1;
for(x=x1;x<=x2;x++)
{
putpixel(x,(int)(i+0.5),color);
i+=steps;}
}
 zheng()
 {
   initgr(); /* BGI初始化*/
   Line1(50,50,300,200,2);
   Line1(50,150,300,300,2);
   Line1(50,250,300,400,2);
   outtextxy(350,300,"The Green ones: DDALine.");
   getch();
   line2(50,50,300,300,WHITE);
   line2(50,150,300,400,WHITE);
   outtextxy(350,320,"The White ones: MIDPOINTLine.");
   getch();

 }

/*------------------------Pro.2 The Bezier curve ang B yangtiao curve-------------------------*/
typedef struct {
     int x,y;
  }Vector;
void DisplayCubicBezierCurve(Vector P[4],int count)
{ float C[2][4],t ,deltat;
  Vector V,newV;
  int i,j;

    C[0][0]=   P[0].x  ;
    C[0][1]=-3*P[0].x+3*P[1].x;
    C[0][2]= 3*P[0].x-6*P[1].x+3*P[2].x;
    C[0][3]=  -P[0].x+3*P[1].x-3*P[2].x+P[3].x;

    C[1][0]=   P[0].y  ;
    C[1][1]=-3*P[0].y+3*P[1].y;
    C[1][2]= 3*P[0].y-6*P[1].y+3*P[2].y;
    C[1][3]=  -P[0].y+3*P[1].y-3*P[2].y+P[3].y;






  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]));
    setcolor(RED);
    line(V.x,V.y,newV.x,newV.y);
    V.x=newV.x,V.y=newV.y;

  }


}
 liu(void)
{  int i,j;
   int count;
   Vector P[4];
  initgr(); /* BGI初始化 */

  /*****此部分添加你自己的代码,例如
  line(25, 25, 220, 220);
  circle(100, 100, 50);
  等等*****/
  count=1000;
  printf("Please input the Vector\n");
  for (i=0;i<4;i++)
    scanf("%d%d",&P[i].x,&P[i].y);
   cleardevice();
   DisplayCubicBezierCurve(P, count);
   setcolor(BLUE);
   for (i=0;i<3;i++)
     line(P[i].x,P[i].y,P[i+1].x,P[i+1].y);




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

/*-------------------------------------Pro.3 The Midcircle,DDAcircle and DDAEllipse--------------------------------*/
void circlepoints(int x0,int y0,int x,int y,int color)
{
 putpixel(x0+x,y0+y,color);
 putpixel(x0+y,y0+x,color);
 putpixel(x0-y,y0+x,color);
 putpixel(x0-x,y0+y,color);
 putpixel(x0+y,y0-x,color);
 putpixel(x0+x,y0-y,color);
 putpixel(x0-x,y0-y,color);
 putpixel(x0-y,y0-x,color);
}
/*-------------圆的中点算法-----------------*/
void MidPointCircle(int x0,int y0,int r,int color)
{
  int x,y,d;

  x=0;
  y=r;
  d=5-4*r;
  circlepoints(x0,y0,x,y,color);
  while (y>x)
  {
    if (d<=0)
     d+=8*x+12;
    else
     {
       d+=8*(x-y)+20;
       y--;
     }
    x++;
    circlepoints(x0,y0,x,y,color);

  }

}
/*----------------圆的DDA算法--------------------*/
void DDAcircle(int x0,int y0,double r,double ts,double te)
{
  double rad,tsl,tel,deg,dte,ta,ct,st;
  int x,y,n,i;
  /*准备工作*/
  rad=0.0174533;
  tsl=ts*rad;
  tel=te*rad;
  if (r<5.08)
    deg=0.015;
  else
    if (r<7.62)
     deg=0.06;
    else
     if (r<25.4)
      deg=0.075;
     else
      deg=0.15;
  dte=deg*25.4/r;
  /*一些小约定*/
  if(tel<tsl)
   tel+=2*pi;
     n=(int)((tel-tsl)/dte+0.5);
  if (n==0)
     n=(int)(2*pi/dte+0.5);
   /*开始画*/
  /*初始化*/
  ta=tsl;
  x=x0+r*cos(tsl);
  y=y0+r*sin(tsl);
  moveto(x,y);

  setcolor(WHITE);
  for(i=1;i<=n;i++)
  {
    ta+=dte;
    ct=cos(ta);
    st=sin(ta);
    x=x0+r*ct;
    y=y0+r*st;
    lineto(x,y);

    setcolor(WHITE);
  }
  /*收尾工作*/
  x=x0+r*cos(tel);
  y=y0+r*sin(tel);
  lineto(x,y);
  setcolor(WHITE);

}
/*---------------椭圆的中点算法---------------------*/
void ellipsepoint(int x0,int y0,int x,int y, int color)
{ putpixel(x+x0,y+y0,color);
  putpixel(-x+x0,y+y0,color);
  putpixel(-x+x0,-y+y0,color);
  putpixel(x+x0,-y+y0,color);
 }
void epse(int x0,int y0,int a,int b,int color)
{ float d,squarea,squareb;
   int xp,yp,x,y;
   squarea=a*a;
   squareb=b*b;
   xp=(int)(0.5+(float)squarea/sqrt((float)(squarea+squareb)));
   yp=(int)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));
   /*生成第一象限内的上半部分椭圆*/
   x=0;
   y=b;
   d=4*(squareb-squarea*b)+squarea; /*初始化*/
   ellipsepoint(x0,y0,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++;
     ellipsepoint(x0,y0,x,y,color);
   }
   /*画下半部分*/
   x=a;
   y=0;
   d=4*(squarea-squareb*a)+squareb;      /*初始化*/
   ellipsepoint(x0,y0,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++;
     ellipsepoint(x0,y0,x,y,color);
   }
}
Midpointellipse()
{
 /* initgr();  BGI初始化 */
  epse(200,200,98,50,YELLOW);

  /* getch(); 暂停一下,看看前面绘图代码的运行结果 */

}
/*--------------椭圆的角度DDA算法-------------------*/
void DDAellipse(int xc,int yc,double a,double b,double alp,double ts,double te)
{
 double rad,tsl,tel,alpl,deg,dte,r,ta,a1,a2,b1,b2;
 int x,y,n,i;

 rad=0.0174533;
 tsl=ts*rad;
 tel=te*rad;     /*将角度转化为弧度*/
 alpl=alp*rad;   /*椭圆的长轴与x轴的夹角*/
 a1=a*cos(tsl);
 b1=cos(alpl);
 a2=b*sin(tsl);
 b2=sin(alpl);
 r=(a>b)?a:b;

 if(r<5.08)
  deg=0.015;
  else
   if(r<7.62)
    deg=0.06;
   else
    if(r<25.4)
     deg=0.075;
    else
    deg=0.15;
  dte=deg*25.4/r;

 if(tel<tsl)
  tel+=2*pi;
  n=(int)((tel-tsl)/dte+0.5);
  if(n==0)
   n=(int)(2*pi/dte+0.5);
  ta=tsl;
  x=xc+a1*b1-a2*b2;
  y=yc+a1*b2+a2*b1;
  setcolor(RED);
  moveto(x,y);
  for(i=1;i<=n;i++)
  {
    ta+=dte;
    a1=a*cos(ta);
    a2=b*sin(ta);
    x=xc+a1*b1-a2*b2;
    y=yc+a1*b2+a2*b1;
    lineto(x,y);
    setcolor(RED);
  }
  a1=a*cos(tel);
  a2=b*sin(tel);
  x=xc+a1*b1-a2*b2;
  y=yc+a1*b2+a2*b1;
  lineto(x,y);

}
  tang(void)
{ int k;
  loop1:initgr(); /* BGI初始化 */

    rectangle(50,30,400,200);
    outtextxy(120,180, "Welcome You!!") ;
    getch();
    outtextxy(100,50,"No.1   The midpointcircle.") ;
    getch();
    outtextxy(120,70,"No.2   The DDAcircle") ;
    getch();
    outtextxy(140,90, "No.3   The DDAellipse") ;
    getch();
    outtextxy(80,140, "Please input a number...") ;
    scanf("%d",&k);
   loop2:switch(k)
    {
     case 1:{cleardevice();
             MidPointCircle(200,200,200,GREEN);
             getch();
             MidPointCircle(150,200,50,GREEN);
             getch();
             MidPointCircle(250,200,50,GREEN);
             getch();
             DDAcircle(200,200,100,60,120);
             getch();
             outtextxy(100,420,"The Midpointcircle.");
             getch();
             goto loop1;break;}
     case 2:{cleardevice();
             DDAcircle(200,200,100,0,360);
             getch();
             MidPointCircle(300,300,100,GREEN);
             getch();
             line(290,250,400,200);
             outtextxy(402,200,"The DDAcircle.");
             getch();
             line(401,300,490,300);
             outtextxy(492,300,"The MidpointCircle.");
             /*outtextxy(100,420,"The DDAcircle.");*/
             getch();
             goto loop1;break;}
     case 3:{cleardevice();
             DDAellipse(201,201,200,100,0,0,360);
             outtextxy(420,180,"Rotate 0.");
             getch();
             DDAellipse(201,201,200,100,45,0,360);
             outtextxy(420,200,"Rotate 45.");
             getch();
             DDAellipse(201,201,200,100,90,0,360);
             outtextxy(420,220,"Rotate 90.");
             getch();
             DDAellipse(201,201,200,100,135,0,360);
             outtextxy(420,240,"Rotate 135.");
             getch();
             outtextxy(100,420,"The RED ones: DDAellipse.");
             getch();
             Midpointellipse();
             outtextxy(100,440,"The YELLOW ones: MIDPOINTellipse.");
             getch();
             outtextxy(200,200,"That's all! Thank you!!");break;}
     default:{outtextxy(80,160,"INPUT ERROR!  Please input again!");
              scanf("%d",&k);goto loop2;}

    }
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
 closegr();  /* 恢复TEXT屏幕模式 */
  return 0;
}
/*--------------------------------------Pro.4 The AreaFilled------------------------*/
struct node
 { int dx,dy;
   struct node *next;
   };
struct node *creat()
{ struct node *h,*p,*r;
  int x,y;
  scanf("%d,%d",&x,&y);
  h=NULL;
  while (x!=0)
  { p=(struct node *)malloc(LEN);
    p->dx=x;
    p->dy=y;
    if (h==NULL) h=p;
     else r->next=p;
    r=p;
    scanf("%d,%d",&x,&y);
   }
  r->next=NULL;
  return(h);
 }
struct node *create()
{ struct node *h,*p,*r;
  int x,y;
  int i;
  int n[10][2];
  h=NULL;
  n[1][0]=100;
  n[1][1]=100;
  n[2][0]=180;
  n[2][1]=80;
  n[3][0]=220;/*220*/
  n[3][1]=150;
  n[4][0]=120;
  n[4][1]=250;
  n[5][0]=70;
  n[5][1]=150;
  for (i=1;i<=4;i++)
  { p=(struct node *)malloc(LEN);
    p->dx=n[i][0];
    p->dy=n[i][1];
    if (h==NULL) h=p;
     else r->next=p;
    r=p;
   }
  r->next=NULL;
  return(h);
 }
draw_polo(h)
struct node *h;
{ int sx,sy;
  struct node *q;
  setcolor(14);
  q=h;
  sx=q->dx;
  sy=q->dy;
  q=q->next;
  while (q!=NULL)
   {
    line(sx,sy,q->dx,q->dy);
    sx=q->dx;
    sy=q->dy;
    q=q->next;

⌨️ 快捷键说明

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