2 USING: accessors arrays fry kernel math math.vectors sequences
5 combinators.cleave.enhanced
10 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 ! Two dimensional world protocol
14 GENERIC: x ( obj -- x )
15 GENERIC: y ( obj -- y )
17 GENERIC: (x!) ( x obj -- )
18 GENERIC: (y!) ( y obj -- )
20 : x! ( obj x -- obj ) over (x!) ;
21 : y! ( obj y -- obj ) over (y!) ;
23 GENERIC: width ( obj -- width )
24 GENERIC: height ( obj -- height )
26 GENERIC: (width!) ( width obj -- )
27 GENERIC: (height!) ( height obj -- )
29 : width! ( obj width -- obj ) over (width!) ;
30 : height! ( obj height -- obj ) over (width!) ;
32 ! Predicates on relative placement
34 GENERIC: to-the-left-of? ( obj obj -- ? )
35 GENERIC: to-the-right-of? ( obj obj -- ? )
37 GENERIC: below? ( obj obj -- ? )
38 GENERIC: above? ( obj obj -- ? )
40 GENERIC: in-between-horizontally? ( obj obj -- ? )
42 GENERIC: horizontal-interval ( obj -- interval )
44 GENERIC: move-to ( obj obj -- )
46 GENERIC: move-by ( obj delta -- )
48 GENERIC: move-left-by ( obj obj -- )
49 GENERIC: move-right-by ( obj obj -- )
51 GENERIC: left ( obj -- left )
52 GENERIC: right ( obj -- right )
53 GENERIC: bottom ( obj -- bottom )
54 GENERIC: top ( obj -- top )
56 GENERIC: distance ( a b -- c )
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
60 ! Some of the above methods work on two element sequences.
61 ! A two element sequence may represent a point in space or describe
64 METHOD: x ( sequence -- x ) first ;
65 METHOD: y ( sequence -- y ) second ;
67 METHOD: (x!) ( number sequence -- ) set-first ;
68 METHOD: (y!) ( number sequence -- ) set-second ;
70 METHOD: width ( sequence -- width ) first ;
71 METHOD: height ( sequence -- height ) second ;
73 : changed-x ( seq quot -- ) over [ [ x ] dip call ] dip (x!) ; inline
74 : changed-y ( seq quot -- ) over [ [ y ] dip call ] dip (y!) ; inline
76 METHOD: move-to ( sequence sequence -- ) [ x x! ] [ y y! ] bi drop ;
77 METHOD: move-by ( sequence sequence -- ) dupd v+ [ x x! ] [ y y! ] bi drop ;
79 METHOD: move-left-by ( sequence number -- ) '[ _ - ] changed-x ;
80 METHOD: move-right-by ( sequence number -- ) '[ _ + ] changed-x ;
82 ! METHOD: move-left-by ( sequence number -- ) neg 0 2array move-by ;
83 ! METHOD: move-right-by ( sequence number -- ) 0 2array move-by ;
85 ! METHOD:: move-left-by ( SEQ:sequence X:number -- )
86 ! SEQ { X 0 } { -1 0 } v* move-by ;
88 METHOD: distance ( sequence sequence -- dist ) v- norm ;
90 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 ! A class for objects with a position
96 METHOD: x ( <pos> -- x ) pos>> first ;
97 METHOD: y ( <pos> -- y ) pos>> second ;
99 METHOD: (x!) ( number <pos> -- ) pos>> set-first ;
100 METHOD: (y!) ( number <pos> -- ) pos>> set-second ;
102 METHOD: to-the-left-of? ( <pos> number -- ? ) [ x ] dip < ;
103 METHOD: to-the-right-of? ( <pos> number -- ? ) [ x ] dip > ;
105 METHOD: move-left-by ( <pos> number -- ) [ pos>> ] dip move-left-by ;
106 METHOD: move-right-by ( <pos> number -- ) [ pos>> ] dip move-right-by ;
108 METHOD: above? ( <pos> number -- ? ) [ y ] dip > ;
109 METHOD: below? ( <pos> number -- ? ) [ y ] dip < ;
111 METHOD: move-by ( <pos> sequence -- ) '[ _ v+ ] change-pos drop ;
113 METHOD: distance ( <pos> <pos> -- dist ) [ pos>> ] bi@ distance ;
115 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
117 ! A class for objects with velocity. It inherits from <pos>. Hey, if
118 ! it's moving it has a position right? Unless it's some alternate universe...
120 TUPLE: <vel> < <pos> vel ;
122 : moving-up? ( obj -- ? ) vel>> y 0 > ;
123 : moving-down? ( obj -- ? ) vel>> y 0 < ;
125 : step-size ( vel time -- dist ) [ vel>> ] dip v*n ;
126 : move-for ( vel time -- ) dupd step-size move-by ;
128 : reverse-horizontal-velocity ( vel -- ) vel>> [ x neg ] [ ] bi (x!) ;
130 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
132 ! The 'pos' slot indicates the lower left hand corner of the
133 ! rectangle. The 'dim' is holds the width and height.
135 TUPLE: <rectangle> < <pos> dim ;
137 METHOD: width ( <rectangle> -- width ) dim>> first ;
138 METHOD: height ( <rectangle> -- height ) dim>> second ;
140 METHOD: left ( <rectangle> -- x ) x ;
141 METHOD: right ( <rectangle> -- x ) \\ x width bi + ;
142 METHOD: bottom ( <rectangle> -- y ) y ;
143 METHOD: top ( <rectangle> -- y ) \\ y height bi + ;
145 : bottom-left ( rectangle -- pos ) pos>> ;
147 : center-x ( rectangle -- x ) [ left ] [ width 2 / ] bi + ;
148 : center-y ( rectangle -- y ) [ bottom ] [ height 2 / ] bi + ;
150 : center ( rectangle -- seq ) \\ center-x center-y bi 2array ;
152 METHOD: to-the-left-of? ( <pos> <rectangle> -- ? ) \\ x left bi* < ;
153 METHOD: to-the-right-of? ( <pos> <rectangle> -- ? ) \\ x right bi* > ;
155 METHOD: below? ( <pos> <rectangle> -- ? ) \\ y bottom bi* < ;
156 METHOD: above? ( <pos> <rectangle> -- ? ) \\ y top bi* > ;
158 METHOD: horizontal-interval ( <rectangle> -- interval )
159 \\ left right bi [a,b] ;
161 METHOD: in-between-horizontally? ( <pos> <rectangle> -- ? )
162 \\ x horizontal-interval bi* interval-contains? ;
164 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
166 TUPLE: <extent> left right bottom top ;
168 METHOD: left ( <extent> -- left ) left>> ;
169 METHOD: right ( <extent> -- right ) right>> ;
170 METHOD: bottom ( <extent> -- bottom ) bottom>> ;
171 METHOD: top ( <extent> -- top ) top>> ;
173 METHOD: width ( <extent> -- width ) \\ right>> left>> bi - ;
174 METHOD: height ( <extent> -- height ) \\ top>> bottom>> bi - ;
176 ! METHOD: to-extent ( <rectangle> -- <extent> )
177 ! { [ left>> ] [ right>> ] [ bottom>> ] [ top>> ] } cleave <extent> boa ;
179 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
181 METHOD: to-the-left-of? ( sequence <rectangle> -- ? ) \\ x left bi* < ;
182 METHOD: to-the-right-of? ( sequence <rectangle> -- ? ) \\ x right bi* > ;
184 METHOD: below? ( sequence <rectangle> -- ? ) \\ y bottom bi* < ;
185 METHOD: above? ( sequence <rectangle> -- ? ) \\ y top bi* > ;
187 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
189 ! Some support for the' 'rect' class from math.geometry.rect'
191 ! METHOD: width ( rect -- width ) dim>> first ;
192 ! METHOD: height ( rect -- height ) dim>> second ;
194 ! METHOD: left ( rect -- left ) loc>> x
195 ! METHOD: right ( rect -- right ) [ loc>> x ] [ width ] bi + ;
197 ! METHOD: to-the-left-of? ( sequence rect -- ? ) [ x ] [ loc>> x ] bi* < ;
198 ! METHOD: to-the-right-of? ( sequence rect -- ? ) [ x ] [ loc>> x ] bi* > ;
200 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
202 USING: locals combinators ;
204 :: wrap ( POINT RECT -- POINT )
207 { [ POINT RECT to-the-left-of? ] [ RECT right ] }
208 { [ POINT RECT to-the-right-of? ] [ RECT left ] }
209 { [ t ] [ POINT x ] }
214 { [ POINT RECT below? ] [ RECT top ] }
215 { [ POINT RECT above? ] [ RECT bottom ] }
216 { [ t ] [ POINT y ] }