Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Thursday, September 19, 2013

MOVING FISH C PROGRAM

7:42 PM ANKIT








     MOVING FISH C PROGRAM

This is a computer graphics program which shows movement of a fish like object .this is why it is also known as the moving fish program.The aim of the program is to make the fish move in a direction giving an impression of a moving fish.

Implementation in C

#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<ctype.h>
 
void main()
{
        int gd=DETECT,gm;
        int xincr=0,newy=0,yincr=5;
 
        initgraph(&gd,&gm," ");
        cleardevice();
        while(!kbhit())
        {
                ellipse(520-xincr,200,30,330,90,30);
                circle(450-xincr,193,3);
                line(430-xincr,200,450-xincr,200);
                line(597-xincr,185,630-xincr,170);
                line(597-xincr,215,630-xincr,227);
                line(630-xincr,170,630-xincr,227);
                line(597-xincr,200,630-xincr,200);
                line(597-xincr,192,630-xincr,187);
                line(597-xincr,207,630-xincr,213);
                line(500-xincr,190,540-xincr,150+newy);
                line(530-xincr,190,540-xincr,150+newy);
                if(xincr>=500)
                        xincr=0;
                if(newy>=82)
                        yincr=-5;
                xincr=xincr+5;
                if(newy<=0)
                        yincr=5;
                newy=newy+yincr;
                delay(50);
                cleardevice();
        }
        cleardevice();
}


FLYING KITE PROGRAM IN C

7:26 PM ANKIT




FLYING KITE SIMULATION

This is a program that depicts the working of a flying kite.It uses built in library functions of garphics.h header file.

Implementation in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
 
void main()
{
        int gd=DETECT,gm;
        int color;
        int x=10,y=1,xincr=10,yincr=10;
        int polygon[10];
 
        initgraph(&gd,&gm," ");
 
        while(!kbhit())
        {
                x += xincr;
                if(x > 200)
                        xincr = -5;
                if(x < 0 )
                        xincr = 10;
                y += yincr;
                if(y > 100)
                        yincr = -10;
                if(y < 0 )
                        yincr = 10;
                cleardevice();
                setcolor(WHITE);
                setbkcolor(BLUE);
 
                polygon[0]=100+x;
                polygon[1]=50+y;
                polygon[2]=140+x;
                polygon[3]=100+y;
                polygon[4]=100+x;
                polygon[5]=155+y;
                polygon[6]=60+x;
                polygon[7]=100+y;
                polygon[8]=100+x;
                polygon[9]=50+y;
                drawpolygon(5,polygon);
                setfillstyle(SOLID_FILL,RED);
                fillpolygon(5,polygon);
 
                setlinestyle(SOLID_LINE,1,3);
                line(100+x,155+y,100+x,180+y);
                line(100+x,155+y,110+x,180+y);
                line(100+x,155+y,90+x,180+y);
 
                setlinestyle(SOLID_LINE,1,0);
                line(0,480,100+x,90+y);
                line(100+x,90+y,130+x,100+y);
                line(100+x,90+y,70+x,100+y);
                line(100+x,90+y,100+x,70+y);
 
                delay(260);
        }
        setlinestyle(SOLID_LINE,0,0);
        fflush(stdin);
}





Tuesday, September 17, 2013

COHEN SUTHERLAND LINE CLIPPING ALGORITHM IMPLEMENTATION IN C

9:04 PM ANKIT


     




       Cohen Sutherland line clipping algorithm in c


The Cohen-Sutherland algorithm is a computer graphics algorithm used for line clipping. The algorithm divides a two-dimensional space into 9 regions (or a three-dimensional space into 27 regions), and then efficiently determines the lines and portions of lines that are visible in the center region of interest (the viewport).The Cohen–Sutherland algorithm can be used only on a rectangular clipping area.



  IMPLEMENTATION IN C


#include<stdio.h>
#include<graphics.h>
#include<conio.h>

typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{

int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}


int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;

if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;

return(code);
}


main()
{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;

clrscr();
initgraph(&gd,&gm,"e:\\tc\\bgi");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);


printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();


lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();

}

 





Monday, September 16, 2013

ANALOG CLOCK C PROGRAM

9:57 PM ANKIT
Analog clock c program

THIS IS A PROGRAM IN COMPUTER GRAPHICS THAT SIMULATES THE WORKING OF A WALL CLOCK.IT SHOWS THE TICKING OF A CLOCK USING HOURS ,MINUTES AND SECONDS HAND.


