Object subclass: #Turtle instanceVariableNames: 'window location direction width penDown ' classVariableNames: '' poolDictionaries: '' category: 'Turtle Graphics'! !Turtle methodsFor: 'turtle commands'! clear window clear! go: dist | delta | delta := Point r: dist theta: (direction-90.0/360.0)*2*Float pi. self goto: location+delta! goto: newLocation penDown ifTrue: [window graphicsContext lineWidth: width; displayLineFrom: location to: newLocation]. location := newLocation! home location := window displayBox center. direction := 0. penDown := true! turn: degrees direction := direction + degrees \\ 360! width: w width := w! ! !Turtle methodsFor: 'initialization'! window: w window := w. width := 1. self home.! ! !Turtle methodsFor: 'designs'! squiral: length angle: angle | dist | dist := length. self home. 1 to: 200 do: [:k | self go: dist. self turn: angle. dist := length + dist] " Smalltalk at: #W put: ScheduledWindow new. W open. Turtle new window: W; squiral: 2 angle: 89 "! ! Turtle subclass: #ColorTurtle instanceVariableNames: 'color ' classVariableNames: '' poolDictionaries: '' category: 'Turtle Graphics'! !ColorTurtle methodsFor: 'initialization'! window: w super window: w. color := ColorValue black! ! !ColorTurtle methodsFor: 'turtle commands'! color: c color := c! go: dist changingColorTo: newColor | oldRed newRed oldBlue newBlue oldGreen newGreen percent | oldRed := color red. oldBlue := color blue. oldGreen := color green. newRed := newColor red. newBlue := newColor blue. newGreen := newColor green. 1 to: dist do: [:k | percent := k asFloat / dist asFloat. color := ColorValue red: oldRed + ((newRed-oldRed)*percent) green: oldGreen + ((newGreen-oldGreen)*percent) blue: oldBlue + ((newBlue-oldBlue)*percent). self go: 1]! goto: newLocation penDown ifTrue: [window graphicsContext paint: color; lineWidth: width; displayLineFrom: location to: newLocation]. location := newLocation! ! !ColorTurtle methodsFor: 'designs'! polygon: sides length: l 1 to: sides do: [:k | self go: l. self turn: (360.0 / sides)] " Smalltalk at: #W put: ScheduledWindow new. W open. W clear. ColorTurtle new window: W; width: 4; color: ColorValue red; polygon: 5 length: 100 "! squiral: length angle: angle | dist | dist := length. 1 to: 200 do: [:k | self go: dist. self turn: angle. dist := length + dist] " Smalltalk at: #W put: ScheduledWindow new. W open. W clear. ColorTurtle new window: W; width: 20; color: ColorValue red; go: 200 changingColorTo: ColorValue blue ColorTurtle new window: W; color: ColorValue red; squiral: 2 angle: 122; home; color: ColorValue purple; squiral: 2 angle: -122 "! !