Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Squeak Smalltalk source code for a Morph displaying sin/cos/tan as an animation. Here's an example captured animation: http://www.eighty-twenty.org/images/sct_anim.gif
Morph subclass: #SinCosTanMorph
instanceVariableNames: 'radius theta sinHistory cosHistory tanHistory stretch'
classVariableNames: ''
poolDictionaries: ''
category: 'SinCosTan'!
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 15:31'!
drawHistory: aHistory on: aCanvas from: aPoint withBasis: aBasis
| xAxis yAxis b p q n |
xAxis := aBasis first * stretch.
yAxis := aBasis second.
b := self bounds.
p := nil.
n := 0.
aHistory reverseDo: [:sample |
q := aPoint + (xAxis * n) + (yAxis * sample * radius).
p isNil ifFalse: [
((b containsPoint: p) or: [b containsPoint: q]) ifTrue: [
aCanvas line: p to: q color: Color blue]].
p := q.
n := n + 1.]! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 16:10'!
drawOn: aCanvas
| center radial |
center := self topLeft + (radius * 2 @ (self height // 2)).
radial := center + (theta cos @ theta sin negated * radius).
aCanvas
frameOval: ((center - (radius@radius)) corner: (center + (radius@radius)))
color: Color black.
aCanvas
line: (center - (radial - center * 20)) to: center + (radial - center * 20)
color: Color green.
aCanvas line: center to: radial color: Color black.
aCanvas fillOval: ((radial - (4@4)) corner: (radial + (4@4))) color: Color black.
aCanvas line: radial x @ self top to: radial x @ self bottom color: Color green.
aCanvas line: self left @ radial y to: self right @ radial y color: Color green.
aCanvas line: center x @ self top to: center x @ self bottom color: Color red.
aCanvas line: center x + radius @ self top to: center x + radius @ self bottom color: Color red.
aCanvas line: self left @ center y to: self right @ center y color: Color red.
self drawHistory: sinHistory on: aCanvas from: center + (radius@0) withBasis: {1@0. 0@-1}.
self drawHistory: tanHistory on: aCanvas from: center + (radius@0) withBasis: {1@0. 0@-1}.
self drawHistory: cosHistory on: aCanvas from: center + (0@radius) withBasis: {0@1. 1@0}.! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 15:28'!
initialize
super initialize.
radius := 50.
theta := 1.
stretch := 4.
self extent: (radius * 5) @ (radius * 5).
sinHistory := OrderedCollection new.
cosHistory := OrderedCollection new.
tanHistory := OrderedCollection new.! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 16:09'!
saveFramesNamed: aString
| oldLen limit fileName n |
theta := 0.
oldLen := sinHistory size.
[self step. oldLen < sinHistory size] whileTrue: [oldLen := sinHistory size].
theta := theta \\ (Float pi * 2).
limit := theta + (Float pi * 2).
n := 0.
[theta < limit] whileTrue: [
fileName := aString, '_', (n printStringLength: 5 padded: true), '.png'.
PNGReadWriter putForm: self imageForm onFileNamed: fileName.
self step.
n := n + 1.
].! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 16:10'!
step
theta := (theta + 0.1) \\ (Float pi * 20).
self updateHistory: sinHistory with: theta sin.
self updateHistory: cosHistory with: theta cos.
self updateHistory: tanHistory with: theta tan.
self changed.! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 15:29'!
stepTime
^ 75! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 15:28'!
updateHistory: aHistory with: aSample
| limit |
limit := (self width max: self height) // stretch.
aHistory add: aSample.
[aHistory size > limit] whileTrue: [aHistory removeFirst].! !
!SinCosTanMorph methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 14:31'!
wantsSteps
^ true! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
SinCosTanMorph class
instanceVariableNames: ''!
!SinCosTanMorph class methodsFor: 'as yet unclassified' stamp: 'tonyg 4/19/2010 15:53'!
example
| m |
m := SinCosTanMorph new.
m extent: 600@600.
m position: 100@100.
m openInWorld.
^ m! !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.