5.2.4.1 Filled Circles
The hollow circle function is interesting, but really seeing the full effect of circles requires the circlefill function:
void circlefill(BITMAP *bmp, int x, int y, int radius, int color)
The following program (shown in Figure 3.17) demonstrates the solid-filled
circle function.
122 Chapter 3 n 2D Vector Graphics Programming
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1,"CircleFill Program - %dx%d - Press ESC to quit",SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 30 + rand() % (SCREEN W-60);
y = 30 + rand() % (SCREEN H-60);
radius = rand() % 30;
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the filled circle
circlefill(screen, x, y, radius, color);
rest(25);
}
Figure 3.7
The CircleFill program draws filled circles.
Ellipses
The ellipse function is similar to the circle function, although the radius is
divided into two parameters—one for the horizontal and another for the vertical—as indicated:
void ellipse(BITMAP *bmp, int x, int y, int rx, int ry, int color)
The Ellipses program draws random ellipses on the screen using two parameters—radiusx and radiusy.
#include
#include "allegro.h"
int main(void)
{
int x,y,radiusx,radiusy;
int red,green,blue,color;
//initialize everything
allegro init();
install keyboard();
install timer();
srand(time(NULL));
//initialize video mode to 640x480
int ret = set gfx mode(GFX AUTODETECT WINDOWED, 640, 480, 0, 0);
if (ret != 0) {
allegro message(allegro error);
return 1;
}
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1,
"Ellipses Program - %dx%d - Press ESC to quit",
SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 30 + rand() % (SCREEN W-60);
y = 30 + rand() % (SCREEN H-60);
radiusx = rand() % 30;
radiusy = rand() % 30;
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the ellipse
ellipse(screen, x, y, radiusx, radiusy, color);
rest(25);
}
//end program
allegro exit();
return 0;
}
END OF MAIN()
Filled Ellipses
You can draw filled ellipses using the ellipsefill function, which takes the same
parameters as the ellipse function but simply renders each ellipse with a solid
filled color:
void ellipsefill(BITMAP *bmp, int x, int y, int rx, int ry, int color)
Figure 3.18 shows the output from the EllipseFill program.
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1,"EllipseFill Program - %dx%d - Press ESC to quit",SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 30 + rand() % (SCREEN W-60);
y = 30 + rand() % (SCREEN H-60);
radiusx = rand() % 30;
radiusy = rand() % 30;
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the filled ellipse
ellipsefill(screen, x, y, radiusx, radiusy, color);
rest(25);
}
- Yêu cầu SV chuẩn bị:
Đọc chương 3 tài liệu [1].
Đọc c ác phần tương ứng trong tài liệu [5].
Chú ý nghe giảng.
Tích cực tham gia phát biểu ý kiến
Chia sẻ với bạn bè của bạn: |