--- /dev/null
+USING: tools.test math.rectangles ;
+IN: math.rectangles.tests
+
+[ T{ rect f { 10 10 } { 20 20 } } ]
+[
+ T{ rect f { 10 10 } { 50 50 } }
+ T{ rect f { -10 -10 } { 40 40 } }
+ rect-intersect
+] unit-test
+
+[ T{ rect f { 200 200 } { 0 0 } } ]
+[
+ T{ rect f { 100 100 } { 50 50 } }
+ T{ rect f { 200 200 } { 40 40 } }
+ rect-intersect
+] unit-test
+
+[ f ] [
+ T{ rect f { 100 100 } { 50 50 } }
+ T{ rect f { 200 200 } { 40 40 } }
+ contains-rect?
+] unit-test
+
+[ t ] [
+ T{ rect f { 100 100 } { 50 50 } }
+ T{ rect f { 120 120 } { 40 40 } }
+ contains-rect?
+] unit-test
+
+[ f ] [
+ T{ rect f { 1000 100 } { 50 50 } }
+ T{ rect f { 120 120 } { 40 40 } }
+ contains-rect?
+] unit-test
+
++[ T{ rect f { 10 20 } { 20 20 } } ] [
++ {
++ { 20 20 }
++ { 10 40 }
++ { 30 30 }
++ } rect-containing
++] unit-test
--- /dev/null
+! Copyright (C) 2008, 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel arrays sequences math math.vectors accessors ;
+IN: math.rectangles
+
+TUPLE: rect { loc initial: { 0 0 } } { dim initial: { 0 0 } } ;
+
+: <rect> ( loc dim -- rect ) rect boa ; inline
+
+: <zero-rect> ( -- rect ) rect new ; inline
+
+: point>rect ( loc -- rect ) { 0 0 } <rect> ; inline
+
+: rect-bounds ( rect -- loc dim ) [ loc>> ] [ dim>> ] bi ;
+
+: rect-extent ( rect -- loc ext ) rect-bounds over v+ ;
+
+: with-rect-extents ( rect1 rect2 loc-quot: ( loc1 loc2 -- ) ext-quot: ( ext1 ext2 -- ) -- )
+ [ [ rect-extent ] bi@ ] 2dip bi-curry* bi* ; inline
+
+: <extent-rect> ( loc ext -- rect ) over [v-] <rect> ;
+
+: offset-rect ( rect loc -- newrect )
+ over loc>> v+ swap dim>> <rect> ;
+
+: (rect-intersect) ( rect rect -- array array )
+ [ vmax ] [ vmin ] with-rect-extents ;
+
+: rect-intersect ( rect1 rect2 -- newrect )
+ (rect-intersect) <extent-rect> ;
+
+GENERIC: contains-rect? ( rect1 rect2 -- ? )
+
+M: rect contains-rect?
+ (rect-intersect) [v-] { 0 0 } = ;
+
+GENERIC: contains-point? ( point rect -- ? )
+
+M: rect contains-point?
+ [ point>rect ] dip contains-rect? ;
+
+: (rect-union) ( rect rect -- array array )
+ [ vmin ] [ vmax ] with-rect-extents ;
+
+: rect-union ( rect1 rect2 -- newrect )
+ (rect-union) <extent-rect> ;
++
++: rect-containing ( points -- rect )
++ [ vsupremum ] [ vinfimum ] bi
++ [ nip ] [ v- ] 2bi <rect> ;
: vmax ( u v -- w ) [ max ] 2map ;
: vmin ( u v -- w ) [ min ] 2map ;
-: vgreatest ( array -- vmax ) { -1.0/0.0 -1.0/0.0 } [ vmax ] reduce ;
-: vleast ( array -- vmax ) { 1.0/0.0 1.0/0.0 } [ vmin ] reduce ;
++: vsupremum ( seq -- vmax ) [ ] [ vmax ] map-reduce ;
++: vinfimum ( seq -- vmin ) [ ] [ vmin ] map-reduce ;
+
: v. ( u v -- x ) [ * ] [ + ] 2map-reduce ;
: norm-sq ( v -- x ) [ absq ] [ + ] map-reduce ;
: norm ( v -- x ) norm-sq sqrt ;
! (c) 2009 Joe Groff, see BSD license
- USING: assocs kernel tools.test quadtrees math.rectangles sorting ;
-USING: accessors assocs kernel tools.test quadtrees math.geometry.rect sorting ;
++USING: accessors assocs kernel tools.test quadtrees math.rectangles sorting ;
IN: quadtrees.tests
: unit-bounds ( -- rect ) { -1.0 -1.0 } { 2.0 2.0 } <rect> ;
! (c) 2009 Joe Groff, see BSD license
-USING: assocs kernel math.geometry.rect combinators accessors
-math.vectors vectors sequences math math.points math.geometry
-combinators.short-circuit arrays fry ;
+USING: assocs kernel math.rectangles combinators accessors
- math.vectors vectors sequences math math.points math.geometry
- combinators.short-circuit arrays fry locals ;
++math.vectors vectors sequences math combinators.short-circuit arrays fry ;
IN: quadtrees
TUPLE: quadtree { bounds rect } point value ll lr ul ur leaf? ;
- : <quadtree> ( bounds -- quadtree ) f f f f f f t quadtree boa ;
+ : <quadtree> ( bounds -- quadtree )
+ quadtree new
+ swap >>bounds
+ t >>leaf? ;
: rect-ll ( rect -- point ) loc>> ;
--: rect-lr ( rect -- point ) [ loc>> ] [ width ] bi v+x ;
--: rect-ul ( rect -- point ) [ loc>> ] [ height ] bi v+y ;
--: rect-ur ( rect -- point ) [ loc>> ] [ dim>> ] bi v+ ;
++: rect-lr ( rect -- point ) [ loc>> ] [ dim>> { 1 0 } v* ] bi v+ ;
++: rect-ul ( rect -- point ) [ loc>> ] [ dim>> { 0 1 } v* ] bi v+ ;
++: rect-ur ( rect -- point ) [ loc>> ] [ dim>> ] bi v+ ;
: rect-center ( rect -- point ) [ loc>> ] [ dim>> 0.5 v*n ] bi v+ ; inline
dup leaf?>> [ leaf-at-point ] [ node-at-point ] if ;
: (node-in-rect*) ( values rect node -- values )
-- 2dup bounds>> intersects? [ in-rect* ] [ 2drop ] if ;
++ 2dup bounds>> contains-rect? [ in-rect* ] [ 2drop ] if ;
: node-in-rect* ( values rect node -- values )
[ (node-in-rect*) ] with each-quadrant ;
: leaf-in-rect* ( values rect leaf -- values )
-- tuck { [ nip point>> ] [ point>> swap intersects? ] } 2&&
++ tuck { [ nip point>> ] [ point>> swap contains-point? ] } 2&&
[ value>> over push ] [ drop ] if ;
: in-rect* ( values rect tree -- values )