Thursday, September 20, 2012

Graphs with Turbo-C–I


Graphics Routine :

Graphics are fun and we can easily make some program based 2-D animation or some fun games.

Turbo C have some built-in graphics command. All graphic utilites are present in the ‘graphics.h’, header file. First we have to find the Graphic mode and Graphic driver for the system we are using. ‘detectgraph’ used for detecting  the graphics adapter and the mode. ‘detectgraph’ needs two integer arguments to detect the driver and mode. Once it is done then we have to initialize the graph by ‘initgraph’ funtion. In 'initgraph' function  we have passed three arguments first is the address of graphic driver, second is the address of graphic mode, that used in ‘detectgraph’ function. The third is the path where your BGI files are present. The BGI path will be vary from machine to machine and one has to adjust this accordingly.

Here are some example of the graphics routine among many functions that are available in the ‘graphics.h’.






#include <stdio.h>
#include<graphics.h>

  void Info()
      {
           int x,y,c;
           char ch;
           x=getmaxx();
           y=getmaxy();
           c=getmaxcolor();
           printf("\n\n\n\tMaximum X and Y coridinate : %d  %d ",x,y);
           printf("\n\n\n\tMaximum Color : %d ",c);
           printf("\n\n\n\tPress Enter to Continue  ");
           ch=getchar();
           cleardevice();
      }
  void LineRect()
      {
           outtextxy(30,20,"Horizontal Line ");
           line(50,50,150,50);
           outtextxy(200,20,"Vertical Line ");
           line(250,50,250,150);
           outtextxy(350,20,"Slant Line ");
           line(350,50,450,150);
           outtextxy(480,20,"Rectangle ");
           rectangle(480,50,580,150);
      }
  void Circular()
      {
           outtextxy(20,200,"Circle ");
           circle(50,250,30);
           outtextxy(120,200,"Ellipse ");
           ellipse(150,250,90,270,30,20);
           outtextxy(200,200,"Sector");
           sector(230,250,270,0,30,20);
      }
  void BarPoly()
      {
           int poly[8];
           char ch;
           outtextxy(300,200,"Bar");
           bar(300,220,330,270);
           outtextxy(390,200,"Bar-3D");
           bar3d(375,230,410,270,10,10);
           outtextxy(490,200,"Polygon");
           poly[0]=525;
           poly[1]=220;
           poly[2]=575;
           poly[3]=270;
           poly[4]=475;
           poly[5]=270;
           poly[6]=poly[0];
           poly[7]=poly[1];
           drawpoly(4,poly);
           outtextxy(200,350,"Press any Key to Cont..");
           ch=getchar();
           cleardevice();
      }
  void ColFill()
      {
           char ch;
           int i,j=1,k;
           outtextxy(120,100,"Fill & Color");
           for(i=50;i<=600;i=i+70)
              {
                   j=j+1;
                   setcolor(j);
                   setfillstyle(j,k);
                   circle(i,150,30);
                   floodfill(i,150,j);
                   if(j>=15)
                        {
                            j=1;
                        }
                   if(k>=10)
                        {
                            k=1;
                        }
                   k=k+1;
              }
           outtextxy(200,350,"Press any Key to Cont..");
           ch=getchar();
           cleardevice();
      }
  void Font()
      {
           char ch;
           int i,j=1,k=1,f=20;
           outtextxy(120,10,"Font Demo");
           for(i=20;i<=560;i=i+120)
              {
                   for(f=20;f<=320;f=f+100)
                   {
                        settextstyle(j,0,k);
                        outtextxy(i,f,"Hi");
                        j++;
                        if(j>5)
                            {
                                j=1;
                            }
                        k++;
                        if(k>10)
                            {
                                k=1;
                            }
                   }
              }
           settextstyle(1,1,1);
           outtextxy(250,350,"Hello");
      }
  int main()
      {
           int gd,gm;
           char ch;
           detectgraph(&gd,&gm); //Detecting the Graphic Driver and mode
           initgraph(&gd,&gm,""); //initializing the graph
           Info();
           LineRect();
           Circular();
           BarPoly();
           ColFill();
           Font();
           settextstyle(1,0,1);
           outtextxy(20,430,"Press any Key to Cont..");
           ch=getchar();
           closegraph();
      }

No comments: