Search This Blog

Showing posts with label cglab. Show all posts
Showing posts with label cglab. Show all posts

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.


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