Search This Blog

Showing posts with label computer graphics lab. Show all posts
Showing posts with label computer graphics lab. Show all posts

C Program for Polygon Clipping by Another Polygon - Computer Graphics Lab Program

Polygon clipping is one of the classic problem in computer graphics lab. Generally, polygons or whatever objects, are clipped by a rectangular clipping windows. But, this one is a little more challenging graphics lab experiment in which we have to clip polygon with another polygon. That is the clipping windows is actually another polygon. The following c program implements polygon clipping with another polygon. The program clips any polygon with the clipping polygon, given both should be convex polygons.

Bresenhams Circle Drawing Algorithm Implementation in C

Bresenham's circle drawing algorithm is one of the basic algorithms in computer graphics. Bresenham's circle drawing algorithm helps to draw circles with minimal calculations. Here is a simple c program that implements Bresenham's circle drawing algorithm.

The program is as follows:

C Program to Implement Midpoint Circle Drawing Algorithm

Midpoint circle drawing algorithm is another basic algorithms in computer graphics. Midpoint circle drawing algorithm is used to draw circles with minimal calculations. Here is a simple c program that implements Midpoint circle drawing algorithm.

Rotation of Wheel - C Program for Computer Graphics Lab

In computer graphics experiments, we need to rotate objects like wheels while doing animation. This is a simple c program that shows rotation of a wheel. A circle is drawn and four radial lines are drawn at 90 degree separation. These lines are drawn from center to a point on circle which depends on the value of theta, a variable. As the value of theta changes, the end points of the radial lines changes. Therefore, increasing the value of theta in a loop give counter clockwise rotaion and decreasing gives clockwise rotation.


The program is as follows:

C Program to Draw Bezier Curve - Graphics Lab Program

This is a c program to draw a bezier curve. You can draw a bezier curve by giving control points. The control points' co-ordinates are stored in arrays x[] and y[]. You can draw different curves by combining multiple bezier curves. We will see such examples in some other posts. Here, this program has a function named bezier which draws the curve using an array of control points declared globally.

C Program to Draw Fern Leaf using Fractals - Computer Graphics Experiment

Fern is a plant with leaves that follow a pattern. A whole fern plant can be drawn using simply lines. Drawing fern is a simple example for fractals in computer graphics. The following program draws a fern plant using the idea of fractals. This is a computer graphics lab experiment.

Keyboard Controlled Car Animation in C - Computer Graphics Lab Program

This is a computer graphics lab program. This is an animation program in which a car can be moved using the arrow keys on keyboard. The car should move forward when right arrow key is pressed and backward when left arrow key is pressed. The car accelerates as the right arrow key is pressed. When it is released the car decelerates. 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:

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:

C Program for Reflection Transformation for Computer Graphics Lab

Reflection is one of the 2d transformation operations in computer graphics. Here i present a c program to implement reflection. The reflection transformation can be applied on any polygon given by user using this c program. Reflection of points is done which results is reflection of lines or polygons drawn from the points. The reflection transformation is sometimes called flipping or mirroring. Reflection of a polygon about horizontal axis is called vertical flip and reflection about vertical axis is called vertical flip. Whatever we call, flipping or reflecting it is producing mirror image about an axis. The following c program performs reflection transformation on given polygon about both axes.

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.


Caterpillar Eating Leaf - Computer Graphics Lab Animation in C

This is a CG lab program in C using graphics.h library to animate a caterpillar eating a leaf. As it eats, the caterpillar (or worm) should grow proportionally in size. The leaf should also be reduced in size. The program is given below:
 The ouput is:


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:




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

    }

}