# quadtree - #load"str.cma module type PT = sig A 2D point...

This preview shows pages 1–3. Sign up to view the full content.

#load "str.cma";; module type PT = sig (* A 2D point *) type t = float * float (* Returns: dist_sq p1 p2 returns the square of the distance from p1 to p2. *) val dist_sq : t -> t -> float (* Returns: dist_sq p1 p2 returns the distance from p1 and p2 *) val dist : t -> t -> float end module Pt : PT = struct (* Abstraction function: (x, y) is a point with coordinates x and y. *) type t = float * float (* dist_sq p1 p2 returns the square of the distance for p1 and p2. *) let dist_sq ((x1,y1) : t) ((x2,y2) : t) : float = (x1 -. x2)**2.0 +. (y1 -. y2)**2.0 let dist (p1 : t) (p2 : t) : float = sqrt (dist_sq p1 p2) end module type RECT = sig (* A rectangle *) type t = Pt.t * Pt.t (* Returns: the upper-left quadrant of the given rectangle *) val nw_quad : t -> t (* Returns: the upper-right quadrant of the given rectangle *) val ne_quad : t -> t (* Returns: the bottom-right quadrant of the given rectangle *) val se_quad : t -> t (* Returns: the bottom-left quadrant of the given rectangle *) val sw_quad : t -> t (* Returns: contains r pt returns true if pt is contained in r *) val contains : t -> Pt.t -> bool (* Returns: intersects r1 r2 returns true if r1 and r2 intersect *) val intersects : t -> t -> bool end module Rect : RECT = struct (* Abstraction function: ((x1, y1), (x2, y2)) is a rectangle containing any point (x, y) such that x1 <= x <= x2 and y1 <= y <= y2. Representation invariant:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
x1 <= x2 and y1 <= y2. *) type t = Pt.t * Pt.t (* rep_ok r is the identity function. Checks: r satisfies the representation invariant for a rectangle *) let rep_ok (((x1, y1), (x2, y2)) as r) = (assert(x1 <= x2); assert(y1 <= y2); r) let avg x y = (x +. y) /. 2. let nw_quad ((x1,y1), (x2, y2)) = rep_ok ((x1, avg y1 y2), (avg x1 x2, y2)) let ne_quad (((x1,y1), (x2, y2)) : t) : t = rep_ok ((avg x1 x2, avg y1 y2), (x2, y2)) let se_quad (((x1,y1), (x2, y2)) : t) : t = rep_ok ((avg x1 x2, y1), (x2, avg y1 y2)) let sw_quad (((x1,y1), (x2, y2)) : t) : t = rep_ok ((x1, y1), ((avg x1 x2), (avg y1 y2))) let contains (((x1, y1), (x2, y2)) : t) ((x, y) : Pt.t) : bool = let intersects (((x1, y1), (x2, y2)) : t) (((x3, y3), (x4, y4)) : t) : bool = let interval_intersects (x1, x2) (y1, y2) =
This is the end of the preview. Sign up to access the rest of the document.