Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Quad p v = Nil
  2.               | Node (p, p) v
  3.                 (Quad p v) -- NW
  4.                 (Quad p v) -- NE
  5.                 (Quad p v) -- SE
  6.                 (Quad p v) -- SW
  7.                 deriving (Show)
  8.  
  9. insert :: (Ord p) => (p, p) -> v -> Quad p v -> Quad p v
  10. insert loc val Nil = Node loc val Nil Nil Nil Nil
  11. insert loc@(x, y) val (Node loc0@(x0, y0) val0 nw ne se sw) = Node loc0 val0 cnw cne cse csw
  12.     where
  13.         cnw = if x < x0 && y > y0 then insert loc val nw else nw
  14.         cne = if x >= x0 && y > y0 then insert loc val ne else ne
  15.         cse = if x >= x0 && y <= y0 then insert loc val se else se
  16.         csw = if x < x0 && y <= y0 then insert loc val sw else sw
  17.  
  18. region :: (Ord p) => (p, p) -> (p, p) -> Quad p v -> [v]
  19. region _ _ Nil = []
  20. region pl@(xl, yl) pr@(xr, yr) (Node loc@(x, y) val nw ne se sw) = pnw ++ pne ++ pse ++ psw ++ here
  21.     where
  22.         here = if x >= xl && x < xr && y >= yl && y < yr then [val] else []
  23.         pnw = if xl >= x || yr <= y then [] else region pl pr nw
  24.         pne = if xr < x  || yr <= y then [] else region pl pr ne
  25.         pse = if xr < x  || yl > y  then [] else region pl pr se
  26.         psw = if xl >= x || yl > y  then [] else region pl pr sw
  27.  
  28. quad = Nil
  29. quad1 = insert (0, 1) "a" quad
  30. quad2 = insert (-1, -1) "b" quad1
  31. quad3 = insert (7, 7) "c" quad2
  32. quad4 = insert (100, 100) "d" quad3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement