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:
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment