home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / prelude / pen < prev    next >
Encoding:
Text File  |  1987-12-30  |  2.6 KB  |  118 lines

  1. "
  2.      the following use the primitives interfacing to the plot(3)
  3.      routines
  4. "
  5.  
  6. " pen - a simple drawing instrument "
  7. Class Pen
  8. | x y up direction |
  9. [
  10.      new
  11.           self up.
  12.           self direction: 0.0.
  13.           self goTo: 100 @ 100.
  14. |
  15.      circleRadius: rad
  16.           <primitive 174 x y rad>
  17. |
  18.      direction
  19.           ^ direction
  20. |
  21.      direction: radians
  22.           direction <- radians
  23. |
  24.      down
  25.           up <- false.
  26. |
  27.      erase     
  28.           <primitive 170>
  29. |
  30.      extent: lowerLeft to: upperRight
  31.           <primitive 176 (lowerLeft x) (lowerLeft y)
  32.                (upperRight x) (upperRight y)>
  33. |
  34.      go: anAmount        | newx newy |
  35.           newx <- (direction radians sin * anAmount) rounded + x.
  36.           newy <- (direction radians cos * anAmount) rounded + y.
  37.           self goTo: newx @ newy
  38. |
  39.      goTo: aPoint
  40.           up ifFalse: [<primitive 177 x y (aPoint x) (aPoint y)>].
  41.           x <- aPoint x.
  42.           y <- aPoint y.
  43. |
  44.      isUp
  45.           ^ up
  46. |
  47.      location
  48.           ^ x @ y
  49. |
  50.      turn: radians
  51.           direction <- direction + radians
  52. |
  53.      up
  54.           up <- true.
  55. ]
  56.  
  57. " penSave - a way to save the drawings made by a pen "
  58. Class PenSave  :Pen
  59. | saveForm |
  60. [
  61.      setForm: aForm
  62.           saveForm <- aForm
  63. |
  64.      goTo: aPoint
  65.           (self isUp)
  66.                ifTrue: [ super goTo: aPoint ]
  67.                ifFalse: [ saveForm add: self location to: aPoint.
  68.                          self up.
  69.                          super goTo: aPoint.
  70.                          self down ]
  71. ]
  72.  
  73. " Form - a collection of lines "
  74. Class Form
  75. | lines |
  76. [
  77.      new
  78.           lines <- Bag new
  79. |
  80.      add: startingPoint to: endingPoint
  81.           lines add: ( Point new ;
  82.                     x: startingPoint ;
  83.                     y: endingPoint )
  84. |
  85.      with: aPen displayAt: location     | xOffset yOffset sPoint ePoint |
  86.           xOffset <- location x.
  87.           yOffset <- location y.
  88.           lines do: [:pair |
  89.                sPoint <- pair x.
  90.                ePoint <- pair y.
  91.                aPen up.
  92.                aPen goTo: 
  93.                     (sPoint x + xOffset) @ (sPoint y + yOffset).
  94.                aPen down.
  95.                aPen goTo: 
  96.                     (ePoint x + xOffset) @ (ePoint y + yOffset).
  97.                ].
  98. ]
  99. "
  100.      pen show - show off some of the capabilities of pens.
  101. "
  102. Class PenShow
  103. | bic |
  104. [
  105.      withPen: aPen
  106.           bic <- aPen
  107. |
  108.      poly: nSides length: length
  109.  
  110.           nSides timesRepeat:
  111.                [ bic go: length ;
  112.                     turn: 2 pi / nSides ]
  113. |
  114.      spiral: n angle: a
  115.           ( 1 to: n ) do:
  116.                [:i | bic go: i ; turn: a]
  117. ]
  118.