home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / SMALTALK / TEXTBOOK / AP26.ST (.txt) < prev    next >
Text File  |  1997-04-22  |  8KB  |  300 lines

  1.  
  2. 'Smalltalk Textbook Appendix 26'!
  3.  
  4.  
  5.  
  6.  
  7.  
  8. EngiGeometric subclass: #EngiBranch
  9.     instanceVariableNames: 'startVertex endVertex branchWeight '
  10.     classVariableNames: ''
  11.     poolDictionaries: ''
  12.     category: 'Engi-Graph'!
  13. EngiBranch comment:
  14. '
  15.  
  16. Engi 0.07 (24 March 1994)
  17. Copyright (C) 1994 by Atsushi Aoki
  18.  
  19. '!
  20.  
  21.  
  22. !EngiBranch methodsFor: 'testing'!
  23.  
  24. containsPoint: aPoint 
  25.     | dist |
  26.     dist := self class
  27.                 distance: aPoint
  28.                 from: self startPoint
  29.                 to: self endPoint.
  30.     ^dist < 3!
  31.  
  32. isBranch
  33.     ^true!
  34.  
  35. isVertex
  36.     ^false! !
  37.  
  38. !EngiBranch methodsFor: 'bounds accessing'!
  39.  
  40. computeBounds
  41.     | bounds margin expand |
  42.     bounds := (self startPoint corner: self startPoint)
  43.                 merge: (self endPoint corner: self endPoint).
  44.     margin := (self lineWidth / 2) asInteger.
  45.     expand := margin @ margin corner: margin + 1 @ (margin + 1).
  46.     bounds := bounds rounded expandedBy: expand.
  47.     ^bounds! !
  48.  
  49. !EngiBranch methodsFor: 'vertex accessing'!
  50.  
  51. endVertex
  52.     ^endVertex!
  53.  
  54. endVertex: aVertex 
  55.     endVertex := aVertex.
  56.     self flushBounds!
  57.  
  58. startVertex
  59.     ^startVertex!
  60.  
  61. startVertex: aVertex 
  62.     startVertex := aVertex.
  63.     self flushBounds! !
  64.  
  65. !EngiBranch methodsFor: 'weight accessing'!
  66.  
  67. branchWeight
  68.     ^branchWeight!
  69.  
  70. branchWeight: anObject 
  71.     branchWeight := anObject! !
  72.  
  73. !EngiBranch methodsFor: 'point accessing'!
  74.  
  75. endPoint
  76.     endVertex isNil ifTrue: [^Point zero].
  77.     ^endVertex bounds center!
  78.  
  79. startPoint
  80.     startVertex isNil ifTrue: [^Point zero].
  81.     ^startVertex bounds center! !
  82.  
  83. !EngiBranch methodsFor: 'transforming'!
  84.  
  85. rotatedBy: angleDegree 
  86.     self flushBounds!
  87.  
  88. scaledBy: scalePoint 
  89.     self flushBounds!
  90.  
  91. translatedBy: amountPoint 
  92.     self flushBounds! !
  93.  
  94. !EngiBranch methodsFor: 'displaying'!
  95.  
  96. displayFilledOn: graphicsContext at: aPoint 
  97.     ^self!
  98.  
  99. displayStrokedOn: graphicsContext at: aPoint 
  100.     | startPoint endPoint |
  101.     self strokeColor isNil ifFalse: [((self bounds translatedBy: aPoint)
  102.             intersects: graphicsContext clippingBounds)
  103.             ifTrue: 
  104.                 [startPoint := (self startPoint + aPoint) rounded.
  105.                 endPoint := (self endPoint + aPoint) rounded.
  106.                 graphicsContext paint: self strokeColor.
  107.                 graphicsContext lineWidth: self lineWidth.
  108.                 graphicsContext displayLineFrom: startPoint rounded to: endPoint]]! !
  109. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  110.  
  111. EngiBranch class
  112.     instanceVariableNames: ''!
  113.  
  114.  
  115. !EngiBranch class methodsFor: 'instance creation'!
  116.  
  117. new
  118.     ^super new initialize! !
  119.  
  120. !EngiBranch class methodsFor: 'examples'!
  121.  
  122. example1
  123.     "EngiBranch example1."
  124.  
  125.     | aBranch aVertex1 aVertex2 activeWindow activeSensor previousPoint currentPoint |
  126.     aBranch := EngiBranch new.
  127.     aVertex1 := EngiVertex new.
  128.     aVertex1 vertexPoint: 100 @ 100.
  129.     aVertex2 := EngiVertex new.
  130.     aVertex2 vertexPoint: 100 @ 100.
  131.     aBranch startVertex: aVertex1.
  132.     aBranch endVertex: aVertex2.
  133.     activeWindow := aBranch class activeWindow.
  134.     activeWindow clear.
  135.     activeSensor := activeWindow sensor.
  136.     previousPoint := nil.
  137.     [activeSensor anyButtonPressed]
  138.         whileFalse: 
  139.             [currentPoint := activeSensor cursorPoint.
  140.             previousPoint = currentPoint
  141.                 ifFalse: 
  142.                     [previousPoint := currentPoint.
  143.                     aVertex2 vertexPoint: currentPoint.
  144.                     aBranch strokeColor: aBranch class sampleColor.
  145.                     aBranch display]].
  146.     activeWindow display.
  147.     ^aBranch!
  148.  
  149. example2
  150.     "EngiBranch example2."
  151.  
  152.     | aBranch aVertex1 aVertex2 visualTransporter |
  153.     aBranch := EngiBranch new.
  154.     aBranch strokeColor: ColorValue red.
  155.     aBranch lineWidth: 3.
  156.     aVertex1 := EngiVertex new.
  157.     aVertex1 vertexPoint: 100 @ 100.
  158.     aVertex2 := EngiVertex new.
  159.     aVertex2 vertexPoint: 200 @ 200.
  160.     aBranch startVertex: aVertex1.
  161.     aBranch endVertex: aVertex2.
  162.     visualTransporter := EngiVisualTransporter load: aBranch.
  163.     visualTransporter
  164.         follow: [self activeSensor cursorPoint]
  165.         while: [self activeSensor noButtonPressed]
  166.         on: self activeGraphicsContext.
  167.     ^aBranch! !
  168.  
  169.  
  170.  
  171.  
  172.  
  173. EngiBranch subclass: #EngiBranchWithArrow
  174.     instanceVariableNames: ''
  175.     classVariableNames: ''
  176.     poolDictionaries: ''
  177.     category: 'Engi-Graph'!
  178. EngiBranchWithArrow comment:
  179. '
  180.  
  181. Engi 0.07 (24 March 1994)
  182. Copyright (C) 1994 by Atsushi Aoki
  183.  
  184. '!
  185.  
  186.  
  187. !EngiBranchWithArrow methodsFor: 'bounds accessing'!
  188.  
  189. computeBounds
  190.     | bounds margin expand |
  191.     bounds := (self startPoint corner: self startPoint)
  192.                 merge: (self endPoint corner: self endPoint).
  193.     margin := (self lineWidth / 2) asInteger.
  194.     expand := margin @ margin corner: margin + 1 @ (margin + 1).
  195.     bounds := bounds rounded expandedBy: expand.
  196.     bounds := bounds merge: self arrowBounds.
  197.     ^bounds! !
  198.  
  199. !EngiBranchWithArrow methodsFor: 'arrow accessing'!
  200.  
  201. arrowAngle
  202.     ^self class angleFrom: self startPoint to: self endPoint!
  203.  
  204. arrowBounds
  205.     | bounds margin expand |
  206.     bounds := nil.
  207.     self arrowPoints do: [:p | bounds isNil
  208.             ifTrue: [bounds := p corner: p]
  209.             ifFalse: [bounds := bounds merge: (p corner: p)]].
  210.     margin := (self lineWidth / 2) asInteger.
  211.     expand := margin @ margin corner: margin + 1 @ (margin + 1).
  212.     bounds := bounds rounded expandedBy: expand.
  213.     ^bounds!
  214.  
  215. arrowPoint
  216.     ^self startPoint + self endPoint / 2!
  217.  
  218. arrowPoints
  219.     | angle p0 p1 p2 p3 |
  220.     angle := self class angleFrom: self startPoint to: self endPoint.
  221.     p0 := self arrowPoint.
  222.     p1 := p0 + (self class rotate: 3 @ 0 by: angle).
  223.     p2 := p0 + (self class rotate: -3 @ 2 by: angle).
  224.     p3 := p0 + (self class rotate: -3 @ -2 by: angle).
  225.     ^Array
  226.         with: p1
  227.         with: p2
  228.         with: p3
  229.         with: p1! !
  230.  
  231. !EngiBranchWithArrow methodsFor: 'displaying'!
  232.  
  233. displayStrokedOn: graphicsContext at: aPoint 
  234.     | startPoint endPoint arrowPolyline |
  235.     self strokeColor isNil ifFalse: [((self bounds translatedBy: aPoint)
  236.             intersects: graphicsContext clippingBounds)
  237.             ifTrue: 
  238.                 [startPoint := (self startPoint + aPoint) rounded.
  239.                 endPoint := (self endPoint + aPoint) rounded.
  240.                 graphicsContext paint: self strokeColor.
  241.                 graphicsContext lineWidth: self lineWidth.
  242.                 graphicsContext displayLineFrom: startPoint rounded to: endPoint.
  243.                 arrowPolyline := self arrowPoints collect: [:p | p rounded].
  244.                 graphicsContext displayPolygon: arrowPolyline at: aPoint.
  245.                 graphicsContext displayPolyline: arrowPolyline at: aPoint]]! !
  246. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  247.  
  248. EngiBranchWithArrow class
  249.     instanceVariableNames: ''!
  250.  
  251.  
  252. !EngiBranchWithArrow class methodsFor: 'examples'!
  253.  
  254. example1
  255.     "EngiBranchWithArrow example1."
  256.  
  257.     | aBranch aVertex1 aVertex2 activeWindow activeSensor previousPoint currentPoint |
  258.     aBranch := EngiBranchWithArrow new.
  259.     aVertex1 := EngiVertex new.
  260.     aVertex1 vertexPoint: 100 @ 100.
  261.     aVertex2 := EngiVertex new.
  262.     aVertex2 vertexPoint: 100 @ 100.
  263.     aBranch startVertex: aVertex1.
  264.     aBranch endVertex: aVertex2.
  265.     activeWindow := aBranch class activeWindow.
  266.     activeWindow clear.
  267.     activeSensor := activeWindow sensor.
  268.     previousPoint := nil.
  269.     [activeSensor anyButtonPressed]
  270.         whileFalse: 
  271.             [currentPoint := activeSensor cursorPoint.
  272.             previousPoint = currentPoint
  273.                 ifFalse: 
  274.                     [previousPoint := currentPoint.
  275.                     aVertex2 vertexPoint: currentPoint.
  276.                     aBranch strokeColor: aBranch class sampleColor.
  277.                     aBranch display]].
  278.     activeWindow display.
  279.     ^aBranch!
  280.  
  281. example2
  282.     "EngiBranchWithArrow example2."
  283.  
  284.     | aBranch aVertex1 aVertex2 visualTransporter |
  285.     aBranch := EngiBranchWithArrow new.
  286.     aBranch strokeColor: ColorValue red.
  287.     aBranch lineWidth: 3.
  288.     aVertex1 := EngiVertex new.
  289.     aVertex1 vertexPoint: 100 @ 100.
  290.     aVertex2 := EngiVertex new.
  291.     aVertex2 vertexPoint: 200 @ 200.
  292.     aBranch startVertex: aVertex1.
  293.     aBranch endVertex: aVertex2.
  294.     visualTransporter := EngiVisualTransporter load: aBranch.
  295.     visualTransporter
  296.         follow: [self activeSensor cursorPoint]
  297.         while: [self activeSensor noButtonPressed]
  298.         on: self activeGraphicsContext.
  299.     ^aBranch! !
  300.