home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
LANGUAGE
/
SMALTALK
/
TEXTBOOK
/
AP26.ST
(
.txt
)
< prev
next >
Wrap
Text File
|
1997-04-22
|
8KB
|
300 lines
'Smalltalk Textbook Appendix 26'!
EngiGeometric subclass: #EngiBranch
instanceVariableNames: 'startVertex endVertex branchWeight '
classVariableNames: ''
poolDictionaries: ''
category: 'Engi-Graph'!
EngiBranch comment:
'
Engi 0.07 (24 March 1994)
Copyright (C) 1994 by Atsushi Aoki
'!
!EngiBranch methodsFor: 'testing'!
containsPoint: aPoint
| dist |
dist := self class
distance: aPoint
from: self startPoint
to: self endPoint.
^dist < 3!
isBranch
^true!
isVertex
^false! !
!EngiBranch methodsFor: 'bounds accessing'!
computeBounds
| bounds margin expand |
bounds := (self startPoint corner: self startPoint)
merge: (self endPoint corner: self endPoint).
margin := (self lineWidth / 2) asInteger.
expand := margin @ margin corner: margin + 1 @ (margin + 1).
bounds := bounds rounded expandedBy: expand.
^bounds! !
!EngiBranch methodsFor: 'vertex accessing'!
endVertex
^endVertex!
endVertex: aVertex
endVertex := aVertex.
self flushBounds!
startVertex
^startVertex!
startVertex: aVertex
startVertex := aVertex.
self flushBounds! !
!EngiBranch methodsFor: 'weight accessing'!
branchWeight
^branchWeight!
branchWeight: anObject
branchWeight := anObject! !
!EngiBranch methodsFor: 'point accessing'!
endPoint
endVertex isNil ifTrue: [^Point zero].
^endVertex bounds center!
startPoint
startVertex isNil ifTrue: [^Point zero].
^startVertex bounds center! !
!EngiBranch methodsFor: 'transforming'!
rotatedBy: angleDegree
self flushBounds!
scaledBy: scalePoint
self flushBounds!
translatedBy: amountPoint
self flushBounds! !
!EngiBranch methodsFor: 'displaying'!
displayFilledOn: graphicsContext at: aPoint
^self!
displayStrokedOn: graphicsContext at: aPoint
| startPoint endPoint |
self strokeColor isNil ifFalse: [((self bounds translatedBy: aPoint)
intersects: graphicsContext clippingBounds)
ifTrue:
[startPoint := (self startPoint + aPoint) rounded.
endPoint := (self endPoint + aPoint) rounded.
graphicsContext paint: self strokeColor.
graphicsContext lineWidth: self lineWidth.
graphicsContext displayLineFrom: startPoint rounded to: endPoint]]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
EngiBranch class
instanceVariableNames: ''!
!EngiBranch class methodsFor: 'instance creation'!
new
^super new initialize! !
!EngiBranch class methodsFor: 'examples'!
example1
"EngiBranch example1."
| aBranch aVertex1 aVertex2 activeWindow activeSensor previousPoint currentPoint |
aBranch := EngiBranch new.
aVertex1 := EngiVertex new.
aVertex1 vertexPoint: 100 @ 100.
aVertex2 := EngiVertex new.
aVertex2 vertexPoint: 100 @ 100.
aBranch startVertex: aVertex1.
aBranch endVertex: aVertex2.
activeWindow := aBranch class activeWindow.
activeWindow clear.
activeSensor := activeWindow sensor.
previousPoint := nil.
[activeSensor anyButtonPressed]
whileFalse:
[currentPoint := activeSensor cursorPoint.
previousPoint = currentPoint
ifFalse:
[previousPoint := currentPoint.
aVertex2 vertexPoint: currentPoint.
aBranch strokeColor: aBranch class sampleColor.
aBranch display]].
activeWindow display.
^aBranch!
example2
"EngiBranch example2."
| aBranch aVertex1 aVertex2 visualTransporter |
aBranch := EngiBranch new.
aBranch strokeColor: ColorValue red.
aBranch lineWidth: 3.
aVertex1 := EngiVertex new.
aVertex1 vertexPoint: 100 @ 100.
aVertex2 := EngiVertex new.
aVertex2 vertexPoint: 200 @ 200.
aBranch startVertex: aVertex1.
aBranch endVertex: aVertex2.
visualTransporter := EngiVisualTransporter load: aBranch.
visualTransporter
follow: [self activeSensor cursorPoint]
while: [self activeSensor noButtonPressed]
on: self activeGraphicsContext.
^aBranch! !
EngiBranch subclass: #EngiBranchWithArrow
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Engi-Graph'!
EngiBranchWithArrow comment:
'
Engi 0.07 (24 March 1994)
Copyright (C) 1994 by Atsushi Aoki
'!
!EngiBranchWithArrow methodsFor: 'bounds accessing'!
computeBounds
| bounds margin expand |
bounds := (self startPoint corner: self startPoint)
merge: (self endPoint corner: self endPoint).
margin := (self lineWidth / 2) asInteger.
expand := margin @ margin corner: margin + 1 @ (margin + 1).
bounds := bounds rounded expandedBy: expand.
bounds := bounds merge: self arrowBounds.
^bounds! !
!EngiBranchWithArrow methodsFor: 'arrow accessing'!
arrowAngle
^self class angleFrom: self startPoint to: self endPoint!
arrowBounds
| bounds margin expand |
bounds := nil.
self arrowPoints do: [:p | bounds isNil
ifTrue: [bounds := p corner: p]
ifFalse: [bounds := bounds merge: (p corner: p)]].
margin := (self lineWidth / 2) asInteger.
expand := margin @ margin corner: margin + 1 @ (margin + 1).
bounds := bounds rounded expandedBy: expand.
^bounds!
arrowPoint
^self startPoint + self endPoint / 2!
arrowPoints
| angle p0 p1 p2 p3 |
angle := self class angleFrom: self startPoint to: self endPoint.
p0 := self arrowPoint.
p1 := p0 + (self class rotate: 3 @ 0 by: angle).
p2 := p0 + (self class rotate: -3 @ 2 by: angle).
p3 := p0 + (self class rotate: -3 @ -2 by: angle).
^Array
with: p1
with: p2
with: p3
with: p1! !
!EngiBranchWithArrow methodsFor: 'displaying'!
displayStrokedOn: graphicsContext at: aPoint
| startPoint endPoint arrowPolyline |
self strokeColor isNil ifFalse: [((self bounds translatedBy: aPoint)
intersects: graphicsContext clippingBounds)
ifTrue:
[startPoint := (self startPoint + aPoint) rounded.
endPoint := (self endPoint + aPoint) rounded.
graphicsContext paint: self strokeColor.
graphicsContext lineWidth: self lineWidth.
graphicsContext displayLineFrom: startPoint rounded to: endPoint.
arrowPolyline := self arrowPoints collect: [:p | p rounded].
graphicsContext displayPolygon: arrowPolyline at: aPoint.
graphicsContext displayPolyline: arrowPolyline at: aPoint]]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
EngiBranchWithArrow class
instanceVariableNames: ''!
!EngiBranchWithArrow class methodsFor: 'examples'!
example1
"EngiBranchWithArrow example1."
| aBranch aVertex1 aVertex2 activeWindow activeSensor previousPoint currentPoint |
aBranch := EngiBranchWithArrow new.
aVertex1 := EngiVertex new.
aVertex1 vertexPoint: 100 @ 100.
aVertex2 := EngiVertex new.
aVertex2 vertexPoint: 100 @ 100.
aBranch startVertex: aVertex1.
aBranch endVertex: aVertex2.
activeWindow := aBranch class activeWindow.
activeWindow clear.
activeSensor := activeWindow sensor.
previousPoint := nil.
[activeSensor anyButtonPressed]
whileFalse:
[currentPoint := activeSensor cursorPoint.
previousPoint = currentPoint
ifFalse:
[previousPoint := currentPoint.
aVertex2 vertexPoint: currentPoint.
aBranch strokeColor: aBranch class sampleColor.
aBranch display]].
activeWindow display.
^aBranch!
example2
"EngiBranchWithArrow example2."
| aBranch aVertex1 aVertex2 visualTransporter |
aBranch := EngiBranchWithArrow new.
aBranch strokeColor: ColorValue red.
aBranch lineWidth: 3.
aVertex1 := EngiVertex new.
aVertex1 vertexPoint: 100 @ 100.
aVertex2 := EngiVertex new.
aVertex2 vertexPoint: 200 @ 200.
aBranch startVertex: aVertex1.
aBranch endVertex: aVertex2.
visualTransporter := EngiVisualTransporter load: aBranch.
visualTransporter
follow: [self activeSensor cursorPoint]
while: [self activeSensor noButtonPressed]
on: self activeGraphicsContext.
^aBranch! !