Sunday 21 June 2015

C graphics examples

1. Drawing concentric circles
#include <graphics.h>
 
int main()
{ 
   int gd = DETECT, gm;
   int x = 320, y = 240, radius;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   for ( radius = 25; radius <= 125 ; radius = radius + 20)
      circle(x, y, radius);
 
   getch();
   closegraph();
   return 0;
}
2. C graphics program moving car
#include <graphics.h>
#include <dos.h>
 
int main()
{
   int i, j = 0, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
   outtextxy(25,240,"Press any key to view the moving car");
 
   getch();
 
   for( i = 0 ; i <= 420 ; i = i + 10, j++ )
   {
      rectangle(50+i,275,150+i,400);
      rectangle(150+i,350,200+i,400);
      circle(75+i,410,10);
      circle(175+i,410,10);
      setcolor(j);
      delay(100);
 
      if( i == 420 )
         break;
      if ( j == 15 )
         j = 2;
 
      cleardevice(); // clear screen
   }
 
   getch();
   closegraph();
   return 0;
}

Animation Graphics in Windows 7

Most of the functions are two dimensional except bar3d which draws a 3d bar, you can also implement these functions using already existing algorithms. You can also use these functions in C++ programs. You can use these functions for developing programs in Windows 7 and Vista using Dev C++ compiler. For that you need to download an additional package WinBGIm, download WinBGIm. Now open Dev C++ compiler go to tools->Package Manager, use install button and then browse the package location. Now create new project and select WinBGIm. This library also offers many functions which can be used for image manipulation, you can open image files, create bitmaps and print images, RGB colors and mouse handling.

No comments:

Post a Comment