9-May-96

BASIC Programming

GRAPHICS


Introduction

Before graphics can be drawn on the screen, a graphics display mode must be selected. This is done with the SCREEN command, for example:

SCREEN 12 

This selects high resolution (640 x 480 pixels) graphics, with 16 colours.

The default coordinate system is not very nice: point (0,0) is in the top left hand corner of the screen, and the y-axis increases downwards (not like normal cartesian systems).

To set up your own coordinate system use the WINDOW command, for example:

WINDOW (0,0) - (1000,750) 

This sets the bottom left hand corner of the screen to be point (0,0), and the top right corner to be point (1000,750). As the screen is not physically square, it is useful to define a coordinate system which is in the same proportion as the screen (about 4:3).

Any coordinate system may be defined, to suit the task. To set up a coordinate system with (0,0) in the centre of the screen, and with x and y axes going from -1 to +1, the following command could be used:

WINDOW (-1,-1) - (1,1) 

Drawing a line

Lines may be drawn using the LINE command. Here is the syntax of the command:

LINE (x1,y1) - (x2,y2) 

(x1,y1) and (x2,y2) are coordinate pairs which define the start and end points of the line respectively. They use the coordinate system that has been set up by the WINDOW command. In the examples that follow, we will assume that a coordinate system has been set up using the following WINDOW command:

WINDOW (0,0) - (1000,750) 

To draw a line from the centre of the screen to the top right corner, the following could be used:

LINE (500,375) - (1000,750) 

There is a shorter form of the LINE command which can be used to draw connected straight lines, where each line segment is drawn from the end of the previous line segment. To draw a square using the shortened form of the LINE command:

LINE (300,300) - (400,300)
LINE - (400,400)
LINE - (300,400)
LINE - (300,300) 

This would draw a square of side length 100 units, with the lower left corner at point (300,300). The shortened form of the LINE statement only specifies the coordinate of the end of the line. Note that the hyphen '-' must still be included.

Drawing a circle

Circles are drawn using the CIRCLE command. Here is the syntax:

CIRCLE (xc,yc),r 

(xc,yc) represent the coordinates of the centre of the circle, and r is the radius.

To draw a circle centred on the screen with a radius of 100 units, the following command could be used:

CIRCLE (500,375),100 

Adding colour

The LINE and CIRCLE commands both allow the colour of the line to be specified. Colours are represented by numbers. In SCREEN 12 display mode, the following colours are available:

0Black8Grey
1Blue9Light Blue
2Green10Light Green
3Cyan11Light Cyan
4Red12Light Red
5Magenta13Light Magenta
6Brown14Yellow
7White15High Intensity White

A red LINE is specified like this:

LINE (50,100) - (200,250),4 

A green CIRCLE is specified like this:

CIRCLE (250,100),50,2 

Drawing boxes

Additional parameters can be given to the LINE command to make it draw boxes (rectangles). The following command will draw a blue outline box:

LINE (100,50) - (200,90),1,B

The corners of the box will be at (100,50) and (200,90). The following example will draw a magenta filled box:

LINE (330,115) - (400,320),5,BF

Example program

SCREEN 12
WINDOW (0,0) - (1000,750)
FOR N=1 TO 50
LINE (N*20,0) - (0,750-N*15)
NEXT N 

See if you can work out what the above program does before you run it.

  • Make the program draw the same pattern in the top right corner as well (you can do this by adding another LINE command inside the loop).
  • How about making the pattern in all four corners ?

Exercises

  1. Write a program which draws lots of random lines on the screen.
    Hints:
    RANDOMIZE TIMER initializes the random number generator.
    RND(1)*5 gives a random decimal number between 0 and 5.
  2. Make the lines appear in random colours.
  3. How many lines can be drawn in about 10 seconds ?
  4. Make the computer draw random circles in random colours.
  5. Draw lines in the bottom left quarter of the screen and circles in the top right corner of the screen.

Department Home Page
foxd@civl.port.ac.uk
beggdw@civl.port.ac.uk