Implementation in C


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
 
// Calculates new co-ordinates of a figure after 
// rotating it at an angle about a point (cx,cy)
void rotate( int figure[], int edges, double angle, int cx, int cy )
{
 double x, y;
 angle = -1 * (angle*3.14/180);
 double cos_a = cos(angle);
 double sin_a = sin(angle);
 
 for(int i=0; i < edges; i++)
 {
  x = figure[2*i] - cx;
  y = figure[2*i+1] - cy;
  figure[2*i] = floor( (x * cos_a) - (y * sin_a) + cx + 0.5 );
  figure[2*i+1] = floor( (x * sin_a)+(y * cos_a) + cy + 0.5 );
 }
}
 
void drawClock(int,int,int);
 
void main()
{
 int second_hand[4],minute_hand[4], hour_hand[4], edges = 2 ;
 double angle;
 int cx=300, cy=200;
 int gd = DETECT, gm;
 initgraph( &gd, &gm, "" );
 int max_y = getmaxy();
 clrscr();
 cleardevice();
 
 angle = -6;
 
 // Set the initial position of the second, minute and the hour hands.
 
 second_hand[0] = cx ;
 second_hand[1] = max_y - cy;
 second_hand[2] = cx;
 second_hand[3] = max_y - 320;
 
 hour_hand[0] = cx;
 hour_hand[1] = max_y - cy;
 hour_hand[2] = cx + 90;
 hour_hand[3] = max_y - 200;
 
 minute_hand[0] = cx;
 minute_hand[1] = max_y - cy;
 minute_hand[2] = cx;
 minute_hand[3] = max_y - 310;
 
 cleardevice();
 setbkcolor(WHITE);
 
 // Draw the clock
 drawClock(cx,max_y - cy,150);
 setlinestyle(SOLID_FILL,0,1);
 
 // Draw the minute and the hour hand
 drawpoly(2,minute_hand);
 drawpoly(2,hour_hand);
 
 int i=0;
 while(!kbhit()) {
  setcolor(RED);
  drawpoly(2,second_hand);
  setcolor(GREEN);
  drawpoly(2,minute_hand);
  setcolor(BLUE);
  drawpoly(2,hour_hand);
  delay(1000);   // set delay(10) to tick the clock fast
  setcolor(15);
  drawpoly(2,second_hand);
  rotate(second_hand,edges,angle,cx,max_y - cy);
  i++;
 
  // Reset the second hand and move the minute hand 
  // when the second hand has moved 60 times.
  if(i%60 == 0) {
   second_hand[0] = cx ;
   second_hand[1] = max_y - cy;
   second_hand[2] = cx;
   second_hand[3] = max_y - 320;
   drawpoly(2,minute_hand);
   rotate(minute_hand,edges,angle,cx,max_y - cy);
  }
 
  // Move the minute hand 
  // when the second hand has moved 720 (60*12) times.
  if(i%720 == 0) {
   i = 0;
   drawpoly(2,hour_hand);
   rotate(hour_hand,edges,angle,cx,max_y - cy);
  }
 
 }
 
 getch();
}
 
// Function to draw the clock 
void drawClock(int cx, int cy, int r) {
 setcolor(GREEN);
 setlinestyle(SOLID_FILL,0,3);
 circle(cx,cy,r);
 
 int max_y = getmaxy();
 int center[2] = {cx, max_y - 340};
 
 for(int i=0; i<60; i++) {
  if(i%5 == 0) {
   circle(center[0],center[1],2);
  } else {
   circle(center[0],center[1],1);
  }
  rotate(center,1,-6,cx,cy);
 }
}


                                           



DDA Line Drawing Algorithm Program in C

7:37 PM Unknown

DDA Line Drawing Algorithm program in C

In computer graphics, a hardware or software implementation of a digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest implementation the DDA Line drawing algorithm interpolates values in interval [(xstart, ystart), (xend, yend)] by computing for each xi the equations xi = xi−1+1/m, yi = yi−1 + m, where Δx = xend − xstart and Δy = yend − ystart and m = Δy/Δx.

Implementation in C

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 
    int gd=DETECT,gm=DETECT,len,dx,dy,i,x1,y1,x2,y2;
    float xi,yi,x,y;
    clrscr();
  printf("Enter the starting point x1 & y1\n"); //Enter starting point
  scanf("%d %d",&x1,&y1);
  printf("Enter the end point x2 & y2\n"); //Enter End points
  scanf("%d %d",&x2,&y2);
    initgraph(&gd, &gm, ""); //Initialize graphics driver
    cleardevice();
    dx=x2-x1;
    dy=y2-y1;
    if(abs(dx)>abs(dy))
  {
  len=abs(dx);
  }
    else
  {
  len=abs(dy);
  }
    xi=dx/(float)len;
    yi=dy/(float)len;
    x=x1;
    y=y1;
    putpixel(x1,y1,4); //Plot points x1 and y1 on screen, 4 is color code.
    for(i=0;i<len;i++) 
 {
  x+=xi;
  y+=yi;
  putpixel(x,y,4);
    }
    getch();
}





Friday, September 13, 2013

Bresenham Line Drawing Algorithm Program C

9:53 PM Unknown

Bresenham’s Line Drawing Algorithm Program Implementation in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void swap(int &x,int &y)
{
    int k=x;
    x=y;
    y=k;
}
 
void main()
{
    int gd=DETECT,gm=DETECT,x1,x2,y1,y2,dx,dy,p,k;
    float m=0;
    clrscr();
  printf("Enter the sarting point x1 & y1\n");
  scanf("%d%d",&x1,&y1);
  printf("Enter the end point x2 & y2\n");
  scanf("%d%d",&x2,&y2);
 dx=abs(x2-x1);
    dy=abs(y2-y1);
    m=(float(y2-y1)/(x2-x1);
    initgraph(&gd,&gm,"");
    cleardevice();
    if(fabs(m)>1) 
 {
  swap(x1,y1);
  swap(x2,y2);
  swap(dx,dy);
    }
    if((x1>x2)) 
 {
  x1=x2;
  y1=y2;
    }
    p=2*dy-dx;
    for(k=0;k<abs(dx);k++) 
 {
  if(p<0)
  {
   p=p+2*dy;
  } 
  else 
  {
   if(m<0)
    y1--;
   else
    y1++;
   p=p+(2*dy)-(2*dx);
  }
  if(fabs(m)<=1)
   putpixel(x1++,y1,15);
  else
   putpixel(y1,x1++,15);
    }
    getch();
}


Wednesday, September 11, 2013

Bresenham circle drawing algorithm

8:20 PM Unknown

Bresenham's Circle Drawing Algorithm.


Bresenham's circle drawing algorithm is based on 8 way symmetry of circle. It avoids Trigonometric and Square root calculations and hence is fatser then other circle drawing algorithms.

Bresenham's Circle drawing Algorithm

1. Input radius for the Circle.
2. Initialize the decision parameter and make initial point on circumference.
d=3-2*r;
x=0;
y=r;
3. Untill (x<y), Perform the following setps
while(x<y)
{ if(d<0)
  { 
   d=d+4*x+6;
   x++;
  }
 else
  {
   d=d+4(x-y)+10;
   y--;
   x++;
  }
}
4. Plot(X,Y).
5. Determine the symmetry points in other octants.
(cx + x, cy + y);
(cx - x, cy + y);
(cx + x, cy - y);
(cx - x, cy - y);
(cx + y, cy + x);
(cx - y, cy + x);
(cx + y, cy - x);
(cx - y, cy - x);


Program to draw Polygon in C

7:34 PM Unknown

Program to draw polygon in C

As a polygon can have any no. of distinct co-ordinates (min 3). Hence, we use 2 array to store X and Y coordinates. To draw line between the user provided points we can either use any line drawing algorithm like ( DDA Line drawing algorithm or Bresenham's line drawing algorithm ) or simple function LINE( ) included in <graphics.h>

Draw polygon in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm;
 int num,i,x[10],y[10];
 clrscr();
  printf("Enter No of Co-ordinates :"); // Min 3 Co-ordinates
  scanf("%d",&num);
  
  for(i=0;i<num;i++)
  { 
   printf(" Enter Co-ordinates for %d (X,Y)",i);
   scanf("%d %d",&x[i],&y[i]);
  }
  
  initgraph(&gd,&gm,"");
  cleardevice();
  for(i=0;i<num;i++)
  {
   line(x[i],y[i],x[((i+1)%num)],y[((i+1)%num)]); //Line function from graphics.h
  }
  getch();
}
In Case of having any problem , Please leave a comment ! :)



Tuesday, September 10, 2013

