Saturday, February 05, 2011

Header Files


/*
* THIS IS A HEADER FILE CALLED “COMPGRAP.H”, CREATED FOR CONVENIENCE.
* SOME COMMON FUNCTIONS THAT NEED TO BE CALLED IN COMPUTER GRAPHICS PROGRAMS,  IS WRITTEN HERE.
*/


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

 /* declaring some global variables */
 struct neworigin {
  int x;               /* this will contain the abscissa of the new origin after axes shift */
  int y;               /* this will contain the ordinate of the new origin after axes shift */
 }origin;

/* the following variables are attributes of the grid i.e. to be decided by the user */
 int pixels_in_a_row ;
 int pixels_in_a_col ;
 int pixel_width ;

/* function to make the necessary initializations */

void initialize(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)
   /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
   /* terminate with an error code */
      exit(1);
   }

   printf("\n Enter number of pixels along a row: ");
   scanf("%d",&pixels_in_a_row);
   printf("\n Enter number of pixels along a column: ");
   scanf("%d",&pixels_in_a_col);
   printf("\n Enter size of a pixel: ");
   scanf("%d",&pixel_width);

   /* to check that the representation does not go beyond the screen size */
   if(pixels_in_a_row*pixel_width>=getmaxx() || pixels_in_a_col*pixel_width>=getmaxy())
   {
    perror("Representation not possible!");
    exit(0);
   }

   /* shifting the co-ordinate axes from top-left corner to centre of the computer screen */
   origin.x = getmaxx()/2 ;
   origin.y = getmaxy()/2 ;

   /* clearing screen */
      clrscr() ;
   /* clearing device for drawing the grid */
      cleardevice();
}

/*Following is a function to create a grid of pixels, where each pixel is equivalent to a square whose width is provided by the user, creategrid function takes as argument two colors, first one for drawing grid, other for drawing the X-Y axes */

void creategrid(int gridcolor, int axescolor)
{
 int oldcolor = getcolor() ;       /* oldcolor is the color that was set by the user before the function call */

 /* for odd number of pixels along a row, negative X-axes will contain one more pixel than positive X-axes */
 int pixels_along_positiveX = pixels_in_a_row/2 ;
 int pixels_along_negativeX = pixels_in_a_row - pixels_along_positiveX ;

 /* for odd number of pixels along a row, negative Y-axes will contain one more pixel than positive Y-axes */
 int pixels_along_positiveY = pixels_in_a_col/2 ;
 int pixels_along_negativeY = pixels_in_a_col - pixels_along_positiveY ;

 int row_length = pixels_in_a_row*pixel_width ;  /* this gives the length of the row in the grid */
 int col_length = pixels_in_a_col*pixel_width ;  /* this gives the length of the column in the grid */

 /* (LTcornerX, LTcornerY) is the extreme left-top corner of the pixel grid,
      not necessarily the top-left corner of screen */
 int LTcornerX = origin.x - pixels_along_negativeX*pixel_width ;
 int LTcornerY = origin.y - pixels_along_positiveY*pixel_width ;
 /* (RBcornerX, RBcornerY) is the extreme right-bottom corner of the pixel grid,
      not necessarily the right-bottom corner of screen */
 int RBcornerX = origin.x + pixels_along_positiveX*pixel_width ;
 int RBcornerY = origin.y + pixels_along_negativeY*pixel_width ;

 register int i;    /* iterating variable for loop */
 setcolor(gridcolor) ;    /* setting color to draw the grid */

 /* this will create a set of vertical lines for the grid */
 for(i=0; i<=row_length; i+=pixel_width)
  line(LTcornerX+i, LTcornerY, LTcornerX+i, RBcornerY) ;


 /* this will create a set of horizontal lines for the grid */
 for(i=0; i<=col_length; i+=pixel_width)
    line(LTcornerX, LTcornerY+i, RBcornerX, LTcornerY+i) ;

  setcolor(axescolor) ;              /* setting color to redraw the X-Y axes */

  line(origin.x, LTcornerY, origin.x, RBcornerY) ;
  line(LTcornerX, origin.y, RBcornerX, origin.y) ;

  setcolor(oldcolor) ;                /* retrieving the old color that was being used before the function call */

 //getch();
}

/* The following function will draw a line segment from (x1, y1) to (x2, y2) in the new coordinate axes system
   Color to draw the straight line must be set by programmer before function call, this is done to keep analogy to   
   the line drawing function in C */

void drawline(int x1, int y1, int x2, int y2)
{
 /* getting equivalents of (x1,y1) and (x2,y2) in the old coordinate axes system */

 int x1_oldaxes = origin.x + x1*pixel_width;
 int x2_oldaxes = origin.x + x2*pixel_width;
 int y1_oldaxes = origin.y - y1*pixel_width;
 int y2_oldaxes = origin.y - y2*pixel_width;

 line(x1_oldaxes, y1_oldaxes, x2_oldaxes, y2_oldaxes);           /* calling the function defined in graphics library */
 getch();
}

/* The following function will draw a circle with centre (x,y) in the new coordinate axes system. Color to draw the
    circle must be set by programmer before function call, this is done to keep analogy to the circle drawing function
    in C */

void drawcircle(int x, int y, int r)
{
 /* getting equivalents of (x,y) in the old coordinate axes system */

 int x_oldaxes = origin.x + x*pixel_width;
 int y_oldaxes = origin.y - y*pixel_width;
 circle(x_oldaxes, y_oldaxes,r*pixel_width) ;   /* calling the function defined in graphics library */
 getch();
}

/* Following function draws an ellipse in the new coordinate system keeping and analogy to the ellipse
    drawing function in C */

void drawellipse(int xcenter, int ycenter, int xaxis, int yaxis)
{
 /* getting equivalents of (x,y) in the old coordinate axes system */

 int x_oldaxes = origin.x + xcenter*pixel_width;
 int y_oldaxes = origin.y - ycenter*pixel_width;
/* calling the function defined in graphics library */
 ellipse(x_oldaxes, y_oldaxes, 0, 360, xaxis*pixel_width, yaxis*pixel_width) ;  
getch();
}

/* The following function will shade the pixel whose left-bottom corner's co-ordinate is (x,y).
   The pattern and the color used in the shading has to be set by the programmer before calling the function */

void plot(int x, int y)
{
 int pixel[8];

 /* left-bottom corner */
 pixel[0] = origin.x + x*pixel_width ;
 pixel[1] = origin.y - y*pixel_width ;

 /* right-bottom corner */
 pixel[2] = origin.x + (x+1)*pixel_width;
 pixel[3] = origin.y - y*pixel_width ;

 /* right-top corner */
 pixel[4] = origin.x + (x+1)*pixel_width;
 pixel[5] = origin.y - (y+1)*pixel_width;

/*left-top corner. fillpoly automatically closes the polygon.*/

 pixel[6] = origin.x + x*pixel_width ;
 pixel[7] = origin.y - (y+1)*pixel_width ;

   /* draw a filled polygon */
   fillpoly(4, pixel);
}

/*
* THIS IS A HEADER FILE CALLED “FILL.H”, CREATED FOR CONVENIENCE.
* SOME COMMON FUNCTIONS THAT NEED TO BE CALLED IN COMPUTER GRAPHICS PROGRAMS,  DEMONSTRATING POLYGON FILL ALGORITHMS ARE WRITTEN HERE.
*/

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

 /* declaring some global variables */
 struct neworigin {
  int x;               /* this will contain the abscissa of the new origin after axes shift */
  int y;               /* this will contain the ordinate of the new origin after axes shift */
 }origin;
 
 /* representing a two-dimensional point */
 typedef struct pointin2d {
            int x,y;
 } point ;

/* the following variables are attributes of the grid i.e. to be decided by the user */
 int pixels_in_a_row = 30;
 int pixels_in_a_col = 22;
 int pixel_width = 20;

 /* Before output, pixels should be first set in the frame buffer.
    The dimension of frame buffer is same as that of the grid */         
 short int framebuffer[30][22];

/* function to make the necessary initializations */
void initialize(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)
   /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
   /* terminate with an error code */
      exit(1);
   }
   /* shifting the co-ordinate axes from top-left corner to centre of the computer screen */
   origin.x = getmaxx()/2 ;
   origin.y = getmaxy()/2 ;
   /* clearing device for drawing the grid */
      cleardevice();
}

/* function to create a grid of pixels, where each pixel is equivalent to a square whose width is known*/
/* creategrid function takes as argument three colors, first one for drawing grid, second for drawing the X-Y axes
   and third for drawing the half-interval scan lines */

void creategrid(int gridcolor, int axescolor, int scancolor)
{
 int oldcolor = getcolor() ;       /* oldcolor is the color that was set by the user before the function call */

 /* for odd number of pixels along a row, negative X-axes will contain one more pixel than positive X-axes */
 int pixels_along_positiveX = pixels_in_a_row/2 ;
 int pixels_along_negativeX = pixels_in_a_row - pixels_along_positiveX ;

 /* for odd number of pixels along a row, negative Y-axes will contain one more pixel than positive Y-axes */
 int pixels_along_positiveY = pixels_in_a_col/2 ;
 int pixels_along_negativeY = pixels_in_a_col - pixels_along_positiveY ;

 int row_length = pixels_in_a_row*pixel_width ;  /* this gives the length of the row in the grid */
 int col_length = pixels_in_a_col*pixel_width ;  /* this gives the length of the column in the grid */

 /* (LTcornerX, LTcornerY) is the extreme left-top corner of the pixel grid, not necessarily the top-left corner of screen */
 int LTcornerX = origin.x - pixels_along_negativeX*pixel_width ;
 int LTcornerY = origin.y - pixels_along_positiveY*pixel_width ;

 /* (RBcornerX, RBcornerY) is the extreme right-bottom corner of the pixel grid, not necessarily the right-bottom corner of screen */
 int RBcornerX = origin.x + pixels_along_positiveX*pixel_width ;
 int RBcornerY = origin.y + pixels_along_negativeY*pixel_width ;

 register int i;    /* iterating variable for loop */
 setcolor(gridcolor) ;    /* setting color to draw the grid */

 /* this will create a set of vertical lines for the grid */
 for(i=0; i<=row_length; i+=pixel_width)
  line(LTcornerX+i, LTcornerY, LTcornerX+i, RBcornerY) ;

 /* this will create a set of horizontal lines for the grid */
 for(i=0; i<=col_length; i+=pixel_width)
    line(LTcornerX, LTcornerY+i, RBcornerX, LTcornerY+i) ;

  setcolor(axescolor) ;              /* setting color to redraw the X-Y axes */

  line(origin.x, LTcornerY, origin.x, RBcornerY) ;
  line(LTcornerX, origin.y, RBcornerX, origin.y) ;
 
  LTcornerY = origin.y - pixels_along_positiveY*pixel_width + pixel_width/2 ;
 
  setcolor(scancolor) ; /* setting color to draw the half-scan lines */

 /* this will create a set of horizontal half-scan lines for the grid */
 for(i=0; i<=col_length-pixel_width; i+=pixel_width)
    line(LTcornerX, LTcornerY+i, RBcornerX, LTcornerY+i) ;

  setcolor(oldcolor) ;                /* retrieving the old color that was being used before the function call */
  getch();                                                         /* wait till a key is pressed */
}

