Showing posts with label graphics program. Show all posts
Showing posts with label graphics program. Show all posts

Wednesday, March 12, 2014

Butterfly animation in C graphics

 #include<graphics.h>
#include<conio.h>
#include<stdlib.h>
char *buff1,*buff2;
void main()
{
    int gd=DETECT,gm;
    void *buff;
    int area1,area2,x,y,inc_x=10,inc_y=10;

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

    //  structure of butterfly
     ellipse(240,80,115,65,10,40);//body

     circle(240,40,10);   // face
     circle(240,30,2);    // lips
     circle(235,37,2);   // left eye
     circle(245,37,2);   // right eye

     ellipse(215,40,15,100,20,40);    //antenna
     ellipse(265,40,85,165,20,40);    //antenna

     ellipse(160,90,270,0,80,25);   //left wing
     ellipse(160,60,0,90,80,25);

     ellipse(160,75,270,90,15,40);   // wing border
     ellipse(320,75,90,270,15,40);   //wing border

     ellipse(320,90,180,270,80,25); //  right wing
     ellipse(320,60,90,180,80,25);

     setfillstyle(SOLID_FILL,YELLOW);
     floodfill(240,40,WHITE);
     setfillstyle(SOLID_FILL,BLUE);
     floodfill(200,80,WHITE);
     floodfill(290,60,WHITE);
     setfillstyle(SOLID_FILL,RED);
     floodfill(240,80,WHITE);
     setfillstyle(SOLID_FILL,GREEN);
     floodfill(240,110,WHITE);
     //Store first image action
     area1=imagesize(160,0,320,120);
     buff1=malloc(area1);

     getimage(160,0,320,120,buff1);
     clearviewport();
     // second image creation part
     ellipse(240,80,120,60,10,40);

     circle(240,40,10);
     circle(240,30,2);
     circle(235,37,2);
     circle(245,37,2);

     ellipse(215,40,15,100,20,30);
     ellipse(265,40,85,165,20,30);

     ellipse(320,90,180,260,80,40);
     ellipse(320,60,100,180,80,40);
     ellipse(310,75,115,245,15,60);

     ellipse(160,90,280,0,80,40); //  left
     ellipse(160,60,0,80,80,40);
     ellipse(170,75,295,65,15,60);
     setfillstyle(SOLID_FILL,YELLOW);
     floodfill(240,40,WHITE);
     setfillstyle(SOLID_FILL,BLUE);
     floodfill(200,80,WHITE);
     floodfill(290,60,WHITE);
     setfillstyle(SOLID_FILL,RED);
     floodfill(240,80,WHITE);
     setfillstyle(SOLID_FILL,GREEN);
     floodfill(240,110,WHITE);

     //Store second image    (WINGS BEHIND)
     area2=imagesize(160,0,320,140);
     buff2=malloc(area2);
     getimage(160,0,320,140,buff2);
     clearviewport();

     x=10;y=1;
     while(!kbhit())
     {
    x += inc_x;
        if(x > getmaxx()-175)
            inc_x = -5;
        if(x < 10 )
            inc_x = 10;
        y += inc_y;
        if(y > getmaxy()-135)
            inc_y = -10;
        if(y < 10 )
            inc_y = 10;
       putimage(x,y,buff1,COPY_PUT);
       delay(500);
       clearviewport();
       putimage(x,y,buff2,COPY_PUT);
       delay(500);
       clearviewport();
      }
      getch();
      closegraph();
}

Tuesday, January 21, 2014

Animated Fan Rotation

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd= DETECT, gm,i,m;
initgraph(&gd,&gm,"..\\bgi");
while(kbhit()==0)
{
for(i=80;i>=0;i-=20)
{
setcolor(BLUE);
circle(320,240,65);
circle(320,240,67);
setfillstyle(SOLID_FILL,BLUE);
bar(310,307,330,360);
bar(290,360,350,375);
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
pieslice(320,240,i,i+40,60);
pieslice(320,240,i+120,i+160,60);
pieslice(320,240,i+240,i+280,60);
setcolor(BLUE);
circle(320,240,10);
setfillstyle(SOLID_FILL,BLUE);
floodfill(320,240,BLUE);
delay(100);
setcolor(0);
setfillstyle(SOLID_FILL,0);
pieslice(320,240,i,i+40,60);
pieslice(320,240,i+120,i+160,60);
pieslice(320,240,i+240,i+280,60);
setcolor(BLUE);
circle(320,240,10);
setfillstyle(SOLID_FILL,BLACK);
floodfill(320,240,BLUE);
}
}
closegraph();
}

Saturday, December 14, 2013

Bézier curve fitting program


Bézier curves can be defined for any degree n. A recursive definition for the Bézier curve of degree n expresses it as a point-to-point linear combination (linear interpolation) of a pair of corresponding points in two Bézier curves of degree n − 1.


Let \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_n} denote the Bézier curve determined by any selection of points P0, P1, ..., Pn. Then to start,
\mathbf{B}_{\mathbf{P}_0}(t) = \mathbf{P}_0 \text{, and}
\mathbf{B}(t) = \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_n}(t) = (1-t)\mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_{n-1}}(t) + t\mathbf{B}_{\mathbf{P}_1\mathbf{P}_2\ldots\mathbf{P}_n}(t)




 The formula can be expressed explicitly as follows:
\begin{align}  \mathbf{B}(t) = {} &\sum_{i=0}^n {n\choose i}(1 - t)^{n - i}t^i\mathbf{P}_i \\                = {} &(1 - t)^n\mathbf{P}_0 + {n\choose 1}(1 - t)^{n - 1}t\mathbf{P}_1 + \cdots \\                  {} &\cdots + {n\choose n - 1}(1 - t)t^{n - 1}\mathbf{P}_{n - 1} + t^n\mathbf{P}_n,\quad t \in [0,1]\end{align}








 where \scriptstyle {n \choose i} are the binomial coefficients.
For example, for n = 5:

\begin{align}
  \mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\mathbf{P}_2\mathbf{P}_3\mathbf{P}_4\mathbf{P}_5}(t) = \mathbf{B}(t)
    = {} & (1 - t)^5\mathbf{P}_0 + 5t(1 - t)^4\mathbf{P}_1 + 10t^2(1 - t)^3 \mathbf{P}_2 \\
    {} & + 10t^3 (1-t)^2 \mathbf{P}_3 + 5t^4(1-t) \mathbf{P}_4 + t^5 \mathbf{P}_5,\quad t \in [0,1]
\end{align}  


Program to Create Bezier curve

This program  use only  simple array operations to compute polynomial coefficients.
Can be done using factorial too .

 #include <stdio.h>
#include <graphics.h>
#include <math.h>
void bezier (int x1[], int y1[], int no_ctrlpt)
{
   int i,j,row,col;
   double t,xt=0,yt=0; // xt , yt - points to plot curve
   int pcoeff[20][20];  // used to save the coefficents of the polynomial
                                 // created using pascal triangle
// code to find the coefficient of the polynomial
    for(i=0;i<no_ctrlpt;i++)   // no_ctrlpt - number of control points .
                                         // one point is a set of x, y coordinate
    {
        for(j=0;j<=i;j++)
        {
        if(j==0||i==j)
        {
        pcoeff[i][j]=1;
        }
        else
        {
        pcoeff[i][j]=pcoeff[i-1][j-1]+pcoeff[i-1][j];
        }
        }

    }
// code to compute the blend and to fit the curve
        for (t = 0.0; t < 1.0; t += 0.005)

          {
          int k, n= no_ctrlpt-1;
          double blend, term1,term2;

          xt=0.0;
          yt=0.0;
          for(k=0;k<no_ctrlpt;k++)
          {
          if(k==0)   // check needed since if k=0 then pow (t,k) will return domain error
          term1=1; //since anything raise to zero is 1
          else
          term1=pow(t,k);
          term2=pow( 1-t, n-k);
          blend = (double)pcoeff[no_ctrlpt-1][k]*term1*term2; // no_ctrlpt - 1 need since
                                              //only last row  of the generated pascal triangle is needed
                                        
          xt=xt+x1[k]*blend;
          yt=yt+y1[k]*blend;
          }

    putpixel ((int)xt,(int)yt, RED);
  }
    for (i=0; i<no_ctrlpt; i++)
    putpixel (x1[i], y1[i], YELLOW);
   }

 void main()
 {
   int gd = DETECT, gm;
   int x[20], y[20]; // used to store x, y coordinate system
   int i,n;
   initgraph (&gd, &gm, "..\\bgi");
   setbkcolor(BLUE);
   printf("Enter the number of control points");
   scanf("%d",&n);
   printf ("Enter the x- and y-coordinates of the  control points.\n");
   for (i=0; i<n; i++)
   scanf ("%d%d", &x[i], &y[i]);
   bezier (x, y,n);    // calling  function to fit the curve
   getch();
   closegraph();
 }

The above program modified to draw the x and y axis and to draw the Bézier curve is listed below :

#include <stdio.h>
#include <graphics.h>
#include <math.h>
void bezier (int x1[], int y1[], int no_ctrlpt)
{
   int i,j,row,col;
   double t,xt=0,yt=0;
   int pcoeff[20][20];


    for(i=0;i<no_ctrlpt;i++)
    {
        for(j=0;j<=i;j++)
        {
        if(j==0||i==j)
        {
        pcoeff[i][j]=1;
        }
        else
        {
        pcoeff[i][j]=pcoeff[i-1][j-1]+pcoeff[i-1][j];
        }
        }

    }

        for (t = 0.0; t < 1.0; t += 0.005)

          {
          int k, n= no_ctrlpt-1;
          double blend, term1,term2;

          xt=0.0;
          yt=0.0;
          for(k=0;k<no_ctrlpt;k++)
          {
          if(k==0)
          term1=1; //since anything raise to zero is 1
          else
          term1=pow(t,k);
          term2=pow( 1-t, n-k);
          blend = (double)pcoeff[no_ctrlpt-1][k]*term1*term2;
          xt=xt+x1[k]*blend;
          yt=yt+y1[k]*blend;
          }

    putpixel ((int)xt,(int)yt, RED);
  }
    for (i=0; i<no_ctrlpt; i++)
    putpixel (x1[i], y1[i], YELLOW);
   }

 void main()
 {
   int gd = DETECT, gm;
   int x[20], y[20],gap=50;
   char str[5];
   int i,n;
   initgraph (&gd, &gm, "..\\bgi");
   setbkcolor(BLUE);
   line(5,getmaxy()-10,getmaxx()-5,getmaxy()-10);
   line(3,8,3,getmaxy()-8);

   for( i= gap;i<getmaxx();i=i+gap)    // gap required for spacing of co-ordinate values
   {
   outtextxy(i,getmaxy()-14,"|");
   itoa(i,str,10);
   outtextxy(i,getmaxy()-8,str);
   }
   for( i=gap;i<getmaxy();i=i+gap)
   {
   outtextxy(1,getmaxy()-i,"-");
   itoa(i,str,10);
   outtextxy(8,getmaxy()-i,str);
   }

   printf("Enter the number of control points");
   scanf("%d",&n);
   printf ("Enter the x- and y-coordinates of the four control points.\n");
   printf(" eg: if 3 control point then enter 50 50 100 100 150 50 \n");
   for (i=0; i<n; i++)
   {
   scanf ("%d%d", &x[i], &y[i]);
   y[i]= getmaxy()-y[i]; // this step required for shifting (0,0) position from top left to bottom left
   }
   bezier (x, y,n);
   getch();
   closegraph();
 }


Sample Input :
           if 3 control points : 200 200 250 150 300 200
           if 4 control points : 200 200 250 150 300 200 350 250



Saturday, December 7, 2013

C graphics program to fill a rectangle using 4 -connected floodfill algorithm

#include <graphics.h>
#include <conio.h>
void flood_fill4(int x,int y,int newColor,int oldColor)
{
int c;
c=getpixel(x,y);
if(c==oldColor)
{
setcolor(newColor);
putpixel (x,y,newColor);
delay(10);
flood_fill4(x+1,y,newColor,oldColor);
flood_fill4(x,y+1,newColor,oldColor);
flood_fill4(x-1,y,newColor,oldColor);
flood_fill4(x,y-1,newColor,oldColor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
rectangle(250,200,300,250);
flood_fill4(251,201,4,0); // 4(red) - newColor  0(black) - oldColor
          // x y point should be one greater than rectangle border
getch();
closegraph();
}