Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
22

Drawing a chart

Introduction

You have a list of numbers. This section turns it into something you can look at - which is most of what a home computer was bought for in 1984, once the games had been played.

It is also the first section that introduces nothing new. Everything in it you already have; what is new is fitting the pieces together so they do not fight.

One bar

Follow it round. Up by the height. Turn right, so you are facing across, and go twenty - that is the top of the bar. Turn right again and come back down the same height. Then turn 180.

Where does that leave the turtle? Twenty to the right of where it started, on the baseline, facing up - which is exactly how it started. That is the whole design. A shape that puts the turtle back into its starting stance can be repeated without anything in between.

Notice the bar has three sides, not four. The fourth is the baseline, and the next bar's left edge is this bar's right edge.

Every bar

This is the list walk from S15, with bar where the printing used to be. It can be this short only because bar leaves the turtle ready.

Somewhere to start

The turtle begins in the middle of the screen, which would put half your chart off the left edge. So move it, pen up, before drawing anything:

setpos takes an [x y] pair and goes there. It does not turn the turtle - it arrives facing whichever way it was already facing, which after cs is up. That matters: if it turned to face its destination, every bar would come out sideways.

ht hides the turtle so the finished chart has no arrow sitting in it.

The code

Starting from

A fresh boot, at the ? prompt.

What you should see

six bars in the proportions of the list

Six bars, in the order you gave them, twenty wide, each as tall as its number.

Putting a title on it

The drawing area and the text you type are the same forty columns by twenty-four lines, so setcursor will put words on the picture:

There is a catch, and it will bite you the first time. The prompt goes wherever you put the cursor, so the next thing you type starts at the top of the screen and walks down over your chart. Send it back afterwards:

Line 23 is the bottom, inside the four-line text window, which is where the prompt lives normally. With those two lines added to the end of histogram:

the same chart with a title, and the prompt back at the bottom

Change one thing

  • Put a 0 in the list: histogram [40 0 65]. You get a gap. Work out from bar why that is, rather than from the picture.
  • histogram []. What clears, what draws, and which line decided?
  • Change fd 20 in bar to fd 40, and give it three values. Then try nine values without changing it back. Where does the chart go, and does Dr Logo complain?
  • Take the setcursor [0 23] off the end and type anything. Now you know what it was for.

When it goes wrong

Symptom Cause
A staircase or a spiral instead of a row bar is not returning the turtle to the baseline facing up. Count the turns - they must add to 360.
Half the chart is off the left of the screen No setpos, so it started in the middle.
The bars come out sideways A seth or a turn between setpos and the drawing. setpos on its own does not turn the turtle.
The chart gets typed over setcursor left the prompt in the graphics area. Put setcursor [0 23] at the end.
A bar runs off the top The value is bigger than the screen. Lines are clipped without an error - you will not be told.

Summary

A shape that leaves the turtle where the next one needs it can be repeated without any repositioning, and that turns "draw a chart" into a four-line list walk. setpos moves without turning. setcursor puts text anywhere on the screen including over a drawing - and takes the prompt with it, so send it back.

Next

S23, curves from straight lines - repeat, and what a circle really is.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.