Wednesday, December 18, 2013

Program to display a Flying Kite

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

void main()
{
    int gd=DETECT,gm;
    int color;
    int x=10,y=1,inc_x=10,inc_y=10;
    int poly[10];

    initgraph(&gd,&gm,"..\\bgi");

    while(!kbhit())
    {
        x += inc_x;
        if(x > getmaxx()-175)
            inc_x = -5;
        if(x < 0 )
            inc_x = 10;
        y += inc_y;
        if(y > 200)
            inc_y = -10;
        if(y < 0 )
            inc_y = 10;
        cleardevice();
        setcolor(WHITE);
        setbkcolor(CYAN);

        poly[0]=100+x;
        poly[1]=50+y;
        poly[2]=140+x;
        poly[3]=100+y;
        poly[4]=100+x;
        poly[5]=155+y;
        poly[6]=60+x;
        poly[7]=100+y;
        poly[8]=100+x;
        poly[9]=50+y;
        drawpoly(5,poly);
        setfillstyle(SOLID_FILL,LIGHTRED);
        fillpoly(5,poly);
        setcolor(LIGHTBLUE);
        setlinestyle(SOLID_LINE,1,3);
        line(100+x,155+y,100+x,300+y);
        line(100+x,155+y,110+x,300+y);
        line(100+x,155+y,90+x,300+y);
        setcolor(WHITE);
        setlinestyle(SOLID_LINE,1,0);
        line(100,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);
    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();
}

Monday, October 28, 2013

C graphics program to shear a rectangle

#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,option,xref,yref;
int i,maxx,maxy,x1,y1,x2,y2,x3,y3,x4,y4,gap=50;
float shx=0.0,shy=0.0;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("enter the  endpoints of the top of the rectangle (x1,y1) & (x2,y2):");
 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
 printf("enter the endpoints of bottom of the rectangle (x3,y3) & (x4,y4)");
 scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
 printf(" Enter the axis to shear\n");
 printf(" 1 - X axis Shear\n");
 printf(" 2 - Y axis shear\n");
 scanf("%d",&option );
 if(option==1)
 {
 printf("enter the value for x-axis shear( can be fraction too):");
 scanf("%f",&shx);
 }
 else
 {
 printf("enter the value for y-axis shear( can be fraction too):");
 scanf("%f",&shy);
 }

clearviewport();
maxx= getmaxx();
maxy=getmaxy();
line(3,maxy-1,maxx-5,maxy-1);
line(5,5,5,maxy-3);

for( i= 0;i<maxx-5;i=i+gap)              // code to display co-ordinates
{
outtextxy(i+3,maxy-7,"|");
itoa(i,str,10);
outtextxy(i,maxy-10,str);
}
for( i= maxy;i>0;i=i-gap)
{
outtextxy(3,i,"-");
itoa(maxy-i,str,10);
outtextxy(9,i,str);

}
 setcolor(50);                // drawing rectangle using endpoints
 line(x1,maxy-y1,x2,maxy-y2);
 line(x3,maxy-y3,x4,maxy-y4);
 line(x1,maxy-y1,x3,maxy-y3);
 line(x2,maxy-y2,x4,maxy-y4);
 outtextxy(10,10,"hit any key to see the shearing effect" );
 getch();
 setcolor(0);    // to hide the rectangle drawn
  line(x1,maxy-y1,x2,maxy-y2);
  line(x3,maxy-y3,x4,maxy-y4);
  line(x1,maxy-y1,x3,maxy-y3);
  line(x2,maxy-y2,x4,maxy-y4);

   setcolor(58);    // to redraw the rectangle
 if(option==1)
  {
  // shearing about x axis so only  points x1 and x2 need to be recomputed
  line(x1+shx*y1,maxy-y1,x2+shx*y2,maxy-y2);
  line(x3,maxy-y3,x4,maxy-y4);
  line(x1+shx*y1,maxy-y1,x3,maxy-y3);
  line(x2+shx*y2,maxy-y2,x4,maxy-y4);

 }
 else
 {
   // shearing about y axis so only  points y2 and y4 need to be recomputed
  line(x1,maxy-y1,x2,maxy-(y2+shy*x2));
  line(x3,maxy-y3,x4,maxy-(y4+shy*x4));
  line(x1,maxy-y1,x3,maxy-y3);
  line(x2,maxy-(y2+shy*x2),x4,maxy-(y4+shy*x4));

 }
 getch();
 closegraph();
}

 
        Original Image             Shearing w.r.t  Y - axis     Shearing w.r.t  X - axis   

Sample Input
    x1 ,y1, x2, y2 : 200 200 300 200
    x3, y3, x4, y4 : 200 100 300 100  
x- shear value: any value even fractional ( eg 0.4)
y- shear value: any value even fractional ( eg 0.5)

Friday, October 25, 2013

C graphics program to display smiling face

/* Computer C Graphics Program to display smiling face */



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

void main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;

   initgraph(&gd,&gm,"..\\bgi");

   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);

   setcolor(RED);
   setfillstyle(SOLID_FILL,RED);
   fillellipse(44,85,2,6);
   fillellipse(56,85,2,6);

   ellipse(50,100,205,335,20,9);
   ellipse(50,100,205,335,20,10);
   ellipse(50,100,205,335,20,11);

   area = imagesize(left, top, left + 50, top + 50);
   p = malloc(area);

   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(155,451,"Smiling Face Animation");

   setcolor(BLUE);
   rectangle(0,0,639,449);

   while(!kbhit())
   {
      temp1 = 1 + random ( 588 );
      temp2 = 1 + random ( 380 );

      getimage(left, top, left + 50, top + 50, p);
      putimage(left, top, p, XOR_PUT);
      putimage(temp1 , temp2, p, XOR_PUT);
      delay(200);
      left = temp1;
      top = temp2;
      delay(200);
   }

   getch();
   closegraph();
}


Program to display circles within circle

/* Program to display circles within circle */



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

void main()
{
   int gd = DETECT, gm, x, y, color, angle = 0;
   struct arccoordstype a, b;

   initgraph(&gd, &gm, "..\\bgi");
   delay(2000);

   while(angle<=360)

   {
   setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,50);
      setcolor(BLUE);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
      setcolor(RED);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
      getarccoords(&a);
      setcolor(GREEN);
      circle(a.xstart,a.ystart,25);
      angle = angle+5;
      delay(50);
   }

   getch();
   closegraph();
   }

Monday, October 21, 2013

C graphics program to simulate a Traffic light


/* This program will simulate a traffic light */

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

main()
{
   int gd = DETECT, gm, midx, midy;

   initgraph(&gd, &gm, "..\\bgi");

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy-10, "Traffic Light Simulation");
   outtextxy(midx, midy+10, "Press any key to start");
   getch();
   cleardevice();
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(midx, midy-50,WHITE);
   setcolor(BLUE);
   outtextxy(midx,midy-50,"STOP");
   delay(1000);
   graphdefaults();
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(midx, midy,WHITE);
   setcolor(BLUE);
   outtextxy(midx-18,midy-3,"READY");

   delay(2000);
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(midx, midy+50,WHITE);
   setcolor(BLUE);
   outtextxy(midx-7,midy+48,"GO");
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
   outtextxy(midx-150, midy+100, "Press any key to exit...");

   getch();
   closegraph();
   return 0;
}

C graphics program to simulate Solar system

 /* This program will simulate a Solar system */

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
{
int i=0,j=260,k=30,l=150,m=90;
int n=230,o=10,p=280,q=220;
float pi=3.1424,a,b,c,d,e,f,g,h,z;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
outtextxy(0,10,"SOLAR SYSTEM");
outtextxy(500,10,"press any key...");
circle(320,240,20);               /* sun */
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");
circle(260,240,8);
setfillstyle(1,2);
floodfill(258,240,15);
floodfill(262,240,15);
outtextxy(240,220,"mercury");
circle(320,300,12);
setfillstyle(1,1);
floodfill(320,298,15);
floodfill(320,302,15);
outtextxy(335,300,"venus");
circle(320,160,10);
setfillstyle(1,5);
floodfill(320,161,15);
floodfill(320,159,15);
outtextxy(332,150, "earth");
circle(453,300,11);
setfillstyle(1,6);
floodfill(445,300,15);
floodfill(448,309,15);
outtextxy(458,280,"mars");
circle(520,240,14);
setfillstyle(1,7);
floodfill(519,240,15);
floodfill(521,240,15);
outtextxy(500,257,"jupiter");
circle(169,122,12);
setfillstyle(1,12);
floodfill(159,125,15);
floodfill(175,125,15);
outtextxy(130,137,"saturn");
circle(320,420,9);
setfillstyle(1,13);
floodfill(320,417,15);
floodfill(320,423,15);
outtextxy(310,400,"urenus");
circle(40,240,9);
setfillstyle(1,10);
floodfill(38,240,15);
floodfill(42,240,15);
outtextxy(25,220,"neptune");
circle(150,420,7);
setfillstyle(1,14);
floodfill(150,419,15);
floodfill(149,422,15);
outtextxy(120,430,"pluto");
getch();
while(!kbhit())             /*animation*/
{
a=(pi/180)*i;
b=(pi/180)*j;
c=(pi/180)*k;
d=(pi/180)*l;
e=(pi/180)*m;
f=(pi/180)*n;
g=(pi/180)*o;
h=(pi/180)*p;
z=(pi/180)*q;
cleardevice();
circle(320,240,20);
setfillstyle(1,4);
floodfill(320,240,15);
outtextxy(310,237,"sun");

circle(320+60*sin(a),240-35*cos(a),8);
setfillstyle(1,2);
pieslice(320+60*sin(a),240-35*cos(a),0,360,8);
circle(320+100*sin(b),240-60*cos(b),12);
setfillstyle(1,1);
pieslice(320+100*sin(b),240-60*cos(b),0,360,12);
circle(320+130*sin(c),240-80*cos(c),10);
setfillstyle(1,5);
pieslice(320+130*sin(c),240-80*cos(c),0,360,10);
circle(320+170*sin(d),240-100*cos(d),11);
setfillstyle(1,6);
pieslice(320+170*sin(d),240-100*cos(d),0,360,11);
circle(320+200*sin(e),240-130*cos(e),14);
setfillstyle(1,7);
pieslice(320+200*sin(e),240-130*cos(e),0,360,14);
circle(320+230*sin(f),240-155*cos(f),12);
setfillstyle(1,12);
pieslice(320+230*sin(f),240-155*cos(f),0,360,12);
circle(320+260*sin(g),240-180*cos(g),9);
setfillstyle(1,13);
pieslice(320+260*sin(g),240-180*cos(g),0,360,9);
circle(320+280*sin(h),240-200*cos(h),9);
setfillstyle(1,10);
pieslice(320+280*sin(h),240-200*cos(h),0,360,9);
circle(320+300*sin(z),240-220*cos(z),7);
setfillstyle(1,14);
pieslice(320+300*sin(z),240-220*cos(z),0,360,7);
delay(20);
i++;
j++;
k++;
l++;
m++;
n++;
o++;
p++;
q+=2;
}
getch();
}

program using cohen sutherland line clipping algorithm

/* This program will clip a line using cohen sutherland line clipping algorithm */

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

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


}


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


void main()
{

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

clrscr();
initgraph(&gd,&gm,"..\\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);
clearviewport();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clearviewport();

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

}

Sample Input :

                   x1 ,y1        : 50 50
                   x2 ,y2        : 100 100
                  xwin , ymin : 75 75
                  xmax , ymax : 150 150

C graphics program for reflection about X- axis, Y axis,Y=X axis

/* This program will  reflect an object ( triangle ) about X- axis, Y- axis and Y=X axis */

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,option;
int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;
double theta;
float m;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

setcolor(255);
printf("Reflection about \n");
printf("X axis :  1\n");
printf("Y axis : 2\n");
printf("X=Y axis : 3\n");
printf(" Enter the option (1-3):");
scanf("%d",&option);
switch( option)
{
case 1:  y1=-y1; y2=-y2;y3=-y3; break;

case 2:  x1=-x1;x2=-x2;x3=-x3;break;

case 3:  y1=-y1; y2=-y2;y3=-y3;
     theta= ((double) 90 *3.14f )/(double)180;
    x1dash=x1*cos(theta)-y1*sin(theta);
    x2dash=x2*cos(theta)-y2*sin(theta);
    x3dash=x3*cos(theta)-y3*sin(theta);
    y1dash=x1*sin(theta)+y1*cos(theta);
    y2dash=x2*sin(theta)+y2*cos(theta);
    y3dash=x3*sin(theta)+y3*cos(theta);
    x1=x1dash;x2=x2dash;x3=x3dash;
    y1=y1dash;y2=y2dash;y3=y3dash;

}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

getch();
closegraph();
}

Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

Tuesday, October 8, 2013

Program using Bresenham's line drawing algorithm ( all quadrants)

/* This program will draw x- y coordinate system and will draw lines in all quadrants using   Bresenham's line drawing algorithm  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x,y,dy,dx,p,gap=50,temp;
float m;
char str[3];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of line\n");
scanf("%d",&x1);
scanf("%d",&y1);
printf("Enter second co-ords of line\n");
scanf("%d",&x2);
scanf("%d",&y2);
if (y1>y2)    // needed for -ve slope to work
{temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
dy=abs(y2-y1);
dx=abs(x2-x1);
m=(float)(dy)/(dx);
dy=abs(dy);
dx=abs(dx);
xmid= getmaxx()/2;
ymid =getmaxy()/2;
x2=x2+xmid;
y2=ymid-y2;
x=x1;y=y1;
if(m>1.0)
{
x=y1;
y=x1;
}


line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}

x=x+xmid;
y=ymid -y;
p=2*dy-dx;
while(x<x2)
{
if(p>=0)
{
putpixel(x,y,5);

y=y-1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,5);
p=p+2*dy;
}
x=x+1;
}
getch();
closegraph();
}

C graphics program to rotate an object about the origin


/* This program will  rotate an object ( triangle) about the origin */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp;
int x1dash,x2dash,x3dash,y1dash,y2dash,y3dash;
float m;
double theta;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

printf("Enter the degree to rotate");
scanf("%lf",&theta);
theta= ((float) theta *3.14f )/(float)180;
x1dash=x1*cos(theta)-y1*sin(theta);
x2dash=x2*cos(theta)-y2*sin(theta);
x3dash=x3*cos(theta)-y3*sin(theta);
y1dash=x1*sin(theta)+y1*cos(theta);
y2dash=x2*sin(theta)+y2*cos(theta);
y3dash=x3*sin(theta)+y3*cos(theta);

line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);

getch();
closegraph();
}


Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

C graphics program to rotate an object using arbitrary point

/* This program will  rotate an object ( triangle) using an arbitrary point */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,xr,yr;
int x1dash,y1dash,x2dash,y2dash,x3dash,y3dash;
float m;
double theta;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle\n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle\n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);

printf("Enter the degree to rotate");
scanf("%lf",&theta);
theta= ((float) theta *3.14f )/(float)180;  // converting theta to radian
printf("Enter the arbitrary point to rotate");
scanf("%d%d",&xr,&yr);
x1dash=xr+(x1-xr)*cos(theta)-(y1-yr)*sin(theta);
x2dash=xr+(x2-xr)*cos(theta)-(y2-yr)*sin(theta);
x3dash=xr+(x3-xr)*cos(theta)-(y3-yr)*sin(theta);

y1dash=yr+(x1-xr)*sin(theta)+(y1-yr)*cos(theta);
y2dash=yr+(x2-xr)*sin(theta)+(y2-yr)*cos(theta);
y3dash=yr+(x3-xr)*sin(theta)+(y3-yr)*cos(theta);

line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);

getch();
closegraph();
}

Tuesday, October 1, 2013

