PreviousUpNext

15.4.1028  src/lib/std/2d/geometry2d-float.pkg

/*
api Geometry2d_Float {

# Compiled by:
#     src/lib/std/standard.lib

    Rpoint = RPT  { x:  Float, y:  Float };
    Rsize = RSIZE { wid:  Float, ht:  Float };
    Rrect = RRECT  { x:  Float, y:  Float, wid:  Float, ht:  Float };
    rorigin_pt:  Rpoint;
    origin_of_box:  Rrect -> Rpoint;
    corner_of_box:  Rrect -> Rpoint;
    rrect:  geometry::Box -> Rrect;
    box:  Rrect -> geometry::Box;
    bound_box:  List( Rpoint ) -> Rrect;
    intersect:  (Rrect, Rrect) -> Bool;
};
*/

# Compiled by:
#     src/lib/x-kit/tut/show-graph/show-graph-app.lib

# This package is like
#     src/lib/std/2d/geometry2d.api
# except that it uses Float not Int type
# point coordinates:

# Throughout this file "r" == "real", which should be changed to "float".  XXX BUGGO FIXME

stipulate
    package f8b =  eight_byte_float;                                    # eight_byte_float      is from   src/lib/std/eight-byte-float.pkg
    package g2d =  geometry2d;                                          # geometry2d            is from   src/lib/std/2d/geometry2d.pkg
herein

    package geometry2d_float {
        #
        Point =        { x:     Float,
                         y:     Float
                       };

        Size  = SIZE   { wide:  Float,
                         high:  Float
                       };

        Box   = BOX    { x:     Float,
                         y:     Float,
                         wide:  Float,
                         high:  Float
                       };

        point_zero
            =
                  { x => 0.0,
                    y => 0.0
                  };


        fun upperleft_of_box (BOX { x, y, ... } )
            =
            { x, y };


        fun lowerright_of_box (BOX { x, y, wide, high } )
            =
            { x => x+wide,
              y => y+high
            };


        fun from_box ({ col=>x, row=>y, wide, high } )
            = 
            BOX { x => float(x),
                  y => float(y),
                  wide => float(wide),
                  high => float(high)
                };


        fun to_box (BOX { x, y, wide, high } )
            = 
            { col  =>  f8b::truncate  x,
              row  =>  f8b::truncate  y,
              #
              wide =>  f8b::truncate  wide,
              high =>  f8b::truncate  high
            };


        fun bound_box []
                =>
                BOX
                  { x    => 0.0,
                    y    => 0.0,
                    wide => 0.0,
                    high => 0.0
                  };

            bound_box (({ x, y } ) ! points)
                =>
                bb (x, y, x, y, points)
                where
                    fun min  (a: Float,  b) =  (a < b  ??  a  ::  b);
                    fun max  (a: Float,  b) =  (a > b  ??  a  ::  b);

                    fun bb (minx, miny, maxx, maxy, [])
                           =>
                           BOX
                             { x => minx,
                               y => miny,
                               #
                               wide => maxx - minx + 1.0,
                               high => maxy - miny + 1.0
                             };

                        bb (minx, miny, maxx, maxy, ({ x, y } ) ! points)
                            =>
                            bb ( min (minx, x),
                                 min (miny, y),
                                 max (maxx, x),
                                 max (maxy, y),
                                 points
                               );
                    end;
                end;
        end;


        fun intersect
              ( BOX { x=>x1, y=>y1, wide=>w1, high=>h1 },
                BOX { x=>x2, y=>y2, wide=>w2, high=>h2 }
              )
            =
            x1 < x2+w2   and
            y1 < y2+h2   and
            x2 < x1+w1   and
            y2 < y1+h1;
    };

end;


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext