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:

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:

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 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:-

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:

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:

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 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:

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; }
0 comments:
Post a Comment