Example: Graphics Primitives

Here's an example program that uses many of Fastgraph's graphics primitives. The program has a top-level menu with selections for different graphics primitive demonstrations, as shown here:

Each menu selection (except Exit) calls an action function named xxxClick(), where "xxx" is the name of the menu item (for example, the Points item calls the PointsClick() function). These action functions call Fastgraph functions to draw the graphics in a 640x480 virtual buffer and then call the program's Blit() function to copy the virtual buffer contents to the client area. Blit() does this using either fg_vbpaste() or fg_vbscale(), depending on the size of the client area. Here is a summary of the action functions:

PointsClick()

Displays a pattern of points. Uses fg_point().

LinesClick()

Displays a series of horizontal, vertical, and diagonal lines of different colors. Uses fg_draw() and fg_move().

RectanglesClick()

Displays 100 rectangles (arranged 10 by 10) of different colors. Uses fg_rect().

CirclesClick()

Displays 25 increasingly larger concentric circles, centered in the middle of the virtual buffer. Uses fg_circle() and fg_move().

EllipsesClick()

Displays 80 increasingly larger concentric ellipses, centered in the middle of the virtual buffer. Uses fg_ellipse() and fg_move().

PolygonsClick()

Displays a grid of filled convex polygons. Uses fg_polyfill() and fg_polyoff().

PaintClick()

Demonstrates region fill and other graphics primitives. Uses fg_box(), fg_circle(), fg_draw(), fg_move(), fg_paint(), and fg_rect().

In addition, each action function uses fg_fillpage() to fill the virtual buffer with a background color, and fg_setcolor() to define the colors in which the objects are drawn.

The CirclesClick() and EllipsesClick() action functions are very similar. Each starts by filling the virtual buffer with blue pixels (color 11) and moving the graphics cursor to the center of the screen. A for loop then draws the circles or ellipses, increasing the radius in each iteration.

The LinesClick() action function first fills the virtual buffer with white pixels (color 25). It then draws a series of horizontal and vertical lines in several colors, followed by a series of light red (color 22) diagonal lines. Note that we rely on the fact that fg_draw() clips each line at the virtual buffer limits to simplify drawing the diagonal lines.

The PaintClick() action function fills the virtual buffer with white (color 25) pixels. It then draws a light cyan (color 21) rectangle covering most of the virtual buffer and draws a black (color 10) border around the rectangle's perimeter. Next, it draws a black circle with a radius of 80 pixels, and then draws two black lines that divide the circle into quadrants. It then calls fg_paint() to fill each quadrant, starting with the upper left quadrant and proceeding clockwise, with colors 11, 12, 13, and 14. Finally, it fills the area outside the circle, but within the first rectangle, with yellow (color 24) pixels.

The PointsClick() action function first fills the virtual buffer with yellow (color 24) pixels. It then draws two patterns of equally spaced points, the first in light blue (color 19) and the second in light red (color 22).

The PolygonsClick() action function uses fg_polyfill() to draw 225 shapes, each made up of three convex polygons, in a repeating pattern. Three sets of four polygon vertices are defined at the beginning of the function. Note how the loops that draw the polygons use fg_polyoff() to define varying offsets. This allows us to use the same vertex values for each polygon, regardless of where it's drawn. Note that these offsets cause all or parts of some polygons to lie outside the virtual buffer limits, but since fg_polyfill() clips the polygons, we only see the portions falling inside the virtual buffer.

The final action function, RectanglesClick(), is very simple. It uses fg_rect() to draw 100 64x48 rectangles in a 10x10 grid covering the virtual buffer. Note how we let the Color variable range between 10 and 24 to draw adjacent rectangles in different colors.

Now that we've described the program's action functions, we still need to discuss some other issues. First of all, why does the program use fg_vbscale() when the window is larger than the virtual buffer, and fg_vbpaste() when it isn't? To answer this question, consider what the "points" display would look like if we always displayed it with fg_vbscale(). When the window is smaller than the virtual buffer size, especially when it is much smaller, fg_vbscale() must eliminate rows and columns to "squish" the virtual buffer contents into the client area. In the points display, this would have the undesirable effect of removing entire points and thus losing the nice pattern we drew. When the window is larger, fg_vbscale() adds rows and columns, which makes some of the points larger than one pixel, but the overall display still retains a reasonably good appearance. For images that include fine detail, important individual pixels, or horizontal or vertical lines, using fg_vbpaste() and fg_vbscale() in this manner is generally preferable to using fg_vbscale() only.

One slight problem with this scheme is that the window size includes the caption bar, the menu bar, and sizing borders. This means the actual client area is slightly less than the window size, so a few rows and columns from the virtual buffer will be truncated at the top and right sides of the window.

C/C++ version

C++Builder version

Delphi version

Visual Basic version

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.