Sunday, April 30, 2017

C Program for creating animated circles

Graphics in C – Program to Generate Animated Circles


.Generating animation in C programming language is very interesting Here is one more program in C computer language using animation. This program generates ten concentric circles bit by bit. To clear the point, please type this program in your editor.

#include<graphics.h>
#include<stdlib.h>
#include<time.h>
void main()
{
        int gd = DETECT, gm;
        int i, j, start = 0, end = 5;
        int maxx, maxy, rad = 20;
        initgraph(&gd, &gm, "c:\\tc\\bgi");
        maxx = getmaxx();
        maxy = getmaxy();
        randomize();
        for(i = 0; i < 10; i++)
        {
                setcolor(random(15) + 1);
                for(j = 0; j <= 360; j+=5)
                {
                       arc(maxx/2, maxy/2, start, end, rad);
                       delay(40);
                       start = end;
                       end += 5;
                }
                rad += 20;
        }
        setcolor(WHITE);
        outtextxy(maxx/2-50, maxy-20, "Press any key...");
        getch();
        closegraph();
        restorecrtmode();
}

Compile and execute this program. You will see ten concentric circles completing the circumference slowly and one by one in different colors. One circle completes then other starts. In this manner, ten concentric circles have been created completing slowly in the outward direction.

In this program, I have taken two nested ‘for’ loops. Outer ‘for’ loop iterates ten times, hence creates ten circles. Inner ‘for’ loop is responsible for creating animation effect.

In the start, outer ‘for’ loop picks a random value for the color, and sets that color.

In the inner ‘for’ loop, loop variable starts from the value ‘0’ and iterates until its value reaches ‘360 degrees’. It is because of the fact that a point on the circumference of a circle moves 360 degrees, while returning on the same point after completing the circle. This variable is incremented ‘5’ after each iteration. Function arc() draws an arc of ‘5’ degrees in each iteration. Then, variable ‘start’ is given the value of variable ‘end’, and ‘end’ is incremented ‘5’ degrees. This way, circle completes bit by bit. Function delay() has been used to give a delay of  ‘40’ milliseconds after every arc of 5 degrees, thus giving the effect of animation. After each iteration, variable ‘rad’(radius of the circle) is incremented ’20 px’ for forming a bigger  circle. Please note that the coordinates of the centers of the circles do not change, thus forming concentric circles.


So, this is a very simple program  giving great effect. For the explanation of the functions and terms I have used in this program, please refer my previous posts.

OUTPUT:

No comments:

Post a Comment

Please give your comment