C graphics program to scale a triangle ( in all quadrants)

 /* This program will  scale an object ( triangle )   in all quadrants  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp;
float m,sxscale,syscale;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of the triangle ( eg : 50 50 ) \n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle ( eg : 100 100 )\n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle ( eg : 150 150 ) \n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
printf("\nEnter the scaling factor for x axis");
scanf("%f",&sxscale);
printf("\nEnter the scaling factor for y axis");
scanf("%f",&syscale);

setcolor(0);  // to hide drawn triangle
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
setcolor(255);

line(x1*sxscale+xmid,ymid-y1*syscale,x2*sxscale+xmid,ymid-y2*syscale);
line(x2*sxscale+xmid,ymid-y2*syscale,x3*sxscale+xmid,ymid-y3*syscale);
line(x3*sxscale+xmid,ymid-y3*syscale,x1*sxscale+xmid,ymid-y1*syscale);

getch();
closegraph();
}


Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

C graphics program to translate a triangle from one position to another in both directions (x and y ) in all quadrants

 /* This C program will  translate a triangle from one position to another in both directions (x and y ) in all quadrants and is compiled  using Turbo C++ ver 3.0*/

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,xtrans,ytrans,x,y,dy,dx,p,gap=50,temp;
float m;
char str[5];
clrscr();
initgraph(&gd,&gm,"..\\bgi");
setbkcolor(BLUE);
printf("Enter first co-ords of the triangle ( eg : 50 50 ) \n");
scanf("%d %d",&x1,&y1);
printf("Enter second co-ords of the triangle ( eg : 100 100 ) \n");
scanf("%d  %d",&x2,&y2);
printf("Enter third co-ords of the triangle ( eg : 150 50 ) \n");
scanf("%d  %d",&x3,&y3);

xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{

outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
printf("\nEnter the no of points to be moved in X and in Y direction");
scanf("%d  %d",&xtrans,&ytrans);
setcolor(0);  // setting color to black to hide drawn triangle
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
setcolor(255); // to set color back to white
line(x1+xmid+xtrans,ymid-y1-ytrans,x2+xmid+xtrans,ymid-y2-ytrans);
line(x2+xmid+xtrans,ymid-y2-ytrans,x3+xmid+xtrans,ymid-y3-ytrans);
line(x3+xmid+xtrans,ymid-y3-ytrans,x1+xmid+xtrans,ymid-y1-ytrans);

getch();
closegraph();
}



Sample Input :

  x1 ,y1   :  50   50
  x2, y2   : 100 100
  x3 ,y3   :150   50   

Midpoint Circle drawing algorithm


/* The program will generate a circle using Midpoint Circle drawing algorithm and is also known as Bresenhams Circle drawing algorithm */

 In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for drawing a circle. The Bresenham's line algorithm is derived from mid-point circle algorithm, and is known as Bresenham's circle algorithm

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"..\\bgi");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");

scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{

x=x+1;
d=d+2*x+1;
}
else
{

x=x+1;
y=y-1;
d=d+2*x-2*y+1;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
}
while(x<y);
getch();
closegraph();
}

Tuesday, September 24, 2013

DDA Line drawing algorithm ( works with all quadrants )

/* This program will draw x- y coordinate system and will draw lines in all quadrants using   DDA  line drawing algorithm  */
Input : x1 , y1 and x2,y2 points ( can input points in all quadrants )

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

#include<conio.h>

void main()
{
int gd=DETECT,gm,i,xmid,ymid,x1,x2,y1,y2,dx=0,dy=0,steps=0,k,gap=50;
float xin=0.0,yin=0.0,x=0.0f ,y=0.0f;
char str[3];

clrscr();
printf("Enter the co-ordinates");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
    steps=abs(dx) ;
    else
    steps=abs(dy);
 xin = dx /(float)steps;
 yin =dy /(float)steps   ;
initgraph(&gd,&gm,"c:..\\bgi");

xmid= getmaxx()/2;
ymid =getmaxy()/2;

line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}

x=x1+xmid;
y=ymid -y1;
putpixel((int)(x+0.5),(int)(y-0.5),7);
for(k=0;k<steps;k++)
{
x=x+xin;
y=y-yin;
putpixel((int)(x+0.5),(int) (y-0.5),7);
}
getch();
closegraph();
}

Monday, September 23, 2013

C graphics program to display BarChart accepting user input


/* BARCHART  Program . This program can accept data for 12 months . However values between 1 and 12 can also be given . */
Input : No of months and the actual data ( not in percentage ) to display


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

#define            MAX        50
#define            ARRAYMAX   3

void makegraph(float p[],int n);

void main(void)
{
  int              i,no,n;
  int              scores[50];
  float            percents[50];
  printf("\nEnter the number of months ( 0 - 12) ");
  scanf("%d", &no);

  for (i = 0; i < no; i++)
    {
    printf("\nEnter score between 0 and %d:  ", MAX);
    scanf("%d", &scores[i]);
    if(scores[i]>50 || scores[i]<0)
    {
    i--;
    continue;
    }
    }
  for (i = 0; i < no; i++)
    percents[i] = ((float) scores[i]) / MAX;

  printf("\n\n\n\tSCORE\tPERCENT");
  for (i = 0; i < no; i++)
    printf("\n%d. \t%d\t%3.0f", i + 1, scores[i], (percents[i] * 100));
  getch();
  makegraph(percents,no);
}

void makegraph(float p[],int n)
{
  int              g_driver, g_mode;
  int              i, left, top, wide, bottom, deep;
  char str[5];
  char month[][20]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  detectgraph(&g_driver, &g_mode);
  initgraph(&g_driver, &g_mode, "..\\bgi");
  wide = (int)((getmaxx()) / ((n * 2 ) + 1));
  bottom = getmaxy() - 20;
  deep = (int) (wide / 4);
  left = wide;
  line(0,bottom,getmaxx(),bottom);
  for (i = 0; i < n; i++)
    {
    top = (bottom) - ((int)(p[i] * 300));
    setfillstyle(XHATCH_FILL,i+1);
    bar3d(left, top, (left + wide), bottom, deep,1);
    itoa((int)(p[i]*100),str,10);
    outtextxy((left+(left+wide))/2,top-15, str);
    outtextxy((left+(left+wide))/2-10,bottom+5,month[i]);
    left += (wide * 2);
    }
  getch();
  closegraph();
  return;
}

C Graphics Program List


 Click on the link to redirect to the program page. It opens in a new window.

      C Graphics program List
  1. Program to create Pie Chart.
  2. Program to create Bar Chart.
  3. Program to display Circles in Circle.
  4. Program to create smiling face.
  5. Program to create National Flag.
  6. Program to create Solar System.
  7. Program to create an analog clock
  8. Program to create a digital clock
  9. Program to animate a Fan.
  10. Program to animate a Flying Kite
  11. Program to animate a Traffic light
  12. Program to draw a line using DDA algorithm.
  13. Program to draw a line using Bresenham's line drawing algorithm
  14. Program to draw a circle using Bresenham's circle drawing algorithm
  15. Program to generate a Character.
  16. Program to clip a line using Cohen Sutherland line clipping algorithm
  17. Program to translate an object with respect to origin.
  18. Program to rotate an object with respect to origin.
  19. Program to scale an object with respect to origin.
  20. Program to rotate an object with respect to arbitrary point.
  21. Program to reflect an object with respect to X axis, Y axis and X=Y axis.
  22. Program to shear a rectangle.
  23. Program to fill a rectangle using Floodfill algorithm.
  24. Program to draw a curve using Bézier algorithm
  25. Program to animate a Butterfly.




Sunday, September 22, 2013

Program to display pie chart accepting user input

/* This program displays a pie chart. This Program slices the pie as per the input given by the user. Make sure the total percentage do not exceed 100 % */
/* The program was compiled using Turbo C++ ver 3.0 */

Input :    Number of slices and % of each slice.

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
   int gd = DETECT, gm, midx, midy,i,dx,dy,value[20],n,degree=0;
   long start,end;

   char str[5];
   clrscr();
   printf("Enter the no of slices");
   scanf("%d",&n);

   initgraph(&gd, &gm, "..\\bgi");

   setcolor(MAGENTA);
   rectangle(0,40,639,450);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   setcolor(WHITE);
   outtextxy(275,10,"Pie Chart");

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   start = 0;
   end=0;
   value[0]=0;
   printf("Enter the percentage");
   for( i=1;i<=n;i++)
  {
   scanf("%d",&value[i]);
  degree= degree +((long)value[i] * 360)/100;
  if ( degree >360)
  {
    printf("Exceded 100 % ..Program terminating.. press any key to continue");
   getch();
   exit(0);
}
   for(i=0;i<n;i++)
   {

  end = start +(long)(value[i+1]*360)/100;
   setfillstyle(i+1,i+1);
   pieslice(midx, midy, start,end, 100);
   itoa(value[i+1],str,10);
 
   dx= midx+100*cos(((double)(start+end)/2)*(3.14f/180));
   dy= midy+100*sin((-1*(double)(start+end)/2)*(3.14f/180));
  outtextxy(dx, dy , str);
   start= start+((long)value[i+1]*360)/100;
  }
  getch();
closegraph();
   return 0;
}