entry #1
comments 0
art.factor ASCII text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
USING: accessors arrays calendar colors kernel math
math.constants math.functions math.parser
math.vectors opengl processing.shapes random sequences timers ui
ui.gadgets ui.gadgets.cartesian ui.gestures ;
IN: art
TUPLE: turtle pdown direction orig position lines ;
: <turtle> ( -- turtle )
t pi 2 / { 0.0 0.0 } dup [ clone ] bi@ V{ } clone turtle boa ;
<PRIVATE
: line ( turtle a b -- turtle )
pick pdown>> [
2array '[ _ suffix! ] change-lines
] [ 2drop ] if ; inline
PRIVATE>
: fwd ( turtle steps -- turtle )
over direction>> [ cos * ] [ sin * ] 2bi 2array
over position>> [ v+ ] keep [ line ] keepd >>position ;
: trn ( turtle rads -- turtle )
over direction>> + >>direction ; inline
: set-angle ( turtle rads -- turtle )
>>direction ; inline
: pdn ( turtle -- turtle ) t >>pdown ; inline
: pup ( turtle -- turtle ) f >>pdown ; inline
CONSTANT: rand-weights { 12 11 10 10 10 10 10 10 10 10 10 11 }
CONSTANT: zoom-factor 1.1
CONSTANT: trans-amt 10
TUPLE: canvas < cartesian turtle scale timer ;
M: canvas graft*
[ timer>> start-timer ] [ call-next-method ] bi ;
M: canvas ungraft*
[ timer>> stop-timer ] [ call-next-method ] bi ;
: rand-idx-weighted ( weights -- idx )
dup dup sum random
[ [ unclip-slice ] dip swap - dup 0 > ] loop drop
2length - ;
: next-line ( canvas -- )
[
turtle>>
rand-weights rand-idx-weighted
rand-weights length / pi 2 * * set-angle 10 fwd
] keep swap >>turtle relayout-1 ;
: update-range ( art -- art )
dup dim>> [ [ -1 * ] keep 2array over scale>> 2 / v*n ] map
over [ first2 ] dip turtle>> orig>> first2 -rotd
[ n+v x-range ] 2dip v+n y-range ;
: draw-art ( art -- )
COLOR: black gl-color
turtle>> lines>> [ first2 draw-line* ] each ;
: <canvas> ( -- gadget )
canvas new init-cartesian
{ 800 800 } >>pdim
<turtle> >>turtle
1.0 >>scale
dup '[ _ next-line ] f 7 milliseconds <timer> >>timer
dup '[ COLOR: white gl-clear _ update-range draw-art ] >>action ;
:: zoom ( art! factor -- )
art factor over scale>> * >>scale art! ;
:: trans ( art! amt -- )
art amt over scale>> trans-amt * v*n
over [ turtle>> orig>> v+ ] keep turtle>> swap >>orig >>turtle art! ;
canvas H{
{ T{ key-down f f "o" } [ zoom-factor zoom ] }
{ T{ key-down f f "i" } [ 1 zoom-factor / zoom ] }
{ T{ key-down f f "UP" } [ { 0 1 } trans ] }
{ T{ key-down f f "DOWN" } [ { 0 -1 } trans ] }
{ T{ key-down f f "LEFT" } [ { -1 0 } trans ] }
{ T{ key-down f f "RIGHT" } [ { 1 0 } trans ] }
} set-gestures
MAIN-WINDOW: art-window
{ { title "Biased Random Walk" } }
<canvas> >>gadgets ;
MAIN: art-window
post a comment