We started with shapes like rectangles and ellipses simply because these are more interesting graphical objects. However, the most basic graphical objects are of course points and lines. Lines are drawn with two commands: MoveTo and LineTo. Both take two arguments which as you might expect are simply X and Y coordinates. MoveTo sets the position of the pen and LineTo actually draws a line. To draw a point, simply draw a line of length 1. Each call to LineTo updates the position of the pen so MoveTo only needs to be called once unless one needs to move the pen without drawing a line. The type of line drawn is determined by the SetPen procedure which takes one argument Color and 2 optional arguments Width and Style. Color and Width are fairly self-explanatory. Style is an enumerated type (enumerated types are discussed in detail later) which may be psSolid, psDash, psDot, psDashDot, psDashDotDot, or psClear. psSolid is the most common and it draws simply a solid line. The dash and dot work only for pen widths of 1. Pen styles are declared in the native Graphics unit so you will need to add it to the uses clause (in addition to FTGraphics) if you want to use something other than psSolid.
Excercise 1-2. Write code for the following problems in the Workspace template starting with the basic code for graphics:
program Workspace;
uses FTGraphics;
procedure Initialize;
begin
end;
procedure OnEnterFrame;
begin
end;
begin
RunGraphicalApp(@Initialize,@OnEnterFrame);
end.
Draw a triangle that has verticies at (0,0), (5,5), and (0,5) using a lime-colored pen of width 2.
We can use SetPen(clLime,2) and then a single call to MoveTo(0,0) followed by three LineTo's (don't forget to close the bottom of the triangle with a LineTo back to (0,0)). You can use Ctrl+G to toggle the grid and see that the triangle is at the correct location.
Draw a trapezoid that has a top of length 2 grid units and a bottom of length 5 grid units. Use a dotted red line. Hint: Use the grid with a blank screen to determine what coordinates you need to use. Don't forget to add Graphics to the uses clause for the dotted pen style.
We did not specify where the trapezoid should be, so any set of coordinates that results in a top of length 2 and a bottom of length 5 will do.
In addition to drawing frames of shapes with lines, closed shapes can be filled using the FloodFill command. FloodFill takes 5 arguments: X, Y, FillColor, FillType, and ControlColor. If you are familiar with common paint programs, FloodFill is identical to the "Paint Bucket Spill" icon: it fills everything starting at the point (X,Y) until reaching some boundary with the color specified in FillColor. If FillType is ffSurface, then FloodFill fills all pixels matching the color ControlColor. If FillType is ffBorder, then FloodFill fills everything until it reaches pixels that have the color ControlColor.
Excercise 1-3. Write code for the following problems in the Workspace template.
Draw a triangle that has verticies at (0,0), (5,5), and (0,5) using a lime-colored pen of width 2 and fill it with red.
Use the triangle code from the exercises above. After the triangle is drawn, pick any coordinate inside the triangle as the initial FloodFill point. We'll use (4,3). The border of the triangle is clLime so we can use that as the ControlColor if FillType is ffBorder. The FillColor is of course clRed.
Do the same as above but this time fill everything except the triangle with red.
Simply choose a point outside the triangle such as (3,4) instead of (4,3).
Since filled shapes are quite common, the FTGraphics library also has procedures called FramePolygon and FillPolygon for empty and filled polygons respectively. Points are passed in the form of
vectors. A vector is simply a quantity (like a coordinate pair) that is completely specified by
elements where
is the dimension of the space. In other words, in 2D graphics, a vector has 2 elements (X and Y). If we were in 3D space a vector would have 3 (X, Y, and Z). In terms of code, a 2D vector is implemented in the TVector2D type which is a record with fields X and Y. The FTGraphics library also provides a method for convenience called Vector which takes two real numbers and returns a TVector2D record. The following code performs the equivalent graphics operations as the filled triangle from above:
procedure OnEnterFrame;
begin
SetPen(clLime,2);
FillPolygon(
[Vector(0,0),
Vector(5,5),
Vector(5,0)],clLime,clRed);
end;