Search This Blog

Showing posts with label animation in c. Show all posts
Showing posts with label animation in c. Show all posts

Two Stroke Engine Piston Movement Animation C Program - CG Lab Program

This program simulates the motion of the components of a two stroke engine. It shows the movement of piston, connecting rod and crank by animating them in c. The program uses the graphics library (grapics.h) to simulate the piston movement in two stroke engine. This is a computer graphics lab (CG lab) problem. The program is given below. The program uses sin and cos functions to simulate the crank. Also it uses the equation to find the distance between to points.


Output:



Program


#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int tt,midx,midy,radius=40,rodlen=150,deg=0,jointx,jointy,px;
float radian,sqrodlen,t;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
midx=getmaxx()/2;
midy=getmaxy()/2;
sqrodlen=rodlen*rodlen;

while(!kbhit())
 {
 circle(midx,midy,radius);
 radian=(float)deg*3.14f/180;
 jointx=midx+radius*sin(radian);
 jointy=midy+radius*cos(radian);
 line(midx,midy,jointx,jointy);
 t=pow(jointy-midy,2);
 tt=(int)sqrt(sqrodlen-t);
 px=jointx-tt;
 line(jointx,jointy,px,midy);
 rectangle(px-30,midy-10,px+20,midy+10);
 rectangle(px-45,midy-35,px-30,midy+35);
 rectangle(midx-240,midy-35,midx-90,midy+35);
 deg+=10;
 delay(80);
 cleardevice();
 }
}

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);
    

    }

}