Skip to content

Instantly share code, notes, and snippets.

@kjagiello
Last active December 11, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjagiello/4658250 to your computer and use it in GitHub Desktop.
Save kjagiello/4658250 to your computer and use it in GitHub Desktop.
fun translate(Rect(l1, t1, r1, b1), Rect(l2, t2, r2, b2)) =
Rect(abs(l1 - l2), abs(t1 - t2), abs(r1 - r2), abs(b1 - b2))
fun calculateRelativePosition(rt as Rect(l, t, r, b), offset) =
let
val width = r - l
val height = t - b
val Rect(x, y, _, _) = translate(rt, offset)
val cx1 = Int.toString(width div 2 + x)
val cx2 = Int.toString(height + y)
val cy1 = Int.toString(height div 2 + y)
val cy2 = Int.toString(width + x)
val x = Int.toString(x)
val y = Int.toString(y)
val width = Int.toString(width)
val height = Int.toString(height)
in
(x, y, width, height, cx1, cx2, cy1, cy2)
end
fun renderRectangle(rt, offset, color) =
let
val (x, y, width, height, _, _, _, _) = calculateRelativePosition(rt, offset)
in
"<rect x='" ^ x ^ "' y='" ^ y ^ "' width='" ^ width ^ "' height='" ^ height ^ "' style='stroke-width: 1px; fill: none; stroke: " ^ color ^ ";'/>"
end
fun renderRectangles([], offset) = ""
| renderRectangles(first::rest, offset) =
renderRectangle(first, offset, "blue") ^ renderRectangles(rest, offset)
fun render'(EmptyQuadTree, _) = ""
| render'(qt as Qt(e as Rect(l, t, r, b), h, v, q1, q2, q3, q4), offset) =
let
val (x, y, _, _, cx1, cx2, cy1, cy2) = calculateRelativePosition(e, offset)
in
renderRectangle(e, offset, "black")
^ "<line x1='" ^ cx1 ^ "' y1='" ^ y ^ "' x2='" ^ cx1 ^ "' y2='" ^ cx2 ^ "' style='stroke-width: 1px; stroke: black;'/>"
^ "<line x1='" ^ x ^ "' y1='" ^ cy1 ^ "' x2='" ^ cy2 ^ "' y2='" ^ cy1 ^ "' style='stroke-width: 1px; stroke: black;'/>"
^ renderRectangles(v, offset)
^ renderRectangles(h, offset)
^ render'(q1, offset)
^ render'(q2, offset)
^ render'(q3, offset)
^ render'(q4, offset)
end
(* render(qt)
TYPE: quadtree -> string
EXAMPLE:
val xxx = emptyQtree(Rect(~100, 100, 100, ~100));
val xxx = insert(xxx, Rect(10, ~10, 30, ~25));
render(xxx);
val xxx2 = emptyQtree(Rect(~1000, 1000, 1000, ~1000));
val xxx2 = insert(xxx2, Rect(10, ~10, 30, ~25));
val xxx2 = insert(xxx2, Rect(~90, ~30, ~10, ~400));
val xxx2 = insert(xxx2, Rect(100, 250, 300, 10));
render(xxx2);
*)
fun render(qt as Qt(Rect(l, t, r, b), _, _, _, _, _, _)) =
let
val width = Int.toString(r - l)
val height = Int.toString(t - b)
val offset = Rect(l, t, 0, 0)
in
"<svg xmlns='http://www.w3.org/2000/svg' width='" ^ width ^ "' height='" ^ height ^ "'>"
^ render'(qt, offset)
^ "</svg>"
end
(* renderToFile(qt, name)
TYPE: quadtree * string -> unit
EXAMPLE:
val xxx = emptyQtree(Rect(~100, 100, 100, ~100));
val xxx = insert(xxx, Rect(10, ~10, 30, ~25));
renderToFile(xxx, "test.svg");
val xxx2 = emptyQtree(Rect(~1000, 1000, 1000, ~1000));
val xxx2 = insert(xxx2, Rect(10, ~10, 30, ~25));
val xxx2 = insert(xxx2, Rect(~90, ~30, ~10, ~400));
val xxx2 = insert(xxx2, Rect(100, 250, 300, 10));
renderToFile(xxx2, "test2.svg");
*)
fun renderToFile(qt, name) =
let
val h = TextIO.openOut name
in
TextIO.outputSubstr(h, Substring.full(render(qt)));
TextIO.flushOut(h)
end
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment