Search This Blog

Blooming Flower and Bee Animation C Program - Computer Graphics Lab Experiment

One of my graphics lab questions was to write a c program to animate a blooming flower and a bee (or a beetle) that comes to the flower, sit on it and fly away. The program uses graphics.h header file.

Output:



Program 

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int i,xmax,ymax;
int startx,starty;
int stopped=0;

void drawFlower(int i)
{
setcolor(GREEN);
line(xmax/2,ymax/2,xmax/2,ymax/2+100);
setcolor(BROWN);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(xmax/2,ymax/2,10+1.8*i,5+.6*i);
fillellipse(xmax/2,ymax/2,5+.6*i,10+1.8*i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,RED);
fillellipse(xmax/2,ymax/2,5+.6*i,5+.6*i);
}

void drawBee(int i)
{
startx+=2;
starty+=7*sin(i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,BLUE);
fillellipse(startx,starty,12,5);
setcolor(BLUE);
fillellipse(startx,starty,5,10*(i%2));
fillellipse(startx+8,starty-3,1,1);
fillellipse(startx+8,starty+3,1,1);
line(startx+9,starty-2,startx+11,starty-5);
line(startx+9,starty+2,startx+11,starty+5);
}

void main()
{
int max,may,i,t;
int gm=DETECT,gd;
initgraph(&gm,&gd," ");
xmax=getmaxx();
ymax=getmaxy();
startx=xmax/2-100;
starty=ymax/2;
setbkcolor(WHITE);
getch();
for(i=0;i<20;i++)
 {
 delay(120);
 cleardevice();
 drawFlower(i);
 }
t=i;

for(i=0;startx+i<xmax-60;i++)
       {
       delay(90);
       cleardevice();
       drawFlower(t);
       drawBee(i);
       if(stopped==0&&abs(startx-xmax/2)<10&&abs(starty-ymax/2)<15)
  {
  delay(2500);
  stopped=1;
  }
       }
getch();

}


C Program to get a Substring from a String from Given Index

In this post we will see a c program to get a substring from a given main string from specified index to a specified index. For example, consider given a string mainstring  which is equal to "cSourceCodes.blogsppot.com". Calling the function subString(mainstring,7,11) will return "Codes".

Arguments (parameters):
First - the original string
Second - The starting index (position) of substring (counting starts from zero)
Third - The end index of substring

Both the start index and end index are inclusive.

#include<stdio.h>
#include<string.h>

char * subString(char *MainString,int fromIndex,int toIndex)
{
int Mainlength,j;
char *subStr;
Mainlength=strlen(MainString);
if(fromIndex<0||fromIndex>=Mainlength||toIndex<fromIndex||toIndex>=Mainlength)
 {
 printf("\nError in args: subString fn");
 return(NULL);
 }
subStr=(char *)malloc(1000*sizeof(char));
for(j=0;j<=toIndex-fromIndex;j++)
 *(subStr+j)=*(MainString+fromIndex+j);

*(subStr+j)='\0';
return(subStr);
}

void main()
{
char mainstring[]="cSourceCodes.blogsppot.com";
printf("%s",subString(mainstring,7,11));
getch();

}


Computer Graphics Program in C to Simulate Motion of Simple Pendulum

In this post, we see a simple C program to simulate motion of a simple pendulum using graphics.h. Actually, this was a problem given in my CG lab (Computer Graphics lab). The program uses graphics.h header file. If you are using linux gcc, you have to include libgraph library. The header file graphics.h is already included in turboc or turboc++ compilers.

If you want corresponding java program, get it here: Java Program to Simulate Motion of Simple Pendulum.

The output of the program is as follows:




Program:


#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define PI 3.14

int gd=DETECT,gm;
int pivotx,pivoty;
double thetamax,theta;;
double len=260;
int x,y,ymax,xmax;
int bobradius=30;
int xsign=-1,ysign=1;
double omega;

void paint()
{
cleardevice();
setcolor(RED);
fillellipse(pivotx,pivoty,8,8);
line(pivotx,pivoty, x,y);
fillellipse(x,y,bobradius,bobradius);
}

void main()
{
 double decr;
 initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
 thetamax=60*PI/180;
 pivotx=getmaxx()/2;
 pivoty=30;
 ymax=(int) (pivoty+len*cos(thetamax));
 xmax=(int) (pivotx+len*sin(thetamax));
 x=xmax;
        y=ymax;
        theta=thetamax;

while(1)
      {
      if(x>=pivotx+abs(len*sin(thetamax)))
        {
        xsign=-1;
 ysign*=-1;
        x=xmax-1;
        delay(40);
        }
      else if(x<=pivotx-abs(len*sin(thetamax)))
      {
          ysign*=-1;
          xsign=1;
          x=(int) (pivotx-abs(len*sin(thetamax))+2);
          delay(40);
      }
      else if(y>=pivoty+len)
 {
              ysign*=-1;
        }

      omega=y/60*PI/180;
      decr=xsign*omega;
      theta=theta+decr;
      x=(int) (pivotx+len*sin(theta));
      y=(int) (pivoty+len*cos(theta));
      paint();
      delay(40);
    

    }

}