Friday, 10 August 2018

Study of various graphics functions in C language and use them to create any graphics object.


In C graphics programming you have to use standard library functions , every graphics program should include "graphics.h" header file.
             #include<graphics.h>
Graphics Mode Initialization :- In a C program, first step is to initialize the graphics drivers on the computer. This is done using the initgraph method provided in graphics.h library The initgraph function that will initialize the graphics mode on the computer. initigraph has the following prototype.
void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
            int gd=DETECT,gm;
            initgraph(&gd,&gm,"f:\\tc\\bgi");

initgraph:- initializes the graphics system by loading the graphics driver from disk (or validating a REGISTERED  driver) then putting the system into graphics mode. initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.
graphics_drivers constant
Numeric value
DETECT
0 (requests autodetect)
CGA
1
MCGA
2
EGA
3
EGA64
4
EGAMONO
5
IBM8514
6
HERCMONO
7
ATT400
8
VGA
9
PC3270
10
*graphdriver:- the integer that specifies the graphics driver to be used. We can give graphdriver a value using a constant of the graphics_drivers enumeration type which is listed in graphics.h. Normally we use value as “0” (requests auto-detect). Other values are 1 to 10 and description of each enumeration type is listed here.

*graphmode:- the integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver.
graphdriver and graphmode must be set to valid values from the following tables, or you will get unpredictable results. The exception is graphdriver = DETECT.

*pathtodriver:- inorder to specify the directory path where initgraph looks for graphics drivers (*.BGI) first. 
(1) If they’re not there, initgraph looks in the current directory.
(2) If pathtodriver is null, the driver files must be in the current directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or we will get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode. We can tell initgraph to use a particular graphics driver and mode, or to auto detect the attached video adapter at run time and pick the corresponding driver. If we tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.
Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGI file from disk.  This dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file.

closegraph:- deallocates all memory allocated by the graphics system, then restores the screen to the mode it was in before you called initgraph. (The graphics system deallocates memory, such as the drivers, fonts, and an internal buffer, through a call to _graphfreemem.)

Below are mentioned various graphics functions with description, syntax and sample code of same:
1.
getmaxx function
Description: -
getmaxx function returns the maximum X coordinate for current graphics mode and driver.
Declaration: -
int getmaxx();
Program :-

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
   int gd = DETECT, gm, max_x;
   char array[100];
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   max_x = getmaxx();
   printf(array, "Maximum X coordinate for current graphics mode and driver
   = %d.",max_x);
   outtext(array);
   getch();
}

2.
getmaxy function
Description: -
getmaxy function returns the maximum Y coordinate for current graphics mode and driver.
Declaration: -
int getmaxy();
Program :-

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
   int gd = DETECT, gm, max_y;
   char array[100];
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   max_y = getmaxy();
   printf(array, "Maximum Y coordinate for current graphics mode & driver is  
   =  %d.",max_y);
   outtext(array);
   getch();
   }
 

3.

setcolor function

Description: -

In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly speaking number of available colors depends on current graphics mode and driver.
For Example: - BLACK is assigned 0, RED is assigned 4 etc.
setcolor function is used to change the current drawing color.            
e.g. setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE.

Declaration: -

void setcolor(int color);

 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main ()
{
   int gd = DETECT, gm;
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   circle (100,100,50);          
   setcolor(RED);
   circle(200,200,50);
   getch();
   closegraph();
}

4.

getcolor function

Description: -

getcolor function returns the current drawing color.

E.g. a = getcolor(); // a is an integer variable if current drawing color is WHITE then a will be 15.

Declaration:-

int getcolor();


#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm, drawing_color;
   char a[100]; 
   initgraph(&gd,&gm,"C:\\TC\\BGI"); 
   drawing_color = getcolor(); 
   printf(a,"Current drawing color = %d", drawing_color);
   outtextxy( 10, 10, a ); 
   getch();   
}
 
5.

outtextxy function

Description: -
outtextxy function display text or string at a specified 
point(x,y) on the screen.
Declaration:-
void outtextxy(int x, int y, char *string);
x, y are coordinates of the point and third argument contains 

the address of string to be displayed.
 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd,&gm,"C:\\TC\\BGI"); 
   outtextxy(100, 100, "WELCOME- TO COMPUTER GRAPHICS 
   LAB"); 
   getch();
}
 
6.

getpixel function

