[Cuis-dev] Pythagoras Tree Morph

Mariano Montone marianomontone at gmail.com
Thu Nov 3 10:43:04 PDT 2022


Hi,

I attach a Pythagoras tree Morph.


Try with:

PythagorasTreeMorph new
     withMultipleColors;
     openInWorld.

Scroll up/down to change angle.
Scroll up/down + Ctrl to change depth (you may need Preferences at: 
#ctrlArrowsScrollHorizontally put: true for this).

(note: scroll + ctrl events are not working for me on Linux, or I've 
implemented it wrong.)

Feel free to include as one of VectorGraphics example morphs in case you 
like it.

Cheers!


     Mariano


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20221103/0877cd80/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Captura de pantalla -2022-11-03 14-22-09.png
Type: image/png
Size: 111690 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20221103/0877cd80/attachment-0001.png>
-------------- next part --------------
'From Cuis 6.0 [latest update: #5546] on 3 November 2022 at 2:37:19 pm'!
!classDefinition: #PythagorasTreeMorph category: 'PythagoreanTree'!
PlacedMorph subclass: #PythagorasTreeMorph
	instanceVariableNames: 'depthLimit angle colors'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'PythagoreanTree'!
!PythagorasTreeMorph commentStamp: 'MM 11/3/2022 14:35:42' prior: 0!
A Pythagoras Tree using Vector Graphics.

Scroll up/down to change angle.
Scroll up/down + Ctrl to change depth (you may need Preferences at: #ctrlArrowsScrollHorizontally put: true for this).

Examples: 

PythagorasTreeMorph new
	withMultipleColors;
	openInWorld.
	
PythagorasTreeMorph new openInWorld.
	
PythagorasTreeMorph new
	angle: 0.4;
	withMultipleColors;
	openInWorld.
!


!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:04:04'!
drawOn: aCanvas

	self drawTreeOn: aCanvas p1: 275 at 500 p2: 375 at 500 depth: 0! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:01:10'!
drawTreeOn: aCanvas p1: p1 p2: p2 depth: depth

	| dx dy p3 p4 p5 color |
	
	depth = depthLimit ifTrue: [ ^ self].
	
	color _ self color.
	colors ifNotNil: [
		color _ colors at: (depth mod: colors size) + 1].
	
	dx := p2 x - (p1 x).
	dy := p1 y - (p2 y).
	
	p3 := (p2 x - dy) @ (p2 y - dx).
	p4 := (p1 x - dy) @ (p1 y - dx).
	p5 := (p4 x + (angle * (dx - dy))) @ (p4 y - (angle * (dx + dy))).
	
	aCanvas fillColor: color do: [
		aCanvas moveTo: p1;
			lineTo: p2;
			lineTo: p3;
			lineTo: p4;
			lineTo: p1].
			
	aCanvas fillColor: color do: [
		aCanvas moveTo:  p3;
			lineTo: p4;
			lineTo: p5;
			lineTo: p3].
			
	self drawTreeOn: aCanvas p1: p4 p2: p5 depth: depth + 1.
	self drawTreeOn: aCanvas p1: p5 p2: p3 depth: depth + 1.! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:16:22'!
handlesMouseScroll: anEvent

	^ true! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:19:52'!
initialize
	super initialize.
	
	depthLimit _ 9.
	angle _ 0.5! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:33:11'!
mouseScroll: aMouseEvent localPosition: localEventPosition
	aMouseEvent direction
		caseOf: {
			[ #up ] 		-> 		[  angle := angle + 0.02. self redrawNeeded ].
			[ #down ] 	-> 		[ angle := (angle - 0.02) max: 0. self redrawNeeded ].
			[ #left ] -> [self halt. depthLimit > 1 ifTrue: [depthLimit := depthLimit - 1. self redrawNeeded]].
			[ #right ] -> [self halt. depthLimit := depthLimit + 1]
		}! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 12:01:33'!
requiresVectorCanvas
	
	^ true! !

!PythagorasTreeMorph methodsFor: 'as yet unclassified' stamp: 'MM 11/3/2022 14:36:44'!
withMultipleColors
	colors _ {Color green. Color red. Color blue. Color orange. Color purple. Color magenta. Color yellow. Color brown}! !


!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
angle
	"Answer the value of angle"

	^ angle! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
angle: anObject
	"Set the value of angle"

	angle := anObject! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
colors
	"Answer the value of colors"

	^ colors! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
colors: anObject
	"Set the value of colors"

	colors := anObject! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
depthLimit
	"Answer the value of depthLimit"

	^ depthLimit! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
depthLimit: anObject
	"Set the value of depthLimit"

	depthLimit := anObject! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
form
	"Answer the value of form"

	^ form! !

!PythagorasTreeMorph methodsFor: 'accessing' stamp: 'MM 11/3/2022 14:03:08'!
form: anObject
	"Set the value of form"

	form := anObject! !


More information about the Cuis-dev mailing list