From 1026587d631263942a4a5e6491fb944ffa2f46c6 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 11 Jul 2008 18:07:04 -0500 Subject: [PATCH] add math.geometry.rect --- extra/math/geometry/rect/rect.factor | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 extra/math/geometry/rect/rect.factor diff --git a/extra/math/geometry/rect/rect.factor b/extra/math/geometry/rect/rect.factor new file mode 100644 index 0000000000..51f42c22ca --- /dev/null +++ b/extra/math/geometry/rect/rect.factor @@ -0,0 +1,42 @@ + +USING: kernel arrays math.vectors ; + +IN: math.geometry.rect + +TUPLE: rect { loc initial: { 0 0 } } { dim initial: { 0 0 } } ; + +: ( -- rect ) rect new ; + +C: rect + +M: array rect-loc ; + +M: array rect-dim drop { 0 0 } ; + +: rect-bounds ( rect -- loc dim ) dup rect-loc swap rect-dim ; + +: rect-extent ( rect -- loc ext ) rect-bounds over v+ ; + +: 2rect-extent ( rect rect -- loc1 loc2 ext1 ext2 ) + [ rect-extent ] bi@ swapd ; + +: ( loc ext -- rect ) over [v-] ; + +: offset-rect ( rect loc -- newrect ) + over rect-loc v+ swap rect-dim ; + +: (rect-intersect) ( rect rect -- array array ) + 2rect-extent vmin >r vmax r> ; + +: rect-intersect ( rect1 rect2 -- newrect ) + (rect-intersect) ; + +: intersects? ( rect/point rect -- ? ) + (rect-intersect) [v-] { 0 0 } = ; + +: (rect-union) ( rect rect -- array array ) + 2rect-extent vmax >r vmin r> ; + +: rect-union ( rect1 rect2 -- newrect ) + (rect-union) ; +