Description: -
int getpixel(int x, int y);
Declaration: -
getpixel function returns the color of pixel present at location(x, y).
 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
  int gd = DETECT, gm, color;
  char array[50]; 
  initgraph(&gd,&gm,"C:\\TC\\BGI"); 
  color = getpixel(0, 0); 
  printf(array,"color of pixel at (0,0) = %d",color);
  outtext(array); 
  getch();
}
 
 
7.

putpixel function

Description: -
putpixel function plots a pixel at location (x, y) of specified color.
Declaration: -
void putpixel(int x, int y, int color);
For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms.
Program:-

#include<stdio.h>

#include<graphics.h>
#include<conio.h>
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   putpixel(25, 25, RED); 
   getch();   }
 

8.

circle function

Description: -

circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle.

Declaration: -

void circle(int x, int y, int radius);

 

#include<stdio.h>
#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   circle(100, 100, 50); 
   getch();
}

 

9.
line function
Description :-
 
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.The code given below draws a line.
Declaration :-
void line(int x1, int y1, int x2, int y2);
 

#include<stdio.h>

#include <graphics.h>
#include <conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   line(100, 100, 200, 200); 
   getch();
}
 
10.

rectangle function

Description: -

rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
Declaration: -
void rectangle(int left, int top, int right, int bottom);

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   rectangle(100,100,200,200); 
   getch();
}
 
11.

ellipse function

Description: -
Ellipse is used to draw an ellipse (x,y) are coordinates of center of 
the ellipse, stangle is the starting angle, end angle is the ending 
angle, and fifth and sixth parameters specifies the X and Y radius 
of the ellipse. To draw a complete ellipse strangles and end angle 
should be 0 and 360 respectively.
Declarations:-
void ellipse(int x, int y, int stangle, int endangle, int xradius,
 int yradius);
 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   ellipse(100, 100, 0, 360, 50, 25); 
   getch();   
}
 

12.

getx function

Description: -

getx function returns the X coordinate of current position.

Declaration:
int getx();

 

#include<stdio.h>

#include <graphics.h>
#include <conio.h> 
void main()
{
   int gd = DETECT, gm;
   char array[100]; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   printf(array, "Current position of x = %d",getx()); 
   outtext(array); 
   getch();   
}

 

13.

gety function

Description: -
gety function returns the y coordinate of current position.
Declaration: -
int gety();
 

#include<stdio.h>

#include <graphics.h>
#include <conio.h> 
void main()
{
   int gd = DETECT, gm, y;
   char array[100]; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   y = gety(); 
   printf(array, "Current position of y = %d", y); 
   outtext(array); 
   getch();
}
 

14.

getmaxcolor function

Description: -

 

getmaxcolor function returns maximum color value for current 
graphics mode and driver. Total number of colors available for 
current graphics mode and driver are ( getmaxcolor() + 1 ) as 
color numbering starts from zero.

Declaration:

int getmaxcolor();

 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm, max_colors;
   char a[100]; 
   initgraph(&gd,&gm,"C:\\TC\\BGI"); 
   max_colors = getmaxcolor(); 
   printf(a,"Maximum number of colors for current graphics mode 
   and driver = %d",max_colors+1);
   outtextxy(0, 40, a); 
   getch();
}

 

15.
setfillstyle function
Description:-
setfillstyle function sets the current fill pattern and fill color.
Different fill styles:
enum fill_style
{EMPTY_FILL,SOLID_FILL,LINE_FILL,LTSLASH_FILL,
SLASH_FILL,KSLASH_FILL,LTBKSLASH_FILL,HATCH_FILL,
XHATCH_FILL,INTERLEAVE_FILL,WIDE_DOT_FILL,
CLOSE_DOT_FILL, USER_FILL };
Declaration :-
void setfillstyle( int pattern, int color);
 

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   setfillstyle(XHATCH_FILL, RED);
   circle(100, 100, 50);
   floodfill(100, 100, WHITE); 
   getch();
}
 
 
16.
Setlinestyle
Description:-

Available line styles:
enum line_styles
{SOLID_LINE,DOTTED_LINE,CENTER_LINE,
DASHED_LINE, USERBIT_LINE }
Declaration:
void setlinestyle( int linestyle, unsigned upattern, int thickness );

#include<stdio.h>

#incude<conio.h>

