Simple graphic output
edorfaus at xepher.net
Mon Oct 21 15:19:24 EDT 2013
On 10/21/2013 01:45 AM, mark adrian bell wrote:
> To practice my Perl programming, I've been writing a simple
> turtle-graphics program. It outputs a list of points like this one:
> 0 0
> 0 1
> 1 1
> 1 0
> 0 0
IIUC, this is a turtle going in a "circle", right? Moving down, left,
up, then right, ending up where it started?
And what you want is something that takes these coordinates, and puts a
visible dot in the corresponding spot on the screen? (Since the turtle
moves one step at a time, that shouldn't leave any holes in the line.)
> Can you recommend a simple program or library to display this?
I started thinking that, if this isn't already easily available, it
should be fairly trivial to implement. Most graphics libraries give you
a canvas to draw on, and functions to draw on it with (e.g. lineTo(x,y)).
Then I remembered a game I had a long time ago, called nibbles, and was
inspired to create something simple that uses the same "graphics" mode
as that did. The quotes are because it technically uses the text mode of
the console, but puts colored rectangles on it as if it was a
low-resolution graphics mode.
You can find my implementation attached; it reads the coordinates from
standard input (so just pipe your program's output to it) and plots them
on the screen in white, with an @ showing where the turtle is.
Resolution on the Ben's screen, at least when run under ash, is 53x21.
I wouldn't recommend this solution for all use cases, especially not
those that need a high resolution or a high speed (it's implemented as a
bash shell script using ANSI escape codes), but it should allow you to
get started with the basics at least. :)
To stop it from exiting (clearing the screen) as soon as the standard
input is closed (your program ends), I added a short delay at the end,
so you can see the drawing for a couple of seconds. If you want to
adjust this delay, simply give the new delay (in seconds) as the first
parameter to the program.
... gah, I couldn't stop myself, I added color support too. Simply
append a third "coordinate" to tell it which color to use for that point
(and all following points until a new color is given).
The available colors are numbered 0 to 7 (inclusive), for details see
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 987 bytes
Desc: not available
More information about the discussion