/* The following function will draw a line segment from (x1, y1) to (x2, y2) in the new coordinate axes system
   Color to draw the straight line must be set by programmer before function call, this is done
   to keep analogy to the line drawing function in C */

void drawline(float x1, float y1, float x2, float y2)
{
 /* getting equivalents of (x1,y1) and (x2,y2) in the old coordinate axes system */
 int x1_oldaxes = origin.x + x1*pixel_width;
 int x2_oldaxes = origin.x + x2*pixel_width;
 int y1_oldaxes = origin.y - y1*pixel_width;
 int y2_oldaxes = origin.y - y2*pixel_width;
setcolor(WHITE);
 line(x1_oldaxes, y1_oldaxes, x2_oldaxes, y2_oldaxes);           /* calling the function defined in graphics library */
}

/* The following function will shade the pixel whose left-bottom corner's co-ordinate is (x,y).
   The pattern and the color used in the shading has to be set by the programmer before calling the function */
void plot(int x, int y)
{
 int pixel[8];

 /* left-bottom corner */
 pixel[0] = origin.x + x*pixel_width ;
 pixel[1] = origin.y - y*pixel_width ;
 /* right-bottom corner */
 pixel[2] = origin.x + (x+1)*pixel_width;
 pixel[3] = origin.y - y*pixel_width ;
 /* right-top corner */
 pixel[4] = origin.x + (x+1)*pixel_width;
 pixel[5] = origin.y - (y+1)*pixel_width;
/*
   left-top corner. fillpoly automatically
   closes the polygon.
*/
 pixel[6] = origin.x + x*pixel_width ;
 pixel[7] = origin.y - (y+1)*pixel_width ;
   /* draw a filled polygon */
   fillpoly(4, pixel);
}

/* The following function switches ON a pixel */
void pixelon(int x,int y)
{
            setcolor(WHITE);                                                           /* highlight color         */
            setfillstyle(SOLID_FILL,WHITE);
            plot(x,y);
}

/* The following function switches OFF a pixel */
void pixeloff(int x,int y)
{
            setcolor(DARKGRAY);                                       /* color of grid */
            setfillstyle(SOLID_FILL,BLACK);
            plot(x,y);
            setcolor(LIGHTGRAY);                                       /* scan line color */
            drawline(x,y+0.5,x+1,y+0.5);
}

/* The following function activates a pixel in a frame buffer */
void complementpix_in_fb(int x,int y)
{
            int xhash, yhash;
            xhash = pixels_in_a_row - pixels_in_a_row/2 + x;
            yhash = pixels_in_a_col - pixels_in_a_col/2 + y;          
                        framebuffer[xhash][yhash]++ ;
}

/* Function to draw a polygon */
void drawpolygon(point polygon[], int vertices)
{  
            int i;
            for(i=0;i<vertices;i++)
                        drawline(polygon[i].x,polygon[i].y,polygon[(i+1)%vertices].x,polygon[(i+1)%vertices].y);
}

/* The following function paints on the screen according to the current status of the frame buffer */
void showoutput(point poly[], int size)
{
            int x,y, i,j;
           
            for(i=0;i<pixels_in_a_row;i++)
            for(j=0;j<pixels_in_a_col;j++)
                        if(framebuffer[i][j]%2==1)                     {
                                    x = i + pixels_in_a_row/2 - pixels_in_a_row;
                                    y = j + pixels_in_a_col/2 - pixels_in_a_col;
                                    pixelon(x,y);
                        }
                        else if(framebuffer[i][j]>0)                    {
                                    x = i + pixels_in_a_row/2 - pixels_in_a_row;
                                    y = j + pixels_in_a_col/2 - pixels_in_a_col;
                                    pixeloff(x,y);
                        }
            drawpolygon(poly,size);
}

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.