PreviousUpNext

15.4.1312  src/lib/x-kit/draw/box2.pkg

## box2.pkg
#
# The standard x-kit box (rectangle) representation is defined in
#
#     src/lib/std/2d/xgeometry.api
#
# in terms of position + size in the form of int row/col/high/wide fields.
#
# Here we define a sometimes-useful alternate representation in
# terms of two corners specified as int x1/y1/x2/y2 fields.
#
# Note that the lower right point is not actually part of the rectangle.
# Explicitly,
#       BOX { x, y, wid, ht } corresponds to
# box2::BOX { x1=x, y1=y, x2=x+wid, y2=y+ht }. For certain computations (e.g.,
# constructing regions), this representation is more useful.

# Compiled by:
#     src/lib/x-kit/draw/xkit-draw.sublib




                                                # xgeometry     is from   src/lib/std/2d/xgeometry.pkg
package box2 {

    Box = BOX  { x1:  Int, y1:  Int, x2:  Int, y2:  Int };

    zero_box = BOX { x1 => 0, y1 => 0, x2 => 0, y2 => 0 };

    fun miny (BOX { y1, ... } )
        =
        y1;

    # inBox:
    # returns TRUE if point is in box

    fun in_box (BOX { x1, y1, x2, y2 }, xgeometry::POINT { col=>x, row=>y } )
        =
        x2 >  x  and
        x  >= x1 and
        y2 >  y  and
        y  >= y1;

    # inside:
    # returns TRUE if first box is contained in second

    fun inside (BOX { x1, y1, x2, y2 }, BOX { x1=>x1', y1=>y1', x2=>x2', y2=>y2' } )
        =
        x1' <= x1 and
        y1' <= y1 and
        x2' >= x2 and
        y2' >= y2;

    # overlap:
    # returns TRUE if boxes overlap

    fun overlap (BOX { x1, y1, x2, y2 }, BOX { x1=>x1', y1=>y1', x2=>x2', y2=>y2' } )
        =
        x2 > x1' and
        x1 < x2' and
        y2 > y1' and
        y1 < y2';

    # boundBox:
    # returns bounding box of two boxes

    fun bound_box (BOX { x1, y1, x2, y2 }, BOX { x1=>x1', y1=>y1', x2=>x2', y2=>y2' } )
        =
        BOX
          { x1 => int::min (x1, x1'),
            x2 => int::max (x2, x2'),
            y1 => int::min (y1, y1'),
            y2 => int::max (y2, y2')
          };

      # offsetBox:
      # translate box by given vector

    fun offset_box (xgeometry::POINT { col=>x, row=>y } ) (BOX { x1, y1, x2, y2 } )
        =
        BOX { x1 => x1+x, x2 => x2+x, y1 => y1+y, y2 => y2+y };

    # xOffsetBox:
    # horizontally translate box
    #
    fun x_offset_box x (BOX { x1, y1, x2, y2 } )
        =
        BOX { x1 => x1+x, x2 => x2+x, y1, y2 };

    # yOffsetBox:
    # vertically translate box
    #
    fun y_offset_box y (BOX { x1, y1, x2, y2 } )
        =
        BOX { x1, x2, y1 => y1+y, y2 => y2+y };

};


## COPYRIGHT (c) 1994 by AT&T Bell Laboratories
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2013,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext