This is a computer graphics lab experiment. The question was to animate both rotation and revolution of two wheels in opposite direction. Both the wheels move in a path in a of shape 'D'. If one moves in clockwise direction, the other moves in counter clockwise direction. The output of the program is as follows:
The program is as follows:
Search This Blog
Showing posts with label CG lab. Show all posts
Showing posts with label CG lab. Show all posts
Man Rowing Boat Animation - Computer Graphics Lab Animation Experiment
This is a computer graphics lab program. This is an animation program. The experiment is to animate a scene of a man rowing boat in a river on a rainy day. The man wears a hat also. The
program uses the graphics library graphics.h. This program works well in the turbo c compiler with Borland graphics interface (BGI). The output of the program is as follows:
The program is as follows:
The program is as follows:
C Program for Shear Transformation - Shear Transformation in 2D Computer Graphics
Shear transformation or shearing is one of the 2d transformations in computer graphics. Here i present a c program for shear transformation of polygons. The shear transformation works as follows:
For shearing along X axis, shearfactor* (y coordinate) is added with x co-ordinates of all points. That is:
Similarly, to shear along Y axis, we add shearfactor* (x coordinate) to y co-ordinates of all points.
For shearing along X axis, shearfactor* (y coordinate) is added with x co-ordinates of all points. That is:
newx=oldx+shearfactor*oldy
Similarly, to shear along Y axis, we add shearfactor* (x coordinate) to y co-ordinates of all points.
newy=oldy+shearfactor*oldx
C Program for Scaling 2D transformation - Scaling Polygons in Computer Graphics
This is a c program for scaling transformation in computer graphics. Scaling is one of the important 2d transformations in computer graphics. The program will tell you how to scale lines or polygons. This CG lab program in c language using the graphics library reads the number of sides of polygon, co-ordinates of its vertices and the scale factors in horizontal and vertical directions. It displays the original polygon and scaled polygon in different colors in same screen. More Computer graphics lab problems are available under related posts heading.
C Program for Rotation of Polygon - 2D Transformation in Computer Graphics
This is a c program for rotation transformation in computer graphics. Rotation is one of the important 2d transformations in computer graphics. The program will tell you how to rotate points or polygon around a point (the pivot point). This CG lab program in c language using the graphics library reads the number of sides of polygon, co-ordinates of its vertices, the pivot point for rotation, and angle of rotation. It displays the original polygon and rotated polygon in different colors in same screen. More Computer graphics lab problems are available under related posts heading.
C Program for Translation 2D Transformation in Computer Graphics
This is a c program for translation transformation in computer graphics. Translation (or shifting) is a very basic 2d transformation operation in computer graphics. Translation or shifting is done by adding the distance to shifted to the co-ordinates. For example, if we want to shift a polygon 50 units (pixels) horizontally, we just have to add 50 to x co-ordinates of all vertices of the polygon. Similarly, if it is translate only vertically by 100 unit, add 100 to y co-ordinates of all vertices. This CG lab program in c language using the graphics library reads the number of sides of polygon, co-ordinates of its vertices and the translation distance along both axes. It displays the original polygon and translated polygon in different colors in same screen. This is only a very simple and basic Computer graphics lab problem.
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();
}
}
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();
}
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:
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);
}
}
Subscribe to:
Posts (Atom)