home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / makedrawf / Doc / TutorialT < prev   
Encoding:
Text File  |  1996-12-05  |  19.4 KB  |  473 lines

  1. [[ Note: This text-only version of the tutorial was produced by hand from
  2.    the Impression version. It is therefore likely that there are lots of
  3.    infelicities in the formatting; in particular, font changes have been
  4.    lost. If a sentence doesn’t seem to make any sense, see whether it’s
  5.    any better if you pretend some of the words in it are in italics. :-) ]]
  6.  
  7.  
  8. A mkdrawf tutorial
  9. ==================
  10.  
  11. This is a fairly gentle introduction to mkdrawf, a program for creating
  12. drawfiles. If you don’t know what a drawfile is, don’t worry: you will soon.
  13. If you don’t know what a program is, perhaps you should read some other
  14. tutorials first. In fact, even if that’s the case you can probably get a fair
  15. way into this tutorial, since most of it consists of instructions saying “Put
  16. this into a file and do that with it, and see what happens”.
  17.  
  18. When you’ve worked through this tutorial, you should find most of the manual
  19. pretty easy going. Actually most of the manual is easy going anyway, but it
  20. suffers a bit from being intended as a reference as well as a tutorial; this
  21. document has no such ambitions, and is unashamedly incomplete; it even
  22. contains a few (minor) lies. If it disagrees with the manual, you know which
  23. to trust.
  24.  
  25.  
  26. Running mkdrawf
  27. ---------------
  28.  
  29. mkdrawf manufactures drawfiles out of ordinary text files. There are two ways
  30. to use it. Firstly, you can run it from the command line: entering a command
  31. like
  32.  
  33.   mkdrawf textfile drawfile
  34.  
  35. will process the file textfile and produce an output file drawfile. Secondly,
  36. you can use the Wimp application !Drawf, which allows you to drag a text file
  37. to its icon, whereupon it will run that file through mkdrawf and allow you to
  38. drag the resulting drawfile somewhere to save it. The icon for !Drawf looks
  39. like this: <<sorry...>>  .
  40.  
  41.  
  42.  
  43. (As you might guess from the look of the icon, !Drawf will also decode
  44. drawfiles for you, producing output suitable for handing to mkdrawf again.
  45. There is a separate manual for !Drawf which will tell you all about this, and
  46. more besides.)
  47.  
  48. From now on, I shall assume that you can do one of these two things. When I say “run mkdrawf on this file”, this means: either choose an output filename and
  49. use the command-line, or else drag the file to the !Drawf icon and put the
  50. resulting output somewhere.
  51.  
  52.  
  53. Running !Draw
  54. -------------
  55.  
  56. The easiest way to see the results of using mkdrawf is to view the files it
  57. produces using !Draw. This program comes with every Archimedes or Risc PC, so
  58. you should definitely have a copy; you can probably find it by clicking on an
  59. icon labelled Apps at the left-hand side of your icon bar.
  60.  
  61. If you haven’t used !Draw before, the first thing you should do is to run it
  62. and play around with it for a while. This should give you an idea of the sort
  63. of thing it can do.
  64.  
  65. A drawfile is a file which !Draw can understand. A drawfile consists
  66. basically of a number of objects (lines, curves, bits of text, that sort of
  67. thing) strung together. In some cases (have you used the Group option on the
  68. menu?) an object can contain a number of other objects. As this tutorial
  69. proceeds, you will learn rather more about drawfiles than you actually want
  70. to know.
  71.  
  72. Every time you run mkdrawf you should have a look at the output by either
  73. double-clicking on the file it produces (after dragging it to a directory
  74. display, if you are using !Drawf) or dragging the file to !Draw’s icon on the
  75. icon bar. (Not quite every time; if something goes wrong and you get lots of
  76. error messages, the resulting drawfile may be full of rubbish, or at any rate
  77. not full of what you wanted.)
  78.  
  79.  
  80. A very simple example
  81. ---------------------
  82.  
  83. Put the following into a file using your favourite text editor, run it
  84. through mkdrawf, and look at the result using !Draw:
  85.  
  86.   # This is a comment; mkdrawf will ignore it.
  87.   Path {
  88.     Move 100 100
  89.     Line 300 200
  90.   }
  91.  
  92. You can probably guess what the result of this will be before you try it.
  93. Your guess will almost certainly be correct. This simple example actually
  94. demonstrates quite a few things about mkdrawf, though… The first line is
  95. self-explanatory. The remaining lines describe a single object. An object is
  96. introduced by saying what sort of object it is; in this case, it is a path
  97. object. (The typical drawfile consists mostly of path objects.) Further
  98. details about the object are given within those braces {}; in this case, the
  99. path consists of a single line segment from (100,100) to (300,200).
  100. Coordinates are always given, as in this example, as pairs of numbers. The
  101. unit, by the way, is the point; the ambiguity here is unfortunate but seldom
  102. causes trouble in practice. A point is 1/72 of an inch; the spacing between
  103. vertical lines here is about 10 points.
  104.  
  105.  
  106. A slightly less simple example
  107. ------------------------------
  108.  
  109. As I already said, a drawfile typically contains several objects, one after
  110. another. This structure is represented in the obvious way in input to
  111. mkdrawf; namely, by putting one object description after another. Here’s an
  112. example, which (as usual) you should try.
  113.  
  114.   Path {
  115.     Move 100 100
  116.     Line 150 100
  117.   }
  118.   Path {
  119.     FillColour r220g0b0
  120.     Move 200 200
  121.     Line 300 200
  122.     Line 300 300
  123.     Close
  124.   }
  125.   Text {
  126.     Style Font "Trinity.Medium"
  127.     StartAt 200 300
  128.     Size 10 10
  129.     Text "Hello!"
  130.   }
  131.  
  132. This produces a drawfile containing three objects: two path objects and a
  133. text object. You can probably make a pretty good guess as to what this will
  134. look like, if I tell you that:
  135.  
  136.   • colours are given as RGB values, each component going from 0 (dark) to
  137.     255 (bright);
  138.   • Size 10 10 means “I want a 10-point font”;
  139.   • Trinity is the typeface in which this tutorial is printed.
  140.  
  141.  
  142. An easy exercise
  143. ----------------
  144.  
  145. Write a mkdrawf file which, when fed into mkdrawf and !Draw, produces the
  146. following picture. <<Sorry, again.>> You are not expected, of course, to
  147. produce the grid lines and the numbers labelling them (though you might be
  148. able to do something approximating to them), and you should not worry about
  149. the thicknesses of the lines. The grey colour inside the square is
  150. r119g119b119, I think; and the text is 20-point Trinity.Medium.Italic. When
  151. you’ve done this, check it (of course you won’t be able to check the scale)
  152. by running it through mkdrawf and !Draw.
  153.  
  154.  
  155. More about path objects
  156. -----------------------
  157.  
  158. You may have guessed from the previous picture (or you may have noticed while
  159. using !Draw) that all sorts of things we haven’t discussed yet are possible
  160. with path objects; for instance, the picture includes dashed lines, lines in
  161. colours other than black, and lines of different thicknesses. And there’s
  162. plenty more. Anyway, here is a mkdrawf file demonstrating some more features
  163. of path objects:
  164.  
  165.  Path {
  166.     Width 2        # all dimensions, including this, are in points
  167.     OutlineColour r0g0b255    # blue lines
  168.     Style { EndCap Triangular }    # !Draw will show you what this means
  169.     Move 100 100
  170.     Line 200 100
  171.     Curve  300 100  200 200  200 300    # a Bezier curve, ending at (200,300)
  172.     Line 200 400
  173.     Move 200 100        # a path can be made up of many subpaths
  174.     Line 200 300
  175.   }
  176.  
  177. What’s new here? The Width keyword allows you to say how thick you want a
  178. line to be. The width is, like all dimensions, given in points (1/72"). It’s
  179. a “diameter” rather than a “radius”. The Style keyword should be followed by
  180. some stuff in braces; there are several other things you can set in there.
  181. The OutlineColour keyword (so called in contrast to FillColour) determines
  182. the colour of lines and curves, as distinct from the filled-in space inside
  183. them. By the way, the usual 16-colour desktop palette doesn’t include a
  184. colour accurately matching r0g0b255, but !Draw will happily display the best
  185. approximation it can, and won’t throw away its information about exactly what
  186. colour you really wanted.
  187.  
  188. The line beginning Curve is more interesting. As well as straight lines, a
  189. path object can contain Bezier curves. A Bezier curve is described by giving
  190. its starting and ending points — in this case (200,100) and (200,300) — and
  191. two control points — in this case (300,100) and (200,200). The curve starts
  192. out from its starting point, heading towards its first control point. It
  193. curves around until eventually it reaches its ending point, from the
  194. direction of its second control point. Subject to these restrictions, the
  195. exact shape of the curve depends on how far away the control points are from
  196. their matching endpoints. Got that?
  197.  
  198. I think the best way to get some intuition for Bezier curves is: Go into
  199. !Draw, select the “open polygon” tool (the one at the very top of the
  200. toolbox), click somewhere, click somewhere else, and then click with the
  201. Adjust (right-hand) mouse button. At this point you should see a grey line
  202. with a square at each end. Now, click with Adjust on the square at whichever
  203. end of the line you clicked on second; the whole line should go red. Click
  204. with the Menu button; the menu you get should include an option “Change to
  205. curve”. Do this. Now you should see, as well as the two endpoints of the
  206. line, two other points in the middle of it. These are the control points of
  207. the Bezier curve you have just produced. Drag them around with the Adjust
  208. button and watch the shape of the curve change. (If you accidentally click
  209. somewhere you didn’t mean to and the coloured squares disappear, then select
  210. the line again using the “arrow” tool and hit control-E.)
  211.  
  212. No, really. You should actually do this, not just read about it. Otherwise
  213. you’ll never really understand Bezier curves, and they’re important.
  214.  
  215.  
  216. Doing things again and again and again
  217. --------------------------------------
  218.  
  219. There are several other sorts of object, but I think it’s time to look at
  220. some other features of mkdrawf before getting even further bogged down in the
  221. details of what they are and how to specify them. Here’s one of the problems
  222. that first got me working on writing this program. I wanted to be able to
  223. draw graphs of functions (sine, cosine, that sort of thing) and make them
  224. into drawfiles, so that I could add annotations, print them out and so on.
  225. Now, obviously we could do that by writing an enormously long mkdrawf
  226. “script” looking something like
  227.  
  228.   Path {
  229.     Move 100 400
  230.     Line 100.1 400.097
  231.     Line 100.2 400.193
  232.     ...blah blah blah...
  233.     Line 500 400
  234.   }
  235.  
  236. with hundreds or thousands of lines, but this would be horrible. But try
  237. feeding the following program into mkdrawf… (This introduces quite a lot of
  238. new ideas, so don’t worry if you can’t see what it does yet. Have a look at
  239. the drawfile it produces anyway.)
  240.  
  241.   Set $2Pi 6.28318530717959
  242.   Set $Factor Over 400 $2Pi
  243.   Path {
  244.     Width 1
  245.     Move 100 400
  246.     For $x0 0 200 {
  247.       Set $x Times $x0 Over $2Pi 200
  248.       Set $y Sin $x
  249.       Set $t Plus 100 Times $x $Factor
  250.       Set $u Plus 400 Times $y $Factor
  251.       Line $t $u
  252.     }
  253.     Line 500 400
  254.   }
  255.  
  256. OK. Let’s take this slowly, since there are lots of tricky things in it.
  257. Anything starting with a dollar sign is a variable. You can set the value of
  258. a variable to be anything at all (including colours like r123g234b11, strings
  259. like "Trinity.Medium", and other even stranger things), but most variables
  260. are used to contain numbers. So, the first line of the program says “Until
  261. further notice, any time you see $2Pi you should replace it with the number
  262. 6.2831853071959.”.
  263.  
  264. The second line introduces arithmetic. I’m afraid the syntax for this is
  265. horrible; its motivating principle is that considering the amount of effort
  266. I’ve put into the program as a whole, you’re lucky to get arithmetic at all
  267. and you have no right to complain if it doesn’t look very nice… More
  268. seriously, the principle is that you have to say what to do before saying
  269. what to do it with. So “2+2” violates this rule, because reading from left to
  270. right you see the first “2” before you know that you’re going to have to add
  271. it to something; to make mkdrawf’s life easier, you have to say Plus first.
  272. Sorry. Anyway, the second line divides 400 by $2Pi’s value, and puts the
  273. result into a variable called $Factor.
  274.  
  275. The next few lines are familiar: they start a path object, indicate the width
  276. of the line to be drawn, and move to the starting position.
  277.  
  278. What happens next is altogether new. The For line means, approximately, “Do
  279. everything inside the {} once with $x0 having the value 0, then again with it
  280. having the value 1, then … and finally with it having the value 199.” In
  281. other words, we add 1 to $x0’s value every time, and we give up when it
  282. reaches 200. So, that’s the significance of the magic numbers 0 and 200 on
  283. that line: start at 0, give up at 200.
  284.  
  285. If you have a look inside those braces, you’ll see that the net result is a
  286. lot of calculation and 200 Lines. I’ll let you work out exactly what the
  287. calculation is doing — it’s not very difficult, especially with the resulting
  288. drawfile in a !Draw window on your screen. (You did run it through mkdrawf,
  289. didn’t you?) Actually, it would be unkind to leave you with no help about the
  290. nasty arithmetic things. The first line inside the {}, for instance, sets $x
  291. to what in any civilised computer language would be called $x0*($2Pi/200).
  292.  
  293. Finally, we draw a line to the right ending point (we should be very near to
  294. it at the end of the For loop anyway), and that’s the end.
  295.  
  296. (Incidentally, you may be wondering: Why For rather than Repeat or ManyTimes
  297. or something? Answer: it’s sort-of traditional that this sort of construct is
  298. called a “for loop”. In the first computer languages to use the word “for”
  299. for this sort of thing, the syntax looked more like
  300.  
  301.   For x = 1 to 100 do blah blah blah end.
  302.  
  303. which you could read as “Do blah blah blah for x=1, then for x=2, and so on”.
  304. This at least makes some sort of sense. This sort of syntax lives on in BASIC
  305. and Pascal.)
  306.  
  307. Got all that? If not, don’t worry. You can always come back to it.
  308.  
  309.  
  310. Another exercise
  311. ----------------
  312.  
  313. Work out what the following does. It is intended to be put in the same
  314. mkdrawf input file as the sine-curve we just drew, but don’t try it until you
  315. think you know what will happen.
  316.  
  317.   Path {
  318.     Width 0    # this means: as thin as possible
  319.     Move 100 400
  320.     Line 500 400
  321.     Move 100 Minus 400 $Factor
  322.     Line 100 Plus 400 $Factor
  323.     # every pi/4 units:
  324.     For $n 0 9 {
  325.       Set $x Times $n Over $2Pi 8
  326.       Set $t Plus 100 Times $x $Factor
  327.       Move $t 390
  328.       Line $t 410
  329.     }
  330.   }
  331.  
  332.  
  333. And another exercise
  334. --------------------
  335.  
  336. You should now be able to do this: Produce some squared paper, with lines as
  337. thin as possible. The lines should be spaced at intervals of 10 points, and
  338. occupy the rectangle whose bottom-left and top-right corners are (100,100)
  339. and (500,500).
  340.  
  341. You will need two loops. I recommend, as a matter of style, having one object
  342. containing the horizontal lines and one containing the vertical lines. Be
  343. very careful about the numbers in your Fors; remember that the second number
  344. is an exclusive rather than an inclusive limit. (Actually you don’t need two
  345. loops. As another exercise, work out a way of doing it with one. Don’t bother
  346. to implement this.)
  347.  
  348.  
  349. Macros
  350. ------
  351.  
  352. Consider the following situation. You want to draw a diagram in which a
  353. number of points are marked with little crosses, thus: . If there are, say,
  354. 100 points then this could mean an awful lot of typing. You can save a lot of
  355. effort by using a macro; what this means is best illustrated by an example.
  356.  
  357.   Define Point {
  358.     Path {
  359.       Width 0.3
  360.       Move Minus %x 2 %y  RLine 4 0
  361.       Move %x Minus %y 2  RLine 0 4
  362.     }
  363.   }
  364.   Point { %x 100 %y 200 }
  365.   Point { %x 200 %y 300 }
  366.   Point { %x 123 %y 321 }
  367.  
  368. There are three new features here. The first, trivial, one is the use of the
  369. RLine keyword, which is just a labour-saving device; the same result could
  370. have been produced by
  371.  
  372.     Move Minus %x 2 %y  Line Plus %x 2 %y
  373.     Move %x Minus %y 2  Line %x Plus %y 2.
  374.  
  375. There are also RMove and RCurve keywords.
  376.  
  377. The second feature is the use of a macro. The first 7 lines say: Whenever
  378. Point appears, replace it with the stuff in lines 2–6, making certain
  379. changes. The “certain changes” are the third feature: the first time the
  380. macro is “invoked” (on line 8), “%x” will be replaced everywhere by “100” and
  381. “%y” by “200”. You can probably guess what will happen the other two times.
  382. %x and %y are called “parameters” of the macro. You can find out much more
  383. about all this by reading the manual.
  384.  
  385. I’ve never worked out why the word “macro” is used for this sort of thing.
  386.  
  387.  
  388. Ellipses and rectangles
  389. -----------------------
  390.  
  391. The “ellipse” and “rectangle” tools of !Draw don’t actually correspond to
  392. special kinds of object. An ellipse or rectangle is just a path which happens
  393. to be the right shape. One consequence of this is that ellipses aren’t really
  394. ellipses, but close approximations by Bezier curves. This is a pain, since
  395. ellipses (especially circles) are often what you want, and finding a sequence
  396. of Bezier curves that approximates a circle well is not entirely trivial.
  397.  
  398. Fortunately I’ve already done the work for you. In the manual you will find,
  399. as part of one of the example programs, a macro which does just that,
  400. together with a couple of examples of its use. Incidentally, reading through
  401. it and checking you understand what’s going on (apart from the choice of the
  402. magic value for %t, whose justification is half a page of mathematical
  403. scribbling) might be a good idea.
  404.  
  405.  
  406. Conditionals
  407. ------------
  408.  
  409. You’re preparing a graph to show your boss, illustrating something terribly
  410. important. The graph consists of lots of points plotted with little crosses,
  411. and you want some of the points — the ones showing unusual results — plotted
  412. in red. To be more precise, let’s suppose that a y-coordinate of 300
  413. or more indicates something wrong, and you want those points plotted in red.
  414. You could define a RedPoint macro and a BlackPoint macro, and in fact if the
  415. mkdrawf file in question is being produced by a computer program that’s
  416. probably the way to do it. But if you’re doing it by hand, you might find the
  417. extra typing (Red and Black) annoying, and you might not be entirely
  418. confident of your ability to make no mistakes. So, instead…
  419.  
  420.   Define Point {
  421.     Path {
  422.       Width 0.3
  423.       OutlineColour
  424.         IfLess Minus 0 %y -300 r255g0b0
  425.         Else                   r0g0b0
  426.         EndIf
  427.       Move Minus %x 2 %y  RLine 4 0
  428.       Move %x Minus %y 2  RLine 0 4
  429.     }
  430.   }
  431.   Point { %x 100 %y 200 }    # this will be in black
  432.   Point { %x 200 %y 300 }    # so will this
  433.   Point { %x 123 %y 321 }    # but this will be in red
  434.  
  435. You can have more complicated conditions than this, of course, provided you
  436. can massage them into the form “so-and-so is less than such-and-such”, or
  437. “so-and-so is equal to such-and-such”, for which you use IfEqual. You should
  438. be warned that arithmetic involving anything other than integers is likely to
  439. be imprecise, so IfEqual may not behave the way you expect it to. For
  440. instance, if you do
  441.  
  442.   Define $Pi 3.141592653589
  443.   IfEqual Sin $Pi 0
  444.     blah
  445.   EndIf
  446.  
  447. you should not rely on blah being done. There is another kind of If, called
  448. IfExists; see the “arcs of circles” thing in the manual for an example of its
  449. use.
  450.  
  451.  
  452. And yet another exercise
  453. ------------------------
  454.  
  455. Write a mkdrawf program that produces 200 random points spread uniformly over
  456. the square whose bottom left and top right corners are at (100,100) and
  457. (200,200), and plots each one with a cross as discussed above, coloured black
  458. if it’s inside the circle that touches all four sides of the square and red
  459. if it’s outside it. (The circle in question has centre (150,150) and radius
  460. 50. You may colour points on its circumference either red or black; I don’t
  461. mind.)
  462.  
  463.  
  464. That’s all, folks...
  465. --------------------
  466.  
  467. At this point you have seen most of the tricky things about mkdrawf, and you
  468. should now read the manual. If you’ve done all the exercises, this should be
  469. a piece of cake.
  470.  
  471.  
  472.  
  473.