Mid-Point Circle Drawing Algorithm

9:53 PM Unknown


Mid - Point Circle drawing Algorithm





Algorithm

1. Input radius (r) and center (Xc,Yc) and calculate the first point on the circumference of the circle centered on the origin as 
x=0; y=r
2. Calculate the initial value of the decision parameter as :
p=1.25-r;
3.
do
 {
  plot(x,y);
   if(p<0)
    { x=x+1;
     y=y;
     p=p+2*x+1;
    }
   else
    { x=x+1;
     y=y-1;
     p=p+2*x-2*y+1;
    }
 }
4.Determine symmetry points in other octants.
(cx + x, cy + y);
(cx - x, cy + y);
(cx + x, cy - y);
(cx - x, cy - y);
(cx + y, cy + x);
(cx - y, cy + x);
(cx + y, cy - x);
(cx - y, cy - x);
// Where (CX,CY) are co-ordinates of center of circle and (X,Y) are points to be plotted.

5.Stop

Check Implementation of this Algorithm in C .



Draw Circle using MID POINT circle drawing algorithm in C

8:50 PM Unknown

Draw Circle in C using MID-POINT circle drawing algorithm.

In Mid-Point circle drawing algorithm we set raster line drawing algorithm to calculate pixel positions around a circle path centered at the co-ordinate origin (0,0). Then each calculated position (x,y) is moved to its proper screen position by adding Xc to X and Yc to Y, Where (Xc, Yc) are the co-ordinates of actual center of the circle with radius r.

Mid-Point circle algorithm in C

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
 
void plotpoints(int x, int y, int cx, int cy)
{
    putpixel(cx + x, cy + y, 4);
    putpixel(cx - x, cy + y, 4);
    putpixel(cx + x, cy - y, 4);
    putpixel(cx - x, cy - y, 4);
    putpixel(cx + y, cy + x, 4);
    putpixel(cx - y, cy + x, 4);
    putpixel(cx + y, cy - x, 4);
    putpixel(cx - y, cy - x, 4);
}
 
void main()
{
    int cx, cy, x = 0, y, r, p;
    int gd = DETECT, gm = DETECT;
 
    clrscr();
 
    printf("Enter the center \n");
    scanf("%d%d", &cx, &cy);
    printf("Enter the radius : ");
    scanf("%d", &r);
 
    y = r;
    p = 1 - r;
 
    initgraph(&gd, &gm, "");
    cleardevice();
 
    while (x < y) {
 plotpoints(x, y, cx, cy);
 x++;
 if (p < 0)
     p += 2 * x + 1;
 else {
     y--;
     p += 2 * (x - y) + 1;
 }
    }
 
    getch();
 
}



Draw Circle Using Bresenham's Circle Drawing Algorithm in C

8:30 PM Unknown


Bresenham's method of drawing the circle avoids trigonometric and square root calculations by using only integer operations involving squares of the pixel separation distances.

It considers the eight way symmetry of the circle and plots 1/8th part of circle from 90(D) to 45(D). As circle is drawn from 90 to 45 degree , the x moves in +ve direction and y moves in -ve direction.


Bresenham's Circle drawing Program in C



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
 
void bresenhamcircle(int cx,int cy,int x,int y)
{
 putpixel(cx+x,cy+y,RED);
 putpixel(cx-x,cy+y,RED);
 putpixel(cx+x,cy-y,RED);
 putpixel(cx-x,cy-y,RED);
 putpixel(cx+y,cy+x,RED);
 putpixel(cx-y,cy+x,RED);
 putpixel(cx+y,cy-x,RED);
 putpixel(cx-y,cy-x,RED);
}
 
void main()
{
 int cx,cy,x = 0,y,r,p;
 int gd=DETECT,gm;
 clrscr();
 printf("Enter the coordinates for centre of the circle: ");
 scanf("%d %d",&cx,&cy);
 printf("Enter radius of : ");
 scanf("%d",&r);
 y=r;
 p=3-2*r;
 initgraph(&gd,&gm,"");
 cleardevice();
  while(x<y)
 {
  bresenhamcircle(cx,cy,x,y);
  x++;
  if(p<0)
   p=p+4*x+6;
  else
  {
   y--;
   p=p+4*(x-y)+10;
  }
  bresenhamcircle(cx,cy,x,y);
  delay(200);
 }
 
 getch();
}

Bresenham's Circle Drawing Alogrithm