📜  计算机图形学程序

📅  最后修改于: 2020-12-21 09:38:30             🧑  作者: Mango

计算机图形学程序

编写一个程序来绘制基本的图形构造,例如直线,圆,弧,椭圆和矩形。

#include
#include
void main()
{
    intgd=DETECT,gm;
    initgraph (&gd,&gm,"c:\\tc\\bgi");
    setbkcolor(GREEN);
    printf("\t\t\t\n\nLINE");
    line(50,40,190,40);
    printf("\t\t\n\n\n\nRECTANGLE");
    rectangle(125,115,215,165);
    printf("\t\t\t\n\n\n\n\n\n\nARC");
    arc(120,200,180,0,30);
    printf("\t\n\n\n\nCIRCLE");
    circle(120,270,30);
    printf("\t\n\n\n\nECLIPSE");
    ellipse(120,350,0,360,30,20);
    getch();
}

输出量

编写程序以使用填充有不同颜色和图案的不断增加的圆圈绘制动画。

#include
#include
void main()
{
    intgd=DETECT, gm, i, x, y;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    x=getmaxx()/3;
    y=getmaxx()/3;
    setbkcolor(WHITE);
    setcolor(BLUE);
    for(i=1;i<=8;i++)
          {
        setfillstyle(i,i);
        delay(20);
        circle(x, y, i*20);
        floodfill(x-2+i*20,y,BLUE);
    }
    getch();
    closegraph();
}

输出量

程序制作屏幕保护程序,以在随机位置显示不同大小的圆圈,并用不同的颜色填充。

#include
#include
#include"graphics.h"
#include"stdlib.h"
void main()
{
    intgd=DETECT,gm,i=0,x,xx,y,yy,r;
    //Initializes the graphics system
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    x=getmaxx();
    y=getmaxy();
    while(!kbhit())
    {
        i++;
      //    setfillstyle(random(i),random(30));

        circle(xx=random(x),yy=random(y),random(30));
        setfillstyle(random(i),random(30));
        floodfill(xx,yy,getmaxcolor());
        delay(200);
    }
    getch();
}

输出量

编写一个程序,以使用内置函数制作一辆有色汽车。

