## coordinate.pkg
## Author: stefan (Last modification by $Author: 2cxl $)
## (C) 1996, Bremen Institute for Safe Systems, Universitaet Bremen
# Compiled by:
#
src/lib/tk/src/tk.sublib# **************************************************************************
# Coordinates for tk
# **************************************************************************
package coordinate
: (weak) Coordinate # Coordinate is from
src/lib/tk/src/coordinate.api{
stipulate
include package basic_tk_types;
include package basic_utilities;
herein
exception COORD String;
fun show col
=
{
fun show_int i
=
if ( i >= 0 ) (int::to_string i);
else ("-" + (int::to_string (i * -1)));fi;
sl
=
map (\\ ((x, y):Coordinate) = (show_int x) + " " + (show_int y))
col;
string::join " " sl;
};
fun read str
=
{
debug::print 5 ("coordinate::read: \"" + str + "\"");
cos = string_util::words str;
fun dezip [] => [];
dezip (x . y . l) =>
{
x' = string_util::to_int x;
y' = string_util::to_int y;
(x', y') . (dezip l);
};
dezip _ => raise exception COORD "coordinate::read: odd number of coordinates"; end;
(dezip cos) except OVERFLOW => raise exception COORD "coordinate::read: number conversion error"; end ;
};
fun add (x1: Int, y1: Int) (x2, y2)
=
(x1 + x2, y1 + y2);
# old: fun sub (x1, y1) (x2, y2) = (max (0, x1- x2), max (0, y1-y2))
fun sub (x1: Int, y1: Int) (x2, y2)
=
(x1- x2, y1-y2);
# scalar multiplication
fun smult (x1: Int, y1: Int) x
=
(x*x1, y1*x);
# rectangles
Box = (Coordinate, Coordinate);
fun between x (y: Int) z = (x <= y) and (y<= z);
fun inside (u: Int, v: Int) ((x1: Int, y1: Int), (x2, y2))
=
(between x1 u x2)
and
(between y1 v y2);
# Intersection of rectangles.
# r1 intersects with r2 if any of the four corners of r2 is inside
# r2, or the other way around.
# Probably can be done shorter.
fun intersect r1 r2
=
{ fun inter r1 ((x1, y1), (x2, y2))
=
list::exists (\\ p=> inside p r1; end )
[(x1, y1), (x1, y2), (x2, y1), (x2, y2)];
(inter r1 r2)
or
(inter r2 r1);
};
fun move_box (p1, p2) p3
=
( add p1 p3,
add p2 p3
);
fun show_box ((x1: Int, y1: Int), (x2: Int, y2: Int))
=
"[" + (int::to_string x1) + ", " + (int::to_string y1) + ";"
+ (int::to_string x2) + ", " + (int::to_string y2) + "] ";
end;
};