turtle.tst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. % Demonstrations file for the REDUCE implementation of Turtle Graphics
  2. % Caroline Cotter, ZIB,Berlin, 1998.
  3. in "$reduce/packages/plot/turtle.red"$
  4. on rounded;
  5. on demo;
  6. %1) Draw 36 rays of length 100
  7. %(Taken from MapleTech - Maple in Mathematics and the Sciences,
  8. % Special Issue 1994)
  9. draw {for i:=1:36 collect
  10. {setheading(i*10), forward 100, back 100} };
  11. %2) Draw a "fan" of 36 straight spikes.
  12. draw {for i:=1:36 collect
  13. {setheading(i*10), forward 100, back 95} };
  14. %3) Draw a "fan" of 36 curved rays.
  15. draw {for i:=1:36 collect
  16. {setheading(i*10), forward 20, turnleft 20, forward 20, turnleft 20,
  17. forward 20, turnleft 20, forward 20, turnleft 20, forward 20,
  18. back 20, turnright 20, back 20, turnright 20, back 20,
  19. turnright 20, back 20, turnright 20, back 18} };
  20. %4) Draw 12 regular polygons with 12 sides of length 40,each polygon
  21. % forming an angle
  22. % of 360/n degrees with the previous one.
  23. %(Taken from MapleTech - Maple in Mathematics and the Sciences,
  24. % Special Issue 1994)
  25. draw {for i:=1:12 collect
  26. {turnleft(30), for j:=1:12 collect
  27. {forward 40, turnleft(30)}} };
  28. %5) A "peak" pattern - an example of a recursive procedure.
  29. <<
  30. procedure peak(r);
  31. begin;
  32. return for i:=0:r collect
  33. {move{x_coord+5,y_coord-10}, move{x_coord+10,y_coord+60},
  34. move{x_coord+10,y_coord-60}, move{x_coord+5,y_coord+10}};
  35. end;
  36. draw {home(), peak(3)} >>;
  37. %This procedure can then be part of a longer chain of commands:
  38. draw {home(), move{5,50}, peak(3), move{x_coord+10,-100},
  39. peak(2), move{x_coord+10,0}};
  40. %6) Write a recursive procedure which draws "trees" such that every branch
  41. % is half the
  42. % length of the previous branch.
  43. %(Taken from MapleTech - Maple in Mathematics and the Sciences,
  44. % Special Issue 1994)
  45. <<
  46. procedure tree(a,b); %Here: a is the start length, b is the number of levels
  47. begin;
  48. return if fixp b and b>0 %checking b is a positive integer
  49. then {turnleft(45), forward a, tree(a/2,b-1),
  50. back a, turnright(90), forward a, tree(a/2,b-1),
  51. back a, turnleft(45)}
  52. else {x_coord,y_coord}; %default: Turtle stays still
  53. end;
  54. draw {home(), tree(130,7)} >>;
  55. %This can be rotated so that the tree grows upwards:
  56. draw {home(), setheading(90), tree(130,7)};
  57. %7) A 36-point star.
  58. draw {home(), for i:=1:36 collect
  59. {turnleft(10), forward 100, turnleft(10), back 100} };
  60. %8) Draw 100 equilateral triangles with the leading points equally spaced
  61. % on a circular path.
  62. draw {home(), for i:=1:100 collect
  63. {forward 150, turnright(60), back(150),
  64. turnright(60), forward 150, setheading(i*3.6)} };
  65. %9) Two or more graphs can be drawn together (this is easier if the graphs
  66. % are named).Here we show graphs 4 and 8 on top of one another:
  67. <<
  68. gr4:={home(), for i:=1:12 collect
  69. {turnleft(30), for j:=1:12 collect
  70. {forward 40, turnleft(30)}} }$
  71. gr8:={home(), for i:=1:100 collect
  72. {forward 150, turnright(60), back(150),
  73. turnright(60), forward 150, setheading(i*3.6)} }$
  74. plot(gr4,gr8) >>;
  75. off rounded;
  76. end;