Light Pen

Single pixel resolution for light pens is now possible with our resolution revolution!

Volume 2

Number 1

March 1984

Light pen breakthrough!

Pixel resolution is possible

By MIKE COOK

EVERYONE knows that light pens cannot draw lines as fine as one pixel. Fortunately no-one has told the BBC Micro that!

This month we present a program which used in conjunction with The Micro User light pen finally achieves pixel resolution.

The pen itself was described in the October 1983 issue of The Micro User and a painting program was given the following month. This simulated the action of a paint brush and produced broad and flowing strokes.

Some artistic endeavours however, are more suited to a clean-cut graphical presentation and so the Draughtsman program has been produced.

The basic light pen is only capable of character resolution — that is, the computer can only sense the position of the pen to the nearest printing character. So in order to achieve pixel resolution we have to resort to some software trickery.

Those of you who have used the painting program will have noticed a small offset between the pen's position and the paint on the screen.

This is naturally compensated for by the brain, and leads to no difficulty in use. However, to coax pixel resolution out of the pen we can use this ability of the brain to good effect.

Consider Figure I. This shows one character position being pointed at by a light pen. If the pixel point we are interested in is in this square then it is not changed.


Figure I: Movement of the target pixel

However if it is in one of the adjacent squares (shown shaded) it is moved one pixel position towards the pen.

If the point of interest is outside the immediate area it is moved much more rapidly towards the pen. In practice this means that the point is teased into the correct position, and due to the cleverness of the human brain is quite naturally achieved.

As we now have a basic method of defining a pixel's position we are able to build the software around this.

Obviously a freehand sketch using the pen would be difficult to control. But as a draughtsman is concerned with straight lines we can use rubber banding techniques to position our line exactly.

When it is in position the press of a key will fix it permanently on the screen.

As well as rubber-banding single lines, we can also use triangles and rectangles and there is a routine for coping with circles.

When running the program the screen will go white to allow the pen to pick up light Nothing will happen until one of the modes is entered.

To do this type the letter for the appropriate mode. For example let's draw a straight line by pressing the L key.

The top line of the display should now say LINE. Take up the light pen and you should see a single lit pixel following the pen.

When it is in the correct position for the start of the line press any key. When the pen is moved a line will be drawn from this point to the pen.

This line will move following the pen and behaving like a rubber band on the screen.

When the end position of the line is at the correct place press the Return key and you will be back in the command mode, ready to draw another shape.

If you want to carry on drawing lines press the space bar instead of the Return key and you will stay in the line mode.

If you want the start of the new line to be the same point as the end of the old line then instead of the space bar press J for join.

The triangle mode works in a similar manner with first a line being rubber-banded and then the full triangle. In this mode the J key will join up the new triangle to the last side of the old one.

When drawing a rectangle the first point will fix one corner and the second point will fix the opposite corner. The join key will join the next rectangle to the last corner of the previous one.

When selecting the polygon mode you will be asked how many sides the polygon is to have. If you require a circle then a large number like 40 should be used.

In this mode the radius is defined by rubber banding but this radius line disappears when the polygon is drawn.

If the join option is used the new polygon will be drawn with the same centre point as the last one.

You can also change the colour of the lines by pressing the C key. This will cycle through the three colours available in Mode 1.

These are set up in line 90 and can be changed to suit your own preferences.

When you have finished your masterpiece the screen can be saved as a file by typing S. You will be asked to provide a file name and the memory locations that make up the screen will be saved.

For those of you with discs this file will be placed in the directory S for screen dump.

This will reduce the confusion between a screen dump file and a program. If you have tape then the characters S. will be added to the beginning of the file name.

The program also allows you to load a previously dumped file back to the screen to be worked on further. This is done by typing L.

Hints on typing in the program: Line 80 defines a text window of one line at the top of the screen. This line is best left out until all the typing errors are corrected as any error messages will scroll off the top before you can read them.

There are some variables that start with the LETTER O such as OX% (Old X). Do not confuse these with the number 0.

In line 300 the space between the quote marks and the number is vital. You will get an error message if it is left out.

Unfortunately the error message is not all that helpful. In various lines (such as 150) note there is no space between the quote marks.