CG 7
CG 7
Rectangle
rectangle function in graphics.h
Declaration :-
void rectangle(int left, int top, int right, int bottom);
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.
Example-1
#include<graphics.h> #include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,”");
rectangle(100,100,200,200);
getch();
closegraph();
return 0;
}
bar function
Bar function is used to draw a 2-dimensional,
rectangular filled in bar.
void bar(int left, int top, int right, int bottom).
Coordinates of left top and right bottom corner are
required to draw the bar. 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. Current fill
pattern and fill color is used to fill the bar. To
change fill pattern and fill color use setfillstyle.
setfillstyle( , )
Setfillstyle function is using to fill the
surface of any graphics objects when
create after the function.
There is two arguments
1. Fill style
2. Color
Setfillstyle (pattern, color);
Example
#include<graphics.h>
#include<conio.h>
main()
{
initgraph(&gd,&gm,"");
settextstyle(1,0,2);
setfillstyle(1, 4);
bar(100, 100, 200, 200);
rectangle(100,100,200,200);
getch();
closegraph();
return 0;
}
Example Move a car
#include<graphics.h>
#include<conio.h>
main()
{
int i, j = 0, d = DETECT, m;
initgraph(&d,&m,"");
settextstyle(1,0,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
for( i = 0 ; i <= 420 ; i = i + 10, j++ )
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);
if( i == 420 )
break;
if ( j == 15 )
j = 2;
cleardevice(); // clear screen
}
getch();
closegraph();
return 0;
}