public QuadtreeRoot(List <Item> items, int depth, float leftB, float topB, float rightB, float bottomB) { //remove Squares outside our boundaries List <Item> tmpList = new List <Item>(); foreach (Item item in items) { if (leftB > item.right || rightB < item.left || topB < item.bottom && bottomB > item.top) { //item outside continue; } tmpList.Add(item); } tree = new Quadtree(tmpList, depth, leftB, topB, rightB, bottomB); this.leftB = leftB; this.topB = topB; this.rightB = rightB; this.bottomB = bottomB; this.depth = depth; }
List <int> landuses; // public Quadtree(List <Item> items, int depth, float leftB, float topB, float rightB, float bottomB) { depth--; landuses = new List <int>(); if (depth == 0) // If we've reached the maximum depth then insert all items into this quadrant. { foreach (Item i in items) { if (!landuses.Contains(i.landuseId)) { landuses.Add(i.landuseId); } } return; } cx = Convert.ToSingle((leftB + rightB) * 0.5); // Find this quadrant's centre. cy = Convert.ToSingle((topB + bottomB) * 0.5); List <Item> nw_items = new List <Item>(); List <Item> sw_items = new List <Item>(); List <Item> se_items = new List <Item>(); List <Item> ne_items = new List <Item>(); foreach (Item item in items) //give items to children { //check if node is in item if (item.left < leftB && item.top > topB && item.right > rightB && item.bottom < bottomB) { if (!landuses.Contains(item.landuseId)) { landuses.Add(item.landuseId); } continue; } //Which side to insert item? bool in_nw = (item.left <= cx && item.top >= cy); bool in_sw = (item.left <= cx && item.bottom <= cy); bool in_ne = (item.right >= cx && item.top >= cy); bool in_se = (item.right >= cx && item.bottom <= cy); if (in_nw) { nw_items.Add(item); } if (in_ne) { ne_items.Add(item); } if (in_se) { se_items.Add(item); } if (in_sw) { sw_items.Add(item); } } //Create the sub-quadrants, recursively. if (nw_items.Count > 0) { nw = new Quadtree(nw_items, depth, leftB, topB, cx, cy); } if (ne_items.Count > 0) { ne = new Quadtree(ne_items, depth, cx, topB, rightB, cy); } if (se_items.Count > 0) { se = new Quadtree(se_items, depth, cx, cy, rightB, bottomB); } if (sw_items.Count > 0) { sw = new Quadtree(sw_items, depth, leftB, cy, cx, bottomB); } }
public void insertItems(List <Item> items, int depth, float leftB, float topB, float rightB, float bottomB) { // If we've reached the maximum depth then insert all items into this // quadrant. depth--; if (depth == 0) { foreach (Item i in items) { if (!landuses.Contains(i.landuseId)) { landuses.Add(i.landuseId); } } return; } // Find this quadrant's centre. cx = Convert.ToSingle((leftB + rightB) * 0.5); cy = Convert.ToSingle((topB + bottomB) * 0.5); List <Item> nw_items = new List <Item>(); List <Item> sw_items = new List <Item>(); List <Item> se_items = new List <Item>(); List <Item> ne_items = new List <Item>(); foreach (Item item in items) { //check if landuse already in landuses if (landuses.Contains(item.landuseId)) { continue; } //check if node is in item if (item.left < leftB && item.top > topB && item.right > rightB && item.bottom < bottomB) { landuses.Add(item.landuseId); continue; } //Which of the sub-quadrants does the item overlap? bool in_nw = (item.left <= cx && item.top >= cy); bool in_sw = (item.left <= cx && item.bottom <= cy); bool in_ne = (item.right >= cx && item.top >= cy); bool in_se = (item.right >= cx && item.bottom <= cy); if (in_nw) { nw_items.Add(item); } if (in_ne) { ne_items.Add(item); } if (in_se) { se_items.Add(item); } if (in_sw) { sw_items.Add(item); } } //Create the sub-quadrants, recursively. if (nw_items.Count > 0) { if (nw == null) { nw = new Quadtree(nw_items, depth, leftB, topB, cx, cy); } else { nw.insertItems(nw_items, depth, leftB, topB, cx, cy); } } if (ne_items.Count > 0) { if (ne == null) { ne = new Quadtree(ne_items, depth, cx, topB, rightB, cy); } else { ne.insertItems(ne_items, depth, cx, topB, rightB, cy); } } if (se_items.Count > 0) { if (se == null) { se = new Quadtree(se_items, depth, cx, cy, rightB, bottomB); } else { se.insertItems(se_items, depth, cx, cy, rightB, bottomB); } } if (sw_items.Count > 0) { if (sw == null) { sw = new Quadtree(sw_items, depth, leftB, cy, cx, bottomB); } else { sw.insertItems(sw_items, depth, leftB, cy, cx, bottomB); } } }