Magic of C programming | drawing shapes and animation in c programming| graphics in c

 Drawing shapes and animation with  C programming 



How to move(animate) object

So simple concept is to draw multiple lines with the same source but each next line should be longer than previous one most important all these activity should happen in milliseconds then user will have an illusion that line is stretching or growing.



Note :-
 
 Must remember that the resolutions of graphic screen is (640*480) pixels.





Printing lines in c:

Syntax : line(X1,Y1,X2,Y2);



Line drawing program:

#include<stdio.h>

#include<conio.h>

#include<graphic.h>

Void main()

{

int gmode= DETECT, gdriver;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

line(50,50,300,300);

getch();

}


Output:




Drawing horizontal line in c:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

Void main()

{

int gmode= DETECT, gdriver;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

line(50,50,300,50);

getch();

}


Output:



Drawing vertical line in C:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

Void main()

{

int gmode= DETECT, gdriver;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

line(50,50,50,300);

getch();

}



Output:




Drawing circle in c program:



Note:

Centre of screen : (320,240)


Syntax : circle(x,y, radius);


Circle drawing program:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

Void main()

{

int gmode= DETECT, gdriver;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

Circle(320,240,100);

getch();

}



Let's create a good animated background:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

Void main()

{

int gmode= DETECT, gdriver;

Int i;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(i=2;i<=100;i+=2)


Circle(320,240,i);

getch();

}




How to use delay function in C:


  • Delay(milliseconds)
  • If you write delay (1000) then it will pause working of C compiler for 1 second.
  • 1sec = 1000 milliseconds
  • So let's update previous code now and let observe the  exciting result.

#include<stdio.h>

#include<conio.h>

#include<graphic.h>

#include<dos.h>

Void main()

{

int gmode= DETECT, gdriver;

Int i;

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(i=2;i<=100;i+=2)

{

Circle(320,240,i);
delay(50);
}

getch();

}


How to draw points on screen:


  • Putpixel(x,y, Colour);
  • Predefined method which prints a dot on output screen on the location having coordinates (X ,y) with colour code colour.
  • Now all you have to do is put different dots by following equation of circle so it will draw point in a circular fashion.

Code to draw dotted animated circle:




#include<stdio.h>

#include<conio.h>

#include<graphic.h>

#include<dos.h>

Void main()

{

int gmode= DETECT, gdriver;
float a=0.00;
float x,y; 

intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(;a<6.28;)
{

X= 320+cos(a)*100;

Y=240+sin(a)*100;

putpixel (x,y,WHITE);

a+=0.1;
delay (50);
}


getch();

}



Why sound exists:


  • Frequency
  • Human ears are capable of listening sound between 20 hertz to 20000 hz.
  • Frequency is low sound will be heavy and thick.
  • Frequencies high sound will be thin and sharp.

Code to produce sound:


  • Pre define function in header file <dos.h>.
  • Syntax : sound(number).
  • Number is frequency which should be between 20 to 20000.
#include<dos.h>

Void main()
{

Sound(50);
getch();
}




How to produce music ( useful sound)


Music is Just a combination of multiple sound produced at different frequency.

Code to produce music:


#include<stdio.h>
#include<conio.h>
#include<dos.h>
Void main()
{

Int i;

for(i=50;i<5000;i++)

{

sound(i);
delay(20);
}

nosound();

getch();
}


Note:

  • Don't forget to use delay function otherwise all sound will be produced so fast that you will not able to listen.
  • You can listen only last sound that has frequency 5000hz.

How to produce discrete sound:



#include<stdio.h>
#include<conio.h>
#include<dos.h>
Void main()
{

Int i,j;

for(i=50,j=4000;i<5000;i+=30,j-=30)

{

sound(i);
nosound(50);
delay(50);
sound(j);
delay(50);
nosound(50);
}

nosound();

getch();
}



How to move (animate) object.


So simple concept is to draw multiple lines with same source but each next line should be longer than previous one most important all these activity should happen in milliseconds then user will have an illusion that line is stretching or growing.

Example 1:

#include<stdio.h>

#include<conio.h>

#include<graphic.h>

#include<dos.h>

Void main()

{

int gmode= DETECT, gdriver;



intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(i=50;i<=500;i+=5)

{

line(50,50,50,i);
delay(50);
}

getch();

}

Example 2:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

#include<dos.h>

Void main()

{

int gmode= DETECT, gdriver;



intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(i=50;i<=500;i+=5)

{
line(50,50,i,50);
line(50,50,50,i);
delay(50);
}

getch();

}




Moving circle animation:


#include<stdio.h>

#include<conio.h>

#include<graphic.h>

#include<dos.h>

Void main()

{

int gmode= DETECT, gdriver;



intitgraph(&gmode, &gdriver,"C:\\tc\\bgi");

for(i=100;i<=400;i+=20)

{
setcolor(WHITE);

circle(i,240,10);

delay(50);

setcolor(BLACK);

circle(i,240,10);

delay(50);

}
setcolor(WHITE);

circle(i,240,10);
getch();

}