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