How to draw a square

After seeing some Forth basics, it's time to see what particular functions are offered by PF in terms of visual. The visual part we're interested in at the moment is OpenGl, in order to tell pf we're going to work with this system you need to load the OpenGl PF plugin. From this point you will be able to interact with the PF console and pass some drawing commands:

Start pf in a terminal, then:

load-opengl                   # load the OpenGl PF plugin
400 300 display               # initialize a 400x300 window
2d                            # set the environment for 2D
drawing                       # start the drawing system

Having typed these commands, now PF is ready to receive orders and interpret them in a two dimensional drawing environment.

As a result:

1. square

will display a square of size 1. Sizes are relative to the window height, as a consequence a square of size 1. will occupy the whole vertical space of the window.

If you type:

0.5 square

and expect to see a half-sized square you will be disapointed. Remember that you are working in a drawing environment, which means elements are drawn on top of eachother. To see a smaller square you need to first clear the screen:

clearscreen
0.5 square

If you want to overwrite this square with a red one set the new color with the rgb word:

1 0 0 rgb                     # set rgb to 1,0,0 = red
0.5 square

Creating a square of size 1 right after will render a red square as well. To change the color of the next rendered object you need to define its rgb value before.

1 0 0 rgb 1. square
0 1 0 rgb 0.6 square
0 0 1 rgb 0.3 square

Surprisingly enough squares can be placed in other places than the center. To do so the "transx" and "transy" words can be used ("transz" will have no effect here as we've asked PF to prepare a 2D environment).

Type this:

128 128 display
clearscreen
1 0 1 rgb
2. square
0 0 0 rgb
0.2 transy -0.2 transx 0.2 square
0.4 transx 0.2 square
1 1 1 rgb
-0.4 transy 0.2 square
-0.2 transx 0.2 square
-0.2 transx 0.2 square
0.5 transy -0.1 transx 0.1 square
0.4 transx 0.1 square

Note: n transx and n transy combos perform their translation starting from the previous coordinate on the x or y axis! This is a relative translation of the element and not an absolute positioning. If you create multiple squares without using 'clearscreen' to reset everything, your squares could end up outside of your window dimensions!

Attachments