#include
#include
int main()
{
    intgd=DETECT,gm, i, maxx, cy;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    setbkcolor(WHITE);
    setcolor(RED);
    maxx = getmaxx();
    cy = getmaxy()/2;
    for(i=0;i

输出量


在C的控制台输出上编写程序以印地文脚本print您的姓名。

#include
#include
#include
#include
void main()
{    
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"c:\\tc\\bgi");
    setbkcolor(9);
    line(100,100,370,100);
    
    line(120,100,120,170);
    arc(143,100,0,180,23);
    line(165,100,165,155);
    arc(150,155,100,0,15);
    line(180,100,180,170);

    circle(210,140,10);
    line(210,130,250,130);

    circle(280,140,10);
    line(280,130,330,130);
    line(330,100,330,170);
    line(345,100,345,170);
    ellipse(337,100,0,180,9,18);
    getch();
}

输出量

使用箭头键编写一个程序控制球。

#include
#include
void main()
{
    intgd=DETECT,gm,x,y,r=40;
    charch;
    initgraph(&gd,&gm,"C:/TURBOC3/BGI");
    setbkcolor(3);
    x=getmaxx()/2;
    y=getmaxy()/2;
    setfillstyle(1,RED);
    circle(x,y,r);
    floodfill(x,y,getmaxcolor());
    while((ch=getch())!=13)
      {
        switch(ch)
                     {
            case 75:    if(x>=r+1)
                                {
                    cleardevice();
                    circle(x-=10,y,r);
                    floodfill(x,y,getmaxcolor());
                          }
                break;
            case 72:    if(y>=r+1)
                                {
                    cleardevice();
                    circle(x,y-=10,r);
                    floodfill(x,y,getmaxcolor());
                                  }
                break;
            case 77:    if(x<=(getmaxx()-r-10))
                               {
                    cleardevice();
                    circle(x+=10,y,r);
                    floodfill(x,y,getmaxcolor());
                                }
                  break;
            case 80:    if(y<=(getmaxy()-r-10))
                               {
                    cleardevice();
                    circle(x,y+=10,r);
                    floodfill(x,y,getmaxcolor());
                 }
        }
    }
    getch();
}

输出量

编写程序以实现数字时钟。

#include
#include
#include
#include

struct time t;
void display(int,int,int);
void main()
{
    int i=0,gd=DETECT,gm,hr,min,sec;
    clrscr();
    initgraph(&gd,&gm,"c:\\turboc3\\bgi");
    setcolor(GREEN);
    settextstyle(4,0,7);

    while(!kbhit())
    {
       gettime(&t);
       hr=t.ti_hour;
       min=t.ti_min;
       sec=t.ti_sec;
       i++;

       display(100,100,hr);
       display(200,100,min);
       display(300,100,sec);
              sound(400);
       delay(30);
       nosound();
       delay(930);
       cleardevice();
    }
    getch();
}
void display(int x,int y,int num)
{

     char str[3];
     itoa(num,str,10);

     settextstyle(4,0,7);

     outtextxy(180,100,":");
     outtextxy(280,100,":");
     outtextxy(x,y,str);

     rectangle(90,90,380,200);
     rectangle(70,70,400,220);

     outtextxy(90,250,"Digital Clock");
}

输出量

编写程序制作益智游戏。

#include
#include
#include
 #include
#include

int a[5][5];
int t[16]={0,4,11,12,7,1,15,5,13,6,10,3,2,14,8,9};
int test[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

struct pos
     {
       int h,v;
     }
    p[4][4];
 int row=4,col=4;

void game(int); //MOVEMENT
void rec();   //DRAWING RECTANGLE
void pri();   //PRINTING NUMBERS INITIALLY
int getkey();   // TO TRACE KEY PRESSED
inline void space() 
   {
         cout<<""; 
   }
inline void print(int r,int c) 
   { 
          cout<

输出量


编写程序以使用正弦波形实现弹跳球。

#include
#include 
#define HEIGHT getmaxy()
#define WIDTH getmaxx()
#define GROUND 450
#define MAXHEIGHT 420 
void main()
{
        int x,y=0,t=MAXHEIGHT,c=1;
        int gd=DETECT,gm;
       initgraph(&gd,&gm,"C:\\T urboC3\\BGI"); 
      for(x=40;x<=getmaxx();x=x+2)
 {
     //Draw Ground
     rectangle (0,MAXHEIGHT,getmaxx(),MAXHEIGHT+5);
     floodfill (5,MAXHEIGHT+3,WHITE);
    //Draw Ball
   pieslice(x,y,0,360,20);
   //floodfill(x,y,RED);
   delay(100);
   if(y>MAXHEIGHT-20)
     {
         c=0;
         t=t-40;
     }
     if(y<=(MAXHEIGHT-t))
    {
           c=1;
     }
        if(t>=40)
       y=y+(c? 15:-15); 
       cleardevice();
       //Exit upon keypress
       if(kbhit())
       break;
}
getch();
}

输出量

编写程序以实现垂直方向的弹跳球。

#include 
#include 
#include 
#include 
 
int main() 
{
          int gd = DETECT, gm;
          int i, x, y, flag=0;
          initgraph(&gd, &gm, "C:\\TC\\BGI");
 
         x = getmaxx()/2;
         y = 30;
        while (!kbhit()) 
      {
            if(y >= getmaxy()-30 || y <= 30)
           flag = !flag;
           /* draws the gray board */
           setcolor(RED);
           setfillstyle(SOLID_FILL, RED);
           circle(x, y, 30);
           floodfill(x, y, RED);
 
          delay(50); 
          cleardevice();
          if(flag)
      {
           y = y + 5;
      } 
          else
     {
          y = y - 5;
      }
  }
 
    getch();
    closegraph();
    return 0;
}

输出量

使用Composite Transformation编写一个平移,旋转和缩放的程序。

#include
#include
#include
#include
int x1,y1,x2,y2,x3,y3,a,b;
void draw();
void rotate();
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter first co-ordinate value for triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter second co-ordinatevalues for triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter third co-ordinate valuesfor triangle:");
scanf("%d%d",&x3,&y3);
draw();
getch();
rotate();
getch();

return 0;
}

void draw()
{
  line(x1,y1,x2,y2);
  line(x2,y2,x3,y3);
  line(x3,y3,x1,y1);
}
 void rotate()
 {
    int a1,a2,a3,b1,b2,b3;
    float angle;
    printf("Enter the rotation angle co-ordinates:");
    scanf("%f",&angle);
    cleardevice();
      angle=(angle*3.14)/180;
      a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
      b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
      a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
      b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
      a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
      b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
      printf("ROTATION");
      printf("\n Changed coordinates\n");
      printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
    line(a1,b1,a2,b2);
    line(a2,b2,a3,b3);
    line(a3,b3,a1,b1);
 }

输出量


编写程序以使用中点实现方法绘制圆。

#include
#include
 
void drawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
 
    while (x >= y)
    {
    putpixel(x0 + x, y0 + y, 7);
    putpixel(x0 + y, y0 + x, 7);
    putpixel(x0 - y, y0 + x, 7);
    putpixel(x0 - x, y0 + y, 7);
    putpixel(x0 - x, y0 - y, 7);
    putpixel(x0 - y, y0 - x, 7);
    putpixel(x0 + y, y0 - x, 7);
    putpixel(x0 + x, y0 - y, 7);
           if (err <= 0)
    {
        y += 1;
        err += 2*y + 1;
    }
 
    if (err > 0)
    {
        x -= 1;
        err -= 2*x + 1;
    }
    }
}
 void main()
{
    int gdriver=DETECT, gmode, error, x, y, r;
    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
           printf("Enter radius of circle: ");
    scanf("%d", &r);
 
    printf("Enter co-ordinates of center(x and y): ");
    scanf("%d%d", &x, &y);
    drawcircle(x, y, r);
    getch();
}

输出量

编写程序以绘制贝塞尔曲线。

#include 
#include 
#include 
#include 

void bezier (int x[4], int y[4])
{
    int gd = DETECT, gm;
    int i;
    double t;
    initgraph (&gd, &gm, "C:\\tc\\bgi");

    for (t = 0.0; t < 1.0; t += 0.0005)
    {
    double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
            3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];

    double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
            3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];

    putpixel (xt, yt, WHITE);
    }
  for (i=0; i<4; i++)
    putpixel (x[i], y[i], YELLOW);

    getch();
    closegraph();
    return;
}

