In the previous article, we discussed some BGI library functions, namely, getmaxcolor(), outtextxy(), putpixel(), getpixel(), setcolor() etc. In the present article, we shall discuss some more BGI library functions. These functions draw various shapes like, circle, arc, line etc.
This article discusses the following functions :
a) void line(int x1, int y1, int x2, int y2)
b) int getx(void)
c) int gety(void)
d) void linerel(int dx, int dy)
e) void lineto(int x, int y)
f) void moverel(int dx, int dy)
g) void moveto(int x, int y)
h) void circle(int x, int y, int r)
i) void arc(int xc, int yc, int stangle, int endangle, int r)
j) void pieslice(int x, int y, int stangle, int endangle, int radius)
k) void getaspectratio( int *xasp, int *yasp)
l) void setaspectratio( int xasp, int yasp)
a) void line(int x1, int y1, int x2, int y2)
This function draws a line from (x1, y1) to (x2, y2) using the current color, line style, and thickness. It does not update the current position (CP).
Example 1 :
#include <graphics.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
setcolor(YELLOW);
line(100, 100, getmaxx()/2, getmaxy()/2);
getch();
closegraph();
return 0;
}
Output of this line() program
Example 2 :
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
while(!kbhit())
{
setcolor(random(getmaxcolor()));
line(random(getmaxx()), random(getmaxy()), random(getmaxx()), random(getmaxy()));
}
getch();
closegraph();
return 0;
}
Output of this line() program
b) int getx(void)
This function returns the x-coordinate of the current graphics position.
c) int gety(void)
This function returns the y-coordinate of the current graphics position.
d) void linerel(int dx, int dy)
This function draws a line from the current position to a point that is a relative distance (dx, dy) from the current position, then advances the current position by (dx, dy).
Example of linerel(), getx() and gety() :
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode;
char msg[80];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
//move the C.P. to location (20, 30)
moveto(20, 30);
//create and output a message at (20, 30)
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
//draw a line to a point a relative
//distance away from the current
//value of C.P.
linerel(100, 100);
//create and output a message at C.P.
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
closegraph();
return 0;
}
Output of this linerel(), getx() and gety() program
e) void lineto(int x, int y)
This function draws a line from the current position to (x, y), then moves the current position to (x, y).
For example,
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
int main(void)
{
int gdriver = DETECT, gmode;
char msg[80];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
//move the C.P. to location (120, 130)
moveto(120, 130);
//create and output a message at (120, 130)
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(120, 130, msg);
//draw a line to (300,320)
lineto(300, 320);
//create and output a message at C.P.
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
closegraph();
return 0;
}
Output of this lineto() program
f) void moverel(int dx, int dy)
This function moves the current position dx pixels in the x direction and dy pixels in the y direction.
For example,
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
char msg[80];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
//move the C.P. to location (20, 30)
moveto(20, 30);
//plot a pixel at the C.P.
putpixel(getx(), gety(), getmaxcolor());
//create and output a message at (20, 30)
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
//move to a point a relative distance
//away from the current value of C.P.
moverel(120, 130);
//plot a pixel at the C.P.
putpixel(getx(), gety(), getmaxcolor());
//create and output a message at C.P.
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
closegraph();
return 0;
}
Output of this moverel() program
g) void moveto(int x, int y)
This function moves the current position to viewport position (x,y).
For example,
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
char msg[80];
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
//move the C.P. to location (20, 30)
moveto(20, 30);
//plot a pixel at the C.P.
putpixel(getx(), gety(), getmaxcolor());
//create and output a message at (20, 30)
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
//move to (100,100)
moveto(100, 100);
//plot a pixel at the C.P.
putpixel(getx(), gety(), getmaxcolor());
//create and output a message at C.P.
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
closegraph();
return 0;
}
Output of this moveto() program
h) void circle(int x, int y, int r)
This function draws a circle with centre (x, y) and radius r.
For example,
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int midx, midy;
int radius = 100;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(CYAN);
//draw the circle
circle(midx, midy, radius);
getch();
closegraph();
return 0;
}
Output of this circle() program
i) void arc(int xc, int yc, int stangle, int endangle, int r)
This function draws an arc with (xc, yc) as starting point, r as radius and stangle and endangle as starting and ending angles respectively.
If stangle = 0 and endangle = 360, the call to arc draws a complete circle.
If your circles are not perfectly round, use setaspectratio to adjust the aspect ratio.
Example 1 :
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
//draw arc
arc(midx, midy, stangle, endangle, radius);
getch();
closegraph();
return 0;
}
Output of this arc() program
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
int gdriver = DETECT, gmode;
int angle=360, radius=100;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
while(!kbhit())
{
setcolor(random(getmaxcolor())+1);
arc(random(getmaxx()), random(getmaxy()), random(angle), random(angle), random(radius));
}
getch();
closegraph();
return 0;
}
Output of this arc() program
j) void pieslice(int x, int y, int stangle, int endangle, int r)
This function draws a pie slice with (x,y)as center point, r as radius and stangle and endangle as starting and ending angles in degrees respectively.
For example,
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
int main()
{int gdriver = DETECT, gmode;
int midx, midy;
int stangle = 45, endangle = 135, radius = 120;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
//set fill style and draw a pie slice
setfillstyle(EMPTY_FILL, getmaxcolor());
pieslice(midx, midy, stangle, endangle, radius);
getch();
closegraph();
return 0;
}
Output of this pieslice() program
k) void getaspectratio( int *xasp, int *yasp)
This function gets the current graphics mode's aspect ratio in *xasp and *yasp. The values stored by its arguments have been listed below :
# xasp : Points to the x aspect factor
# yasp : Points to the y aspect factor
l) void setaspectratio( int xasp, int yasp)
This function changes the default aspect ratio of the graphics system. The values stored by its arguments have been listed below :
# xasp : New value for x aspect factor
#yasp : New value for y aspect factor
Example of getaspectratio() and setaspectratio() :
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
int xasp, yasp, midx, midy;
initgraph(&gdriver, &gmode, "c://turboc3//bgi");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
//get current aspect ratio settings
getaspectratio(&xasp, &yasp);
//draw normal circle
circle(midx, midy, 100);
getch();
// clear the screen
cleardevice();
//adjust the aspect for a wide circle
setaspectratio(xasp/2, yasp);
circle(midx, midy, 100);
getch();
//adjust the aspect for a narrow circle
cleardevice();
setaspectratio(xasp, yasp/2);
circle(midx, midy, 100);
//clean up
getch();
closegraph();
return 0;
}
Output of this getaspectratio() and setaspectratio() program
Normal Circle |
Wide Circle |
Narrow Circle |
Aspect Ratio
The graphics system uses the aspect ratio to make sure that circles are round onscreen.
If circles appear elliptical, the monitor is not aligned properly. You could correct this in the hardware by realigning the monitor, but it's easier to change in the software by using setaspectratio to set the aspect ratio.
To obtain the current aspect ratio from the system, call getaspectratio.
*yasp is normalized to 10,000. In general, the relationship between *yasp and *xasp is
*yasp = 10,000
*xasp <= 10,000
On all graphics adapters except the VGA, *xasp < *yasp (because the pixels are taller than they are wide).
On the VGA, which has square pixels, *xasp = *yasp.
No comments:
Post a Comment