Functions of graphics
1) getmaxx function
Description: - Getmaxx function returns the maximum X coordinate for
current graphics mode and driver.
Declaration: - int getmaxx();
C program for getmaxx
#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();
C program for getmaxy
#include<stdio.h>
#include<graphics.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 and
driver is = %d.",max_y);
outtext(array);
getch();
}
3) setcolor function
Declaration: - void setcolor(int color);
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.
C programming of setcolor
#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.
Declaration: int getcolor();
E.g. a = getcolor(); // a is an integer variable if current drawing color is
WHITE then a will be 15.
C programming code for 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.
C programming of outtextxy
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
outtextxy(100, 100, "Outtextxy function");
getch();
}
6) getpixel function
Description: - getpixel function returns the color of pixel present at
location(x, y).
Declaration: - int getpixel(int x, int y);
C program for getpixel
#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.
C programming code for putpixel
#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
Declaration: - void circle(int x, int y, int radius);
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.
C program for circle
#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 [Link] code given
below draws a line.
Declaration :- void line(int x1, int y1, int x2, int y2);
C programming code for line
#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
Declaration: - void rectangle(int left, int top, int right, int bottom);
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. The code given
below draws a rectangle.
C programming of rectangle
#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
Declarations:-
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
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.
C programming code for ellipse
#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();
C program of 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();
C programming for 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();
C program of 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.
Declaration :- void setfillstyle( int pattern, int color);
Different fill styles:
enum fill_style
{EMPTY_FILL,SOLID_FILL,LINE_FILL,LTSLASH_FILL,SLASH_FILL,KSLAS
H_FILL,
LTBKSLASH_FILL,HATCH_FILL,XHATCH_FILL,INTERLEAVE_FILL,WIDE_
DOT_FILL, CLOSE_DOT_FILL, USER_FILL };
C programming of setfillstyle
#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
Declaration:
void setlinestyle( int linestyle, unsigned upattern, int thickness );
Available line styles:
enum line_styles
{ SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE,
USERBIT_LINE
};
C programming code
#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
Declaration: - void setbkcolor(int color);
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.
C programming of setbkcolor
#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
Declaration:- void setfillpattern(char *upattern, int color);
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.
C programming of setfillpattern
#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
Declaration:- void getfillpattern(char *pattern);
Description: - getfillpattern copies the user-defined fill pattern, as set by
setfillpattern, into the 8-byte area pointed to by [Link] 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};
C programming of getfillpattern
#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.
C programming of gotoxy
#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();
}