(****************************************************************************** ** GeometryPrims2d.sml ** sml ** ** Franklin Chen and Guy Blelloch ** An implemetation of GEOMETRY_PRIMS parameterized on the type of number. ******************************************************************************) functor GeometryPrims2d (structure Number : NUMBER) :> GEOMETRY_PRIMS where Number = Number = struct structure Number = Number exception BadDimension structure Vec = struct structure Number = Number type vec = Number.t * Number.t type t = vec val zero : t = (Number.zero, Number.zero) val infinity = (Number.posInf, Number.posInf) fun fromSeq #[x, y] : t = (x, y) | fromSeq _ = raise BadDimension fun toSeq (x,y) = #[x, y] (* fun sub ((x, y), 0) = x | sub ((x, y), 1) = y *) fun (x, y) + (x', y') : t = let open Number in (x + x', y + y') end fun ~ (x, y) : t = let open Number in (~x, ~y) end fun (x, y) - (x', y') : t = let open Number in (x - x', y - y') end fun scale ((x, y) : t, a : Number.t) : t = let open Number in (a * x, a * y) end fun dot ((x, y), (x', y')) : Number.t = let open Number in x * x' + y * y' end fun cross #[(x,y) : t] = let open Number in (y, ~x) end | cross _ = raise BadDimension (* Derived form. *) fun norm (p : t) : Number.t = Number.sqrt (dot (p, p)) fun == ((x, y), (x', y')) = let open Number in == (x, x') andalso == (y, y') end fun != ((x, y), (x', y')) = let open Number in != (x, x') orelse != (y, y') end fun toString(x,y) = ("(" ^ Number.toString(x) ^ ", " ^ Number.toString(y) ^ ")") end structure Point = PointFromVec(structure Vec = Vec) fun minCoords((x1,y1),(x2,y2)) = (Number.min(x1,x2),Number.min(y1,y2)) fun maxCoords((x1,y1),(x2,y2)) = (Number.max(x1,x2),Number.max(y1,y2)) exception NYI type plane = (Vec.t*Number.t) fun planeNormal (normal, offset) = normal fun planeFromPoints points = let val #[(x0,y0),(x1,y1)] = points open Number val v = ((y0-y1),(x1-x0)) val offset = (x0*y1-x1*y0) in (v,offset) end fun planeDistance (normal, offset) point = let val det = Vec.dot(normal,point) in Number.-(det,offset) end fun planeSide (normal, offset) point = case Number.compare((planeDistance (normal, offset) point),Number.zero) of LESS => Side.In | GREATER => Side.Out | EQUAL => Side.On fun planeEqual((n1,o1),(n2,o2)) = Vec.==(n1,n2) andalso Number.==(o1,o2) fun planeInvert(normal,offset) = (Vec.~ normal, offset) fun planeToString(a) = raise NYI fun planeFromNormal(a) = raise NYI type sphere = unit fun sphereFromPoints _ = raise NYI fun sphereSide _ _ = raise NYI end