#include <graphics.h> 
void main()
{
   int gd = DETECT, gm, c , x = 100, y = 50; 
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   for ( c = 0 ; c < 5 ; c++ )
   {
       setlinestyle(c, 0, 2); 
       line(x, y, x+200, y);
       y = y + 25;
   
   getch(); 
}

17.
setbkcolor function
Description:-

setbkcolor function changes current background color 
e.g. setbkcolor(YELLLOW) changes the current background 
 color to YELLOW.
Remember that default drawing color is WHITE and 

background color is BLACK.
Declaration: -
void setbkcolor(int color);

#include<stdio.h>

#include<graphics.h>
#include<conio.h> 
void main()
{
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "C:\\TC\\BGI"); 
   outtext("Press any key to change the background color to
   GREEN.");
   getch(); 
   setbkcolor(GREEN); 
   getch();
}

 

18.
Setfillpattern
Description:-
 
setfillpattern is like setfillstyle, except that you use it to set a user-defined 8x8 pattern rather than a predefined pattern. upattern is a pointer to a sequence of 8 bytes, with each byte corresponding to 8 pixels in the pattern. Whenever a bit in a pattern byte is set to 1, the corresponding pixel is plotted.
Declaration:-
void setfillpattern(char *upattern, int color);
 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   int maxx, maxy;
   char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};
   initgraph(&gdriver, &gmode, "");
   errorcode = graphresult();
   getch();
   exit(1);              
   maxx = getmaxx();
   maxy = getmaxy();
   setcolor(getmaxcolor());
   setfillpattern(pattern, getmaxcolor());
   bar(0, 0, maxx, maxy);
   getch();
 }
 
19.
Getfillpattern
Description: -

getfillpattern copies the user-defined fill pattern, as set by setfillpattern, into the 8-byte area pointed to by pattern.pattern is a pointer to a sequence of 8 bytes, with each byte corresponding to 8 pixels in the pattern. Whenever a bit in a pattern byte is set to 1, the corresponding pixel will be plotted.

For example, the following user-defined fill pattern represents a checkerboard:
char checkboard[8] = {   0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
Declaration:-
void getfillpattern(char *pattern);

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04}; initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
setfillpattern(pattern, getmaxcolor());
 bar(0, 0, maxx, maxy); getch();
getfillpattern(pattern);
pattern[4] -= 1; pattern[5] -= 3; pattern[6] += 3; pattern[7] -= 4;
 setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy);
getch();
 }


20.

gotoxy function

Description:-
gotoxy in c: gotoxy function places cursor at a desired location on screen i.e. we can change cursor position using gotoxy function.
Declaration:-
void gotoxy(int x,int y)
where (x, y) is the position where we want to place the cursor.

#include <stdio.h>
#include <conio.h> 
void main()
{
   int x, y; 
   x = 10;
   y = 10; 
   gotoxy(x, y); 
   printf("C program to change cursor position.");  
   getch();
 }
 
Below is a C program that includes the usage of above graphics function with 
its output :-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
            int choice;
            int gd=DETECT,gm;
            int style;
             char *fname[] = {"SOLID_FILL"};
            initgraph(&gd,&gm,"c:\\tc\\bgi");
            while(1)
            {
            printf("enter the choice which you want:");
            printf("\n 1)circle");
            printf("\n 2)rectangle");
            printf("\n 3)ellipse");
            printf("\n 4)line");
            printf("\n 5)point");
            printf("\n 6)exit");
            scanf("%d",&choice);
            clrscr();
            cleardevice();
            switch(choice)
            {

            case 1:
                        for(style = SOLID_FILL; style < USER_FILL; style++)
                        setfillstyle(SOLID_FILL, getmaxcolor());
                        setbkcolor(5);
                        setcolor(11);
                        circle(40,50,20);
                        floodfill(50,60,11);
                        getch();
                        break;

            case 2:setbkcolor(6);
                        setcolor(12);
                        rectangle(100,80,200,140);
                        getch();
                        break;

            case 3:setbkcolor(8);
                        setcolor(13);
                        ellipse(80,100,45,405,10,20);
                        getch();
                        break;

            case 4: setbkcolor(12);
                        line(160,350,250,390);
                          getch();
                        break;

            case 5:setbkcolor(2);
                        putpixel(40,50,20);
                        getch();
                        break;

            case 6:exit();
                        getch();
                        break;
            }
            getch();
            } 
    }

output:

 

            

            

THANK YOU!!!!

1 comment: