Wednesday, 10 July 2013

Drawing symbols, lines, polygons, shapes in color in c using graphics.h

Posted by srinath reddy
Drawing various symbols like lines, polygons, fractals can be made by using the graphics functions available in graphics.h header file. we can also draw various shapes like square, rectangle, circle, elipse, etc by using the inbuilt library functions of graphics programming. by using those functions we can draw anything on the pixeled graphics screen(console screen). In this article i will explain you about how to draw all those lines circles rectangles and fractals in c using the library graphics functions available in graphics.h header file.
we can also draw them in different colors. In this article we shall discuss about all those things clealry using example c programs and syntax for those functions.

Drawing Straight Lines:

we can draw lines using the library function line() in c. line function has four parameters. they are the co-ordinates of the starting point of the line and the co-ordinates of the ending point of the line. the point can be represented by using two co-ordinates x and y.

syntax to draw a line:-

line(x1,y1,x2,y2);
where
x1,y1 are the co-ordinates of the initial/starting point of the line.
x2,y2 are the co-ordinates of the final/ending point of the line.

C Program to draw Lines:

c program to draw lines

how to draw lines in color ?

To draw the lines in specified colours then use the function setcolor() in c. the function setcolor() takes the colour codes as parameters.
syntax to setcolor():-
setcolor(6);
The below are the list of colour codes used in graphics functions available in graphics.h header file.

List of colour codes used in graphics.h:

colour codes in graphics programming

applying line styles:

To draw the lines in a different styles use the function setlinestyle() in c. the function setlinestyle() takes three parameters of integer type. they are
<linestyle>,<pattern>,<thickness>.
syntax to apply line styles:-
setlinestyle(1,2,3);
There are predefined constants for line style and thickness. you can use those constants in setlinestlye() function.

Predefined constants and codes for line styles in c:

drawing lines in c

Drawing Rectangles:

Rectangles can be drawn using the library function rectangle(). the library function rectangle() takes four integer arguments. they are the co-ordinates of the starting point and breadth and length of the rectangle.

syntax to draw a rectangle:-

rectangle(x,y,breadth,length);
where x and y are the co-ordinates of the point and breadth and length are the breadth and length of the rectangle respectively.

C program to draw rectangles:-

c program to draw rectangles

how to draw rectangles in colour in c ?

To draw the rectangle in specified colours then use the same function setcolor(). The function setcolor() takes the colour codes as parameters.

filling color inside a rectangle in c:

rectangle can be filled by different colours and styles. the library function setfillstyle() in c is used to set the style to fill in the rectangle and floodfill() function in c is used to fill the color and style in the rectangle. set fillstyle() function takes two integer arguments, and floodfill() function takes three integer arguments.
syntax for setfillstyle():-
setfillstyle(x,y);
where x is the style code and y is the colour code.
syntax for floodfill():-
floodfill(x,y,z);
where x and y are the co-ordinates of a point and z is the colour code of boundary of area. if the point (x,y) is inside the rectangle then the colour is filled inside the rectangle. if the point (x,y) is outside the rectangle then the colour is filled outside the rectangle.

constants and codes for rectangle fill styles in c:

constants for rectangle fill styles in c

Drawing Circles:

circles can be drawn using the library function circle(). The library function circle() takes three integer arguments. they are the co-ordinates of the center and radius of the circle.

syntax to draw a circle:-

circle(x,y,radius);
where x and y are the co-ordinates of the center of the circle and radius is the radius of the circle.

C Program to draw circles:

c program to draw circles

how to draw circles in colour in c?

To draw the circle in specified colours then use the function setcolor(). The function setcolor() takes the colour codes as parameters.

how to fill circle with colours in c?

circle can be filled by different colours and styles. the library function setfillstyle() is used to set the style to fill in the circle and floodfill() function is used to fill the color and style in the circle. set fillstyle() function takes two integer arguments, and floodfill() function takes three integer arguments.
syntax for setfillstyle():-
setfillstyle(x,y);
where x is the style code and y is the colour code.
syntax for floodfill():-
floodfill(x,y,z);
where x and y are the co-ordinates of a point and z is the colour code of boundary of area. if the point (x,y) is inside the circle then the colour is filled inside the circle. if the point (x,y) is outside the circle then the colour is filled outside the circle.

constants and codes for circle fill styles in c:

drawing circles in c

Drawing Boxes and Tables:

Boxes and tables are drawn using the extended ASCII characters. There are ascii codes to draw the horizontal, vertical lines and to draw edges. the following table shows the ascii value of the characters used for drawing boxes.

List of ascii codes for drawing boxes and tables:

list of ascii codes for drawing boxes and tables
By using the above ascii codes we can draw boxes and tables. we use gotoxy() function to move the cursor to the desired place and draw the box at that place. gotoxy() function takes two parameters. they are the position of the cursor in terms of columns and rows.if we declare gotoxy(1,5); then the cursor moves to first column, fifth row.

C program to draw a box:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main(void)
{
int i, j, x, y, sizex, length, breadth, sizey ;
clrscr();
printf("\n enter length and breadth of box");
scanf("%d%d",&sizex,&sizey);
printf("Enter position of the box ");
scanf("%d%d",&x,&y);
for(i=1;i<=sizex;i++) /* for drawing horizontal lines */
{
gotoxy(x+i,y);
printf("%c",196);/* printing top horizontal line */
gotoxy(x+1,y+1+sizey);
printf("%c",196); /* printing bottom horizontal line */
}
for(i=);i<=sizey;i++)/* for drawing vertical lines */
{
gotoxy(x,y+i);
printf("%c",179); /* printing left vertical line */
gotoxy(x+sizex+1,y+i);
printf("%c",179); /* printing right vertical line */
}
gotoxy(x,y);
printf("%c",218); /* printing top left corner */
gotoxy(x+1+sizex,y);
printf("%c",191); /* printing top right corner */
gotoxy(x,y+1+sizey);
printf("%c",192); /* printing bottom left corner */
gotoxy(x+1+sizex,y+1+sizey);
printf("%c",217); /* printing bottom right corner */
getch();
return 0;
}

Conclusion:

By using all the above graphics functions, we can draw any kind of shapes, lines and symbols in colours. All these functions are available in graphics.h header file. These functions can be used for graphics programming in c. If you have any doubts, then share them in the comments. thank you..........

0 comments:

Post a Comment