5.2.2.2 Vertical Lines
Vertical lines are drawn with the vline function:
void vline(BITMAP *bmp, int x, int y1, int y2, int color)
The VLines program (see Figure 3.11) is the same as the HLines program except
for a single function call inside the while loop. Also note that this program uses
a single X variable and two Y variables, y1 and y2. Here is the listing:
//display screen resolution
textprintf(screen, font, 0, 0, 15, "VLines Program - %dx%d - Press ESC to quit",
SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 10 + rand() % (SCREEN W-20);
y1 = 10 + rand() % (SCREEN H-20);
y2 = 10 + rand() % (SCREEN H-20);
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the vertical line
vline(screen,x,y1,y2,color);
}
5.2.3 Rectangles
The next logical step is a twodimensional object containing points in both the X-axis and the Y-axis. Although a triangle would be the next best thing, I believe the rectangle is easier to deal with at this stage because triangles carry with them the connotation of the mighty polygon, and we aren’t quite there yet. Here is the rect function:
void rect(BITMAP *bmp, int x1, int y1, int x2, int y2, int color)
As you might have guessed, a rectangle is comprised strictly of two horizontal and two vertical lines; therefore, the rect function simply calls hline and vline to render its shape (see Figure 3.5).
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1, "Rect Program - %dx%d - Press ESC to quit", SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x1 = 10 + rand() % (SCREEN W-20);
y1 = 10 + rand() % (SCREEN H-20);
x2 = 10 + rand() % (SCREEN W-20);
y2 = 10 + rand() % (SCREEN H-20);
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the rectangle
rect(screen,x1,y1,x2,y2,color);
}
Chia sẻ với bạn bè của bạn: |