void main()
{
    int x[4], y[4];
    int i;
    printf ("Enter the x- and y-coordinates of the four control points.\n");
    for (i=0; i<4; i++)
   scanf ("%d%d", &x[i], &y[i]);
    bezier (x, y);
 }         

输出量


程序围绕其中点旋转矩形

#include
#include 
#include
#include
#include
#define pi 3.14
class arc
{
    float x[10], y[10], theta, h1, k1,r[0][10],ang;
    float p[10][10],p1[10][10],x1[10], y1[10],xm,yx;
    int i, k, j, n;
    public:
    void get();
    void cal();
    void map();
    void graph();
    void plot();
    void plot1();
};
void arc::get()
{
    cout "<<"\n ENTER ANGLE OF ROTATION ";
           cin>>ang;
           n = 4;
           cout<<"\n ENTER";
         for (i=0; i>x[i]>>y[i];
         }
         h1=x [0] + (([1]-x[0])/2);
         k1=y[0]+(([3]-y[0])/2);
         cout<<"\n MIDPOINT OF RECTANGLE IS--"<

输出量

程序使用Liang Barsky方法剪切线

#include
#include 
#include
#include
#include
class liang
{
    float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym;
    float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5];
    int gd, gm, test, s, k;
    public:
    void clipliang();
    void get();
    void map();
    void graph();
};
void liang :: map()
{
    gd=DETECT;
    initgraph (&gd, &gm, "");
    int errorcode = graphresult();
    /*an error occurred */
           if (errorcode!=grOK)
    {
        printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void liang::graph()
{
    xm=getmaxx()/2;
    ym=getmaxy()/2;
    line (xm, 0, xm, 2 * ym);
    line (0, ym, 2 * xm, ym);
}
void liang :: get()
{
    cout<<"\n ENTER WINDOW COORDINATES xwmin, ywmin, xwmax, ywmax";
    cin>>xmin>>ymin>>xmax>>ymax>>;
    rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym);
       //  rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin);
           cout<<"\n ENTER END POINTS OF LINE (x1, y1)(x2, y2) ";
           cin>>x1>>y1>>x2>>y2;
    line (x1+xm,-y1+ym, x2+xm,-y2+ym);
    getch();
}
       // line (x1, y1, x2, y2);
void liang :: clipliang()
{
    float x=0, y=1;
    dx=x2-x1;
    dy=y2-y1;
    p[0]=-dx;
    p[1]=dx;
    p[2]=-dy;
    p[3]=dy;
    q[0]=x1-xmin;
    q[1]=xmax-x1;
    q[2]=y1-ymin;
    q[3]=ymax-y1;
    for (k=0;k<4;k++)
    {
        if (p[k]==0&&q[k]<0)
        {
            cout<<"\n OUTSIDE";
                                 getch();
                                 exit(0);
                      }
                       if (p[k]<0)
               {
            r[k]=q[k]/p[k];
            if (r1[k]u2)
        {
            cout<<"\n COMPLETE OUTSIDE WINDOW";
                                 getch();
                                 exit(0);
                        }
void main ()
{
    liang1;
    clrscr();
    l.map();
    l.graph();
    l.get();
    l.clipliang();
    getch();
}

输出量

实现3维标准透视投影的程序。

#include
#include
#include
#include
#include
#include
#define pi 3.14
class per
{
    float x[10], y[10], z [10], d;
    float x1 [10], y1[10], xm, ym, z1 [10];
    int i, k, j, n;
    public:
    void get();
    void perpro();
    void map();
    void graph();
    void plot1();
}
void per::get()
{
    cout<< "\n ENTER OF VERTICES";
            cin>>n;
    cout<<"\n ENTER";
            for (i=0;i>x[i]>>y[i]>>z[i];
    }
        cout<<"\n ENTER WIDTH FOR Z-AXIS-d";
        cin>>d;
}
void per:: perpro()
{
    for(i=0;i

输出量

实现3维平行投影的程序。

#include
#include
#include
#include
#include
#include
#define pi 3.14
class per
{
    float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10];
    float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10];
    float p1[10] [10];
    float a, b, c, ang, theta, f;
    int i, k, j, n;
    public:
    void get();
    void par_v();
    void par_vnr0();
    void map();
    void graph();
    void project(float m[],float q[], float r[]);
};
void per:: perproject(float m[], float q[], float r[])
{
    cout<<"\n ENTER WIDTH FOR Z-AXIS d";
    cin>>d;
    cout<< "\n AFTER PROJECTION, VERTICES OF POLYGON ARE--";
            for(i=0;i>n;
    cout<<"\n ENTER";
            for (i=0;i>x[i]>>y[i]>>z[i];
    }
        cout<<"\n ENTER WIDTH FOR Z-AXIS-d";
        cin>>d;
        int ch=100;
        do
        {
            cout<< "\n 1->orthographic projection";
                                cout<<"\n 2->oblique projection";
                                cout<<"\n 0-> exit";
            cout<<"\n enter ch";
            cin>>ch;
            switch (ch)
            {
                case1:
                    int ch1=100;
                do
                {
                    cout<<"\n 1->for xy viewplane";
                    cout<<"\n 2->for yz viewplane";
                    cout"\n 3->for zx viewplane";
                    cout<<"\n 0->exit";
                    cout<<"\n enter ch1";
                    cin>>ch1;
                    switch(ch1)
                    {
                        case1:
                            x1[i]=x[i];
                            y1[i]=y[i];
                            z1[i]=0;
                            project(x1,y1,z1);
                            break;
                        case2:
                            x1[i]=0;
                            y1[i]=0;
                            z1[i]=z[i];
                            project(x1,y1,z1);
                            break;
                        case3:
                            x1[i]=x[i];
                            y1[i]=0;
                            z1[i]=z[i];
                            project(x1,y1,z1);
                            break;
                    }
                }
            while (ch!=0)
                 break;
                   case2:
                ch1=100;
                do
                {
                    cout<<"\n 1->on xy plane";
                    cout<<"\n 2->cavalier";
                    cout<<"\n 0->exit";
                    cout<<"\n enter ch1";
                    cin>>ch1;
                    switch(ch1)
                    {
                        case1:
                            par_v();
                            break;
                        case2:
                            par_vnr0();
                            break();
                    }
                    while(ch!=0);
                    break;
                }
                while(ch!=0)
            }
    void per::par_v()
    {
        cout<<"\n enter v (a, b, c) "    ;
        cin>>a>>b>>c;
        for(i=0; i>f;
        cout<<"\n enter angle";
        cin>>angle;
        theta= ((pi/180)* ang);
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                if(i==j)
                cav[i] [j]=1;
                else
                    cav[i] [j]=0;
            }
        }
        cav[0] [2]=f*cos(theta);
        cav[1][2]=f*sin (theta);
        cav[2][2]=0;
        for(i=0;i

输出量

绘制正弦,余弦和正切曲线的程序

#include
#include
#include
#include
#include
#include
#define pi 3.14
class arc
{
    float x, y, a, xm, ym;
    int i, ch;
    public:
    void get();
    void map();
    void sine();
    void cosine();
    void tangent();
};
void arc::get()
{
    cout<<"\n ENTER THE MAXIMUM VALUE FOR Y";
           cin>>y;
           cout<<"\n MENU IS---------";
    cout<<"\n 1-> SINE CURVE";
           cout<<"\n 2->COSINE CURVE";
    cout<<"\n 3-> TANGENT CURVE";
    cout<<"\n 4-> EXIT";
    cout<<"\n ENTER YOUR CHOICE";
           cin>>ch;
           switch(ch)
           {
                     case1:
            sine();
            break();
        case2:
            cosine();
        case3:
            tangent();
        case4:
            exit(0);
    }
}
void arc::sine()
{
    cleardevice();
    xm=getmaxx()/2;
    ym=getmaxy()/2;
    line(xm, 0, xm, 2*ym);
    line(0, ym, 2 *xm, ym);
    outtextxy(0, ym, "X-AXIS");
           outtextxy(xm, 0, "Y-AXIS");
    for(x=-300;x<=300;x=x+0.5)
    {
        y=a*sin((pi*x)/180);
        putpixel(x+(320),-y+240,RED);
    }
}
void arc::cosine()
{
    cleardevice();
    xm=getmaxx()/2;
    ym=getmaxy()/2;
    line(xm, 0, xm, 2*ym);
    line(0, ym, 2 *xm, ym);
    outtextxy(0, ym, "X-AXIS");
           outtextxy(xm, 0, "Y-AXIS");
    for(x=-300;x<=300;x=x+0.5)
    {
        y=a*cos((pi*x)/180);
        putpixel(x+(320),-y+240,RED);
    }
}
void arc :: map()
{
    int gd=DETECT,gm;
    initgraph (&gd, &gm, "");
    int errorcode = graphresult();
    /*an error occurred */
           if (errorcode!=grOK)
    {
        printf("Graphics error: %s \n",grapherrormsg (errorcode));
                      printf("Press and key to halt: ");
                      getch();
                      exit(1); /* terminate with an error code */
            }
}
void main()
{
    class arc a;
    clrscr();
    a.map();
    a.get();
    getch();
}

输出量