Graphics program in C



How to include graphics.h ?

<graphics.h>  is a header file which is used to run the graphics program graphics.h is header file in C/C++ which has many predefined function which is used to create many different graphics as you see below while writing program for any graphics related you must include graphics.h header file in your source file and also initialise the graphics driver and graphics data
 initgraph(&gd, &gm, "C:\\turboc3\\bgi");

If you don't have graphics.h header file in your compiler download the header file from here 

                                                  Download Here

Program to draw a car using graphics in c language with source code :


#include <graphics.h> 

#include <stdio.h> 

  

// Function to draw moving car 

void draw_moving_car(void) { 

  

    int i, gd = DETECT, gm;

  

    // Passed three arguments to initgraph 

    // function to initialize graphics mode 

    initgraph(&gd, &gm, "C:\\turboc3\\bgi");

  

    for (i = 0; i <= 420; i = i + 10) { 

  

        // Set color of car as red 

        setcolor(RED); 

  

               // Thease lines for bonnet and

               // body of car

               line(0 + i, 300, 210 + i, 300);

               line(50 + i, 300, 75 + i, 270);

               line(75 + i, 270, 150 + i, 270);

               line(150 + i, 270, 165 + i, 300);

               line(0 + i, 300, 0 + i, 330);

               line(210 + i, 300, 210 + i, 330);

 

               // For left wheel of car

               circle(65 + i, 330, 15);

               circle(65 + i, 330, 2);

 

               // For right wheel of car

               circle(145 + i, 330, 15);

               circle(145 + i, 330, 2);

 

               // Line left of left wheel

               line(0 + i, 330, 50 + i, 330);

 

               // Line middle of both wheel

               line(80 + i, 330, 130 + i, 330);

 

               // Line right of right wheel

               line(210 + i, 330, 160 + i, 330);

 

               delay(100);

 

               // To erase previous drawn car, draw

               // the whole car at same possition

               // but color using black

               setcolor(BLACK);

 

               // Lines for bonnet and body of car

               line(0 + i, 300, 210 + i, 300);

               line(50 + i, 300, 75 + i, 270);

               line(75 + i, 270, 150 + i, 270);

               line(150 + i, 270, 165 + i, 300);

               line(0 + i, 300, 0 + i, 330);

               line(210 + i, 300, 210 + i, 330);

 

               // For left wheel of car

               circle(65 + i, 330, 15);

               circle(65 + i, 330, 2);

 

               // For right wheel of car

               circle(145 + i, 330, 15);

               circle(145 + i, 330, 2);

 

               // Line left of left wheel

               line(0 + i, 330, 50 + i, 330);

 

               // Line middle of both wheel

               line(80 + i, 330, 130 + i, 330);

 

               // Line right of right wheel

               line(210 + i, 330, 160 + i, 330);

 

    }

 

    getch();

 

    closegraph();

}

 

// Driver code

int main()

{

    draw_moving_car();

    line(10,10,200,10);

    return 0; 

}

Output 



Program to make an animation in c language of rainbow  rainbow (rainbow animation using graphics):


#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

{

int gdriver = DETECT,gmode;

int x,y,i;

               initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");

               x=getmaxx()/2;

               y=getmaxy()/2;

               for(i=30;i<200;i++)

               {

                              delay(100);

                              setcolor(i/10);

                              arc(x,y,0,180,i-10);

               }

getch();

}

Output:
                        


Program to make animation of watch using graphics in C language  with source code:


#include<conio.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

 

#define WBC 5

//^watchbackcolor

#define X 200

#define Y 200

 

void dial(int x, int y);

void sechand(int timeminute);

 

void minhand(int t)

{

int x1,y1;

setlinestyle(0,0,3);

 

x1= X+ (80 * cos(t*0.1047));

y1= Y+ (80 * sin(t*0.1047));

 

setcolor(BLACK);

line( X, Y, x1, y1);

 

setcolor(WBC+1);

line( X, Y, X+ 80 * cos((t-1)*0.1047),Y+ 80 * sin((t-1)*0.1047));

circle(X,Y,4);

}

 

void sechand(int t)

{

int x1,y1;

setlinestyle(0,0,3);

 

x1= X+(100 * cos(t*0.1047));

y1= Y+(100 * sin(t*0.1047));

 

setcolor(RED);

line(X, Y, x1, y1);

 

setcolor(WBC+1);

line(X, Y, X+ 100 * cos((t-1)*0.1047),Y+ 100 * sin((t-1)*0.1047));

 

circle(X,Y,4);

}

 

void dial(int x,int y)

{

int const size=200;

 

setfillstyle(1,WBC);

fillellipse(x,y,size,size);

 

setfillstyle(1,WBC+1);

fillellipse(x,y,size-20,size-20);

 

outtextxy(x,y-(size-40),"12");

outtextxy(x,y+(size-40),"6");

outtextxy(x+(size-40),y,"3");

outtextxy(x-(size-40),y,"9");

outtextxy(x+size/3,y-2*size/3,"1");

outtextxy(x+2*size/3,y-size/3,"2");

outtextxy(x+2*size/3,y+size/3,"4");

outtextxy(x+size/3,y+2*size/3,"5");

outtextxy(x-size/3,y+2*size/3,"7");

outtextxy(x-2*size/3,y+size/3,"8");

outtextxy(x-size/3,y-2*size/3,"11");

outtextxy(x-2*size/3,y-size/3,"10");

 

circle(x,y,4);

}

 

void main()

{

int gd=DETECT, gm,i,j, flag=1;

initgraph(&gd,&gm,"C:\\turboc3\\bgi");

 

dial(200,200);

do

{

minhand(i);

for(j=0;j<60;j++)

{

sechand(j);

delay(1000

);

if(kbhit()) {

flag =0;

break;

}

}

i++;

}while(flag);

closegraph();

}

Output:

                             


Program to print heart pattern in c language with source code


#include <stdio.h>

#include <math.h>

#include <string.h>

 

// C program to print heart pattern

int main()

{

               int size = 10;

 

               char* message = " I love You ";

               int n = strlen(message);

 

               int print_line = 4;

 

               for (int x = 0; x < size; x++)

               {

                              for (int y = 0; y <= 4 * size; y++)

                              {

                                             double dist1 = sqrt(pow(x - size, 2) + pow(y - size, 2));

                                             double dist2 = sqrt(pow(x - size, 2) + pow(y - 3 * size, 2));

 

                                             if (dist1 < size + 0.5 || dist2 < size + 0.5)

                                                            printf("*");

                                             else

                                                            printf(" ");

                              }

                              printf("\n");

               }

 

               for (int x = 1; x < 2 * size; x++)

               {

                              for (int y = 0; y < x; y++)

                                             printf(" ");

 

                              for (int y = 0; y < 4 * size + 1 - 2 * x; y++)

                              {

                                             if (x >= print_line - 1 && x <= print_line + 1)

                                             {

                                                            int idx = y - (4 * size - 2 * x - n) / 2;

                                                            if (idx < n && idx >= 0)

                                                            {

                                                                           if (x == print_line)

                                                                                          printf("%c", message[idx]);

                                                                           else

                                                                                          printf(" ");

                                                            }

                                                            else

                                                                           printf("*");

                                             }

                                             else

                                                            printf("*");

                              }

                              printf("\n");

               }

}

Output












Post a Comment

Previous Post Next Post