2 USING: accessors arrays fry kernel math math.vectors sequences
5 combinators.short-circuit
6 combinators.cleave.enhanced
11 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13 ! Two dimensional world protocol
15 GENERIC: x ( obj -- x )
16 GENERIC: y ( obj -- y )
18 GENERIC: (x!) ( x obj -- )
19 GENERIC: (y!) ( y obj -- )
21 : x! ( obj x -- obj ) over (x!) ;
22 : y! ( obj y -- obj ) over (y!) ;
24 GENERIC: width ( obj -- width )
25 GENERIC: height ( obj -- height )
27 GENERIC: (width!) ( width obj -- )
28 GENERIC: (height!) ( height obj -- )
30 : width! ( obj width -- obj ) over (width!) ;
31 : height! ( obj height -- obj ) over (width!) ;
33 ! Predicates on relative placement
35 GENERIC: to-the-left-of? ( obj obj -- ? )
36 GENERIC: to-the-right-of? ( obj obj -- ? )
38 GENERIC: below? ( obj obj -- ? )
39 GENERIC: above? ( obj obj -- ? )
41 GENERIC: in-between-horizontally? ( obj obj -- ? )
43 GENERIC: horizontal-interval ( obj -- interval )
45 GENERIC: move-to ( obj obj -- )
47 GENERIC: move-by ( obj delta -- )
49 GENERIC: move-left-by ( obj obj -- )
50 GENERIC: move-right-by ( obj obj -- )
52 GENERIC: left ( obj -- left )
53 GENERIC: right ( obj -- right )
54 GENERIC: bottom ( obj -- bottom )
55 GENERIC: top ( obj -- top )
57 GENERIC: distance ( a b -- c )
59 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
61 ! Some of the above methods work on two element sequences.
62 ! A two element sequence may represent a point in space or describe
65 METHOD: x ( sequence -- x ) first ;
66 METHOD: y ( sequence -- y ) second ;
68 METHOD: (x!) ( number sequence -- ) set-first ;
69 METHOD: (y!) ( number sequence -- ) set-second ;
71 METHOD: width ( sequence -- width ) first ;
72 METHOD: height ( sequence -- height ) second ;
74 : changed-x ( seq quot -- ) over [ [ x ] dip call ] dip (x!) ; inline
75 : changed-y ( seq quot -- ) over [ [ y ] dip call ] dip (y!) ; inline
77 METHOD: move-to ( sequence sequence -- ) [ x x! ] [ y y! ] bi drop ;
78 METHOD: move-by ( sequence sequence -- ) dupd v+ [ x x! ] [ y y! ] bi drop ;
80 METHOD: move-left-by ( sequence number -- ) '[ _ - ] changed-x ;
81 METHOD: move-right-by ( sequence number -- ) '[ _ + ] changed-x ;
83 ! METHOD: move-left-by ( sequence number -- ) neg 0 2array move-by ;
84 ! METHOD: move-right-by ( sequence number -- ) 0 2array move-by ;
86 ! METHOD:: move-left-by ( SEQ:sequence X:number -- )
87 ! SEQ { X 0 } { -1 0 } v* move-by ;
89 METHOD: distance ( sequence sequence -- dist ) v- norm ;
91 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
93 ! A class for objects with a position
97 METHOD: x ( <pos> -- x ) pos>> first ;
98 METHOD: y ( <pos> -- y ) pos>> second ;
100 METHOD: (x!) ( number <pos> -- ) pos>> set-first ;
101 METHOD: (y!) ( number <pos> -- ) pos>> set-second ;
103 METHOD: to-the-left-of? ( <pos> number -- ? ) [ x ] dip < ;
104 METHOD: to-the-right-of? ( <pos> number -- ? ) [ x ] dip > ;
106 METHOD: move-left-by ( <pos> number -- ) [ pos>> ] dip move-left-by ;
107 METHOD: move-right-by ( <pos> number -- ) [ pos>> ] dip move-right-by ;
109 METHOD: above? ( <pos> number -- ? ) [ y ] dip > ;
110 METHOD: below? ( <pos> number -- ? ) [ y ] dip < ;
112 METHOD: move-by ( <pos> sequence -- ) '[ _ v+ ] change-pos drop ;
114 METHOD: distance ( <pos> <pos> -- dist ) [ pos>> ] bi@ distance ;
116 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
118 ! A class for objects with velocity. It inherits from <pos>. Hey, if
119 ! it's moving it has a position right? Unless it's some alternate universe...
121 TUPLE: <vel> < <pos> vel ;
123 : moving-up? ( obj -- ? ) vel>> y 0 > ;
124 : moving-down? ( obj -- ? ) vel>> y 0 < ;
126 : step-size ( vel time -- dist ) [ vel>> ] dip v*n ;
127 : move-for ( vel time -- ) dupd step-size move-by ;
129 : reverse-horizontal-velocity ( vel -- ) vel>> [ x neg ] [ ] bi (x!) ;
131 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
133 ! The 'pos' slot indicates the lower left hand corner of the
134 ! rectangle. The 'dim' is holds the width and height.
136 TUPLE: <rectangle> < <pos> dim ;
138 METHOD: width ( <rectangle> -- width ) dim>> first ;
139 METHOD: height ( <rectangle> -- height ) dim>> second ;
141 METHOD: left ( <rectangle> -- x ) x ;
142 METHOD: right ( <rectangle> -- x ) \\ x width bi + ;
143 METHOD: bottom ( <rectangle> -- y ) y ;
144 METHOD: top ( <rectangle> -- y ) \\ y height bi + ;
146 : bottom-left ( rectangle -- pos ) pos>> ;
148 : center-x ( rectangle -- x ) [ left ] [ width 2 / ] bi + ;
149 : center-y ( rectangle -- y ) [ bottom ] [ height 2 / ] bi + ;
151 : center ( rectangle -- seq ) \\ center-x center-y bi 2array ;
153 METHOD: to-the-left-of? ( <pos> <rectangle> -- ? ) \\ x left bi* < ;
154 METHOD: to-the-right-of? ( <pos> <rectangle> -- ? ) \\ x right bi* > ;
156 METHOD: below? ( <pos> <rectangle> -- ? ) \\ y bottom bi* < ;
157 METHOD: above? ( <pos> <rectangle> -- ? ) \\ y top bi* > ;
159 METHOD: horizontal-interval ( <rectangle> -- interval )
160 \\ left right bi [a,b] ;
162 METHOD: in-between-horizontally? ( <pos> <rectangle> -- ? )
163 \\ x horizontal-interval bi* interval-contains? ;
165 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
167 TUPLE: <extent> left right bottom top ;
169 METHOD: left ( <extent> -- left ) left>> ;
170 METHOD: right ( <extent> -- right ) right>> ;
171 METHOD: bottom ( <extent> -- bottom ) bottom>> ;
172 METHOD: top ( <extent> -- top ) top>> ;
174 METHOD: width ( <extent> -- width ) \\ right>> left>> bi - ;
175 METHOD: height ( <extent> -- height ) \\ top>> bottom>> bi - ;
177 ! METHOD: to-extent ( <rectangle> -- <extent> )
178 ! { [ left>> ] [ right>> ] [ bottom>> ] [ top>> ] } cleave <extent> boa ;
180 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
182 METHOD: to-the-left-of? ( sequence <rectangle> -- ? ) \\ x left bi* < ;
183 METHOD: to-the-right-of? ( sequence <rectangle> -- ? ) \\ x right bi* > ;
185 METHOD: below? ( sequence <rectangle> -- ? ) \\ y bottom bi* < ;
186 METHOD: above? ( sequence <rectangle> -- ? ) \\ y top bi* > ;
188 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
190 ! Some support for the' 'rect' class from math.geometry.rect'
192 ! METHOD: width ( rect -- width ) dim>> first ;
193 ! METHOD: height ( rect -- height ) dim>> second ;
195 ! METHOD: left ( rect -- left ) loc>> x
196 ! METHOD: right ( rect -- right ) [ loc>> x ] [ width ] bi + ;
198 ! METHOD: to-the-left-of? ( sequence rect -- ? ) [ x ] [ loc>> x ] bi* < ;
199 ! METHOD: to-the-right-of? ( sequence rect -- ? ) [ x ] [ loc>> x ] bi* > ;
201 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
203 USING: locals combinators ;
205 :: wrap ( POINT RECT -- POINT )
208 { [ POINT RECT to-the-left-of? ] [ RECT right ] }
209 { [ POINT RECT to-the-right-of? ] [ RECT left ] }
210 { [ t ] [ POINT x ] }
215 { [ POINT RECT below? ] [ RECT top ] }
216 { [ POINT RECT above? ] [ RECT bottom ] }
217 { [ t ] [ POINT y ] }
223 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
225 GENERIC: within? ( a b -- ? )
227 METHOD: within? ( <pos> <rectangle> -- ? )
229 [ left to-the-right-of? ]
230 [ right to-the-left-of? ]