home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
LANGUAGE
/
SMALTALK
/
TEXTBOOK
/
AP14.ST
(
.txt
)
< prev
next >
Wrap
Text File
|
1997-04-22
|
7KB
|
299 lines
'Smalltalk Textbook Appendix 14'!
Object subclass: #EngiTurtle
instanceVariableNames: 'location direction down nib color gstate '
classVariableNames: ''
poolDictionaries: ''
category: 'Engi-Geometric'!
EngiTurtle comment:
'
Engi 0.04 (8 February 1994)
Copyright (C) 1994 by Atsushi Aoki
'!
!EngiTurtle methodsFor: 'initialize-release'!
initialize
location := 0 @ 0.
direction := 0.
down := true.
gstate := nil! !
!EngiTurtle methodsFor: 'accessing'!
color
^color!
color: colorValue
color := colorValue!
direction
^direction!
direction: degrees
direction := degrees \\ 360!
graphicsContext
^gstate!
graphicsContext: graphicsContext
graphicsContext notNil ifTrue: [gstate := graphicsContext copy]!
location
^location!
location: aPoint
location := aPoint!
nib
^nib!
nib: anInteger
nib := anInteger asInteger max: 0! !
!EngiTurtle methodsFor: 'testing'!
canDraw
^self graphicsContext notNil!
isDown
^down = true!
isUp
^self isDown not! !
!EngiTurtle methodsFor: 'moving'!
down
down := true!
east
self direction: 0!
go: distance
| dir |
direction = 0 ifTrue: [^self goto: location + (distance @ 0)].
direction = 90 ifTrue: [^self goto: location + (0 @ distance)].
direction = 180 ifTrue: [^self goto: location - (distance @ 0)].
direction = 270 ifTrue: [^self goto: location - (0 @ distance)].
dir := direction degreesToRadians.
dir := dir cos @ dir sin.
self goto: dir * distance + location!
goto: aPoint
| old |
old := self location.
self location: aPoint.
self drawLineFrom: old to: aPoint!
home
self canDraw
ifTrue: [self place: self graphicsContext medium bounds center]
ifFalse: [self place: 0 @ 0]!
north
self direction: 270!
place: aPoint
self location: aPoint!
south
self direction: 90!
turn: degrees
self direction: self direction + degrees!
up
down := false!
west
self direction: 180! !
!EngiTurtle methodsFor: 'converting'!
asImage
self canDraw
ifTrue: [^self graphicsContext medium asImage]
ifFalse: [^nil]! !
!EngiTurtle methodsFor: 'designs'!
dragon: orderNumber distance: distance
orderNumber = 0
ifTrue: [self go: distance]
ifFalse: [orderNumber > 0
ifTrue:
[self dragon: orderNumber - 1 distance: distance.
self turn: 90.
self dragon: 1 - orderNumber distance: distance]
ifFalse:
[self dragon: -1 - orderNumber distance: distance.
self turn: -90.
self dragon: 1 + orderNumber distance: distance]]!
mandala: npoints diameter: d
| l points |
l := (3.14 * d / npoints) rounded.
self home; up; turn: -90; go: d // 2; turn: 90; go: 0 - l // 2.
points := Array new: npoints.
1 to: npoints do:
[:i |
points at: i put: location rounded.
self go: l; turn: 360 // npoints].
self down.
npoints // 2
to: 1
by: -1
do: [:i | 1 to: npoints do:
[:j |
self place: (points at: j).
self goto: (points at: j + i - 1 \\ npoints + 1)]]!
spiral: n angle: a
1 to: n do: [:i | self go: i; turn: a]! !
!EngiTurtle methodsFor: 'private'!
drawLineFrom: startPoint to: endPoint
| graphicsContext |
graphicsContext := self graphicsContext.
(self canDraw and: [self isDown])
ifTrue:
[graphicsContext lineWidth: self nib.
graphicsContext paint: self color.
graphicsContext capStyle: GraphicsContext capRound.
graphicsContext joinStyle: GraphicsContext joinRound.
graphicsContext displayLineFrom: startPoint to: endPoint]! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
EngiTurtle class
instanceVariableNames: ''!
!EngiTurtle class methodsFor: 'instance creation'!
new
| turtle |
turtle := super new initialize.
turtle graphicsContext: nil.
turtle home.
^turtle!
on: graphicsContext
| turtle |
turtle := super new initialize.
turtle graphicsContext: graphicsContext.
turtle home.
^turtle! !
!EngiTurtle class methodsFor: 'examples'!
example1
"EngiTurtle example1."
| window turtle start |
window := ScheduledControllers activeController view.
turtle := EngiTurtle new.
turtle nib: 8; color: ColorValue red.
turtle graphicsContext: window graphicsContext.
window clear.
turtle home.
start := turtle location.
turtle east; turn: 30; go: 60.
turtle turn: 90.
30 * 2 timesRepeat: [turtle go: 60 * 2 * 3.14159 / (36 * 2); turn: 10 / 2].
turtle goto: start.
window sensor waitClickButton.
window display!
example2
"EngiTurtle example2."
| window turtle |
window := ScheduledControllers activeController view.
turtle := EngiTurtle new.
turtle nib: 2; color: ColorValue red.
turtle graphicsContext: window graphicsContext.
window clear.
turtle home.
turtle dragon: 10 distance: 5.
window sensor waitClickButton.
window display!
example3
"EngiTurtle example3."
| window turtle size |
window := ScheduledControllers activeController view.
turtle := EngiTurtle new.
turtle nib: 4; color: ColorValue red.
size := window bounds width min: window bounds height.
turtle graphicsContext: window graphicsContext.
window clear.
turtle home.
turtle spiral: size * (4 / 5) angle: 144.
window sensor waitClickButton.
window display!
example4
"EngiTurtle example4."
| window turtle size |
window := ScheduledControllers activeController view.
turtle := EngiTurtle new.
turtle nib: 1; color: ColorValue red.
size := window bounds width min: window bounds height.
turtle graphicsContext: window graphicsContext.
window clear.
turtle home.
turtle mandala: 24 diameter: size - 20.
window sensor waitClickButton.
window display!
example5
"EngiTurtle example5."
| window creator turtle1 turtle2 turtle3 turtle4 turtle5 box size promise spiral |
window := ScheduledControllers activeController view.
creator := [(EngiTurtle new) nib: 1; color: ColorValue red; yourself].
turtle1 := creator value.
turtle2 := creator value.
turtle3 := creator value.
turtle4 := creator value.
turtle5 := creator value.
window clear.
box := window bounds.
size := (box width min: box height)
// 3.
promise := Semaphore new.
spiral :=
[:turtle |
1 to: size
do:
[:i |
turtle graphicsContext: window graphicsContext.
turtle go: i; turn: 89.
Processor yield].
promise signal].
turtle1 place: (box topLeft corner: box center) center; direction: 0.
turtle2 place: (box topCenter corner: box rightCenter) center; direction: 0.
turtle3 place: (box leftCenter corner: box bottomCenter) center; direction: 0.
turtle4 place: (box center corner: box bottomRight) center; direction: 0.
turtle5 place: box center; direction: 0.
[spiral value: turtle1] fork.
[spiral value: turtle2] fork.
[spiral value: turtle3] fork.
[spiral value: turtle4] fork.
[spiral value: turtle5] fork.
promise wait; wait; wait; wait; wait.
window sensor waitClickButton.
window display! !