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