Graphics in C Functions

  • When anybody wants to draw any graphics on the screen then he need the various functions which are defined in the graphics.h header file
  • These functions are used for drawing the different-different images on screen and also for providing the attractive graphics on the screen as per demand of the user.
  • These functions are used to develop graphics programs, draw shapes, produce images and develop games and animation etc
  • These functions of graphics.h header file are used to draw shapes like circle, ellipse, rectangle, lines and many more geometrical figures. One can fill the color into these images and color can also be changed using the functions.

Here is a table which gives the overview of some commonly used functions of graphics.h header file which includes function name, its purpose and example:

Name Of The Function Purpose Example
putpixel(x,y) Plots the pixel at the grid location (x,y). putpixel(320,240) - plots a pixel with the set color at the center of the screen.
getpixel(x,y) Gets the color of the pixel located at (x,y). getpixel(320,240) - gets the color of the center of the screen for 640X480 resolution.
line(x1, y1, x2, y2) Draws a line from (x1, y1) to (x2, y2). line(0,0,320,240) – draws a line from the upper left of screen to the center.
circle(xc, yc, r) Draws a circle with center (xc, yc) & radius = r. circle(320,240,10) – draws a circle with center at the center of the screen and radius = 10 units.
rectangle(x1,y1,x2,y2) Draws a rectangle with (x1, y1) and (x2, y2) as corners. rectangle(320,240,400, 300) - will draw a rectangle such that (320,240) is the left-top co-ordinate and (400,300) is the right-bottom one.
ellipse(xc, yc, s, e, xr, yr) Draws an ellipse with (xc,yc) as center. xr&yr are semimajor and semiminor axes respectively. If s=0 & e=180 only upper half of the ellipse is drawn. ellipse(320,240,0,360,100,50) – draws an entire ellipse whose center is at (320,240) with semimajor and semiminor axes 100 & 50 respectively.
arc(xc, yc, s, e, r) Draws an arc with center as (xc,yc), radius=r & staring & end angles as s & e respectively. arc(320,240,45,135,100) – draws an arc whose center is at (320,240), radius is 100 units and the start angle is 45 degrees & the end angle 135 degrees.
moveto(x,y) Moves the cursor pointer to (x, y). moveto(320,240) - moves the cursor to the center of the screen from its current location.
lineto(x,y) Draws a line up to (x, y) from the current cursor location. lineto(320,240) – will draw a line from the current location to the center of the screen.
moverel(xr, yr) Moves the cursor by a relative distance of xr along x-axis and yr along y-axis. moverel(10, 5) – moves the cursor in the x-direction by 10 units and in the y-direction by 15 units.
linerel(xd,yd) Draws a line from the cursor point to a point at a relative distance of xd along x and yd along y, from the current position. linerel(10, 5) – draws a line from the current position to a point whose distance from the current position is 10 units along x and 5 units along y.
outtextxy(x,y,``string”) Displays the string within inverted commas at location (x,y). outtextxy(320,240,``Graphics”) - displays the word Graphics with the first letter starting from the center of the screen.
setlinestyle(x,y,z) Draws lines with specified styles, where x is the line type, y is the pattern and z is the thickness. (x=0 ⇒ solid, x=1⇒dotted, x=2⇒ center line,x=3⇒ dashed, x=4 ⇒user-defined line). setlinestyle(0,1,1) – sets the system for drawing a solid line with thickness 1 and a particular pattern.
setcolor(x) Sets color of line. There are 16 possible colors. setcolor(RED) – sets the color of the drawing pen as RED.
setfillstyle(x,y) Sets a predefined fill style where x⇒item number identifying a fill style &y⇒color. setfillstyle(SOLID_FILL, RED) – prepares to fill an area in solid style and with RED color.
fillellipse(xc,yc,xrad,yrad) Fills up an ellipse with center (xc,yc), a=xrad& b=yrad. fillellipse(320,240,100,50)– fills an ellipse whose center is at (320,240) and the two axes at x=100 and y=50.
drawpoly(x,y) Draws a polygon where x⇒number of points used to build the polygon and y⇒base address of the array containing the co-ordinate points. drawpoly(4,array) – draws a polygon containing 4 points and the base address is contained in the array array[].
fillpoly(x,y) Fills up a polygon. x⇒number of points used to build the polygon and y⇒base address of the array containing the co-ordinate points. fillpoly(x,y) - draws a polygon containing 4 points and the base address is contained in the array array[]. It also fills up the polygon using the current fill style and color.
getnewsettings(&vp) Filling up the elements of the structure viewporttype (ex vp) with the co-ordinates of the current viewport.
getpalette(&palette) Gets the set of color values. Palette is of structure palettetype.
setpalette(colornum,color) Changes the color values. setpalette changes the colormun entry in palette to color. setpalette(0,5) – Changes the first color in the current palette (background color) to actual color number 5.
kbhit() Returns true if keyboard is hit. This is useful for interactive graphics.
settextstyle(x,y,z) Sets font style. x⇒font type, y⇒font direction (e.g. VERT_DIR), z⇒point size. settextstyle(DEFAULT_font,HORIZ_DIR,4) – draws a text in the existing font and from left to right
sound(x) Activates the speaker at a specific time unit in x Hz. sound(7) – will activate a sound at 7 Hz.
nosound() Stops previously activated sound.
delay(x) Allowing the previously executed command to remain activated for a time unit x(in msec). delay(6000) – will make the command run for 6 sec.
getimage(x,y,a,b,c) A function for storing the image. x⇒x co-ordinate of the top left corner of the block. y⇒y co-ordinate of the top left corner of the block. a⇒x co-ordinate of the bottom right corner of the block. b⇒y co-ordinate of the bottom right corner of the block. c⇒the address of the memory location from where the image would be stored.
bar(left,top,right,bottom) Draws a filled-in two-dimensional rectangular bar. It does not draw the bar boundary. The rectangle is drawn using the current fill pattern and color. bar(20,30,40,70) – will draw a rectangle whose upper left corner is at (20,30) and lower right corner is at (40,70).