public void insertItems(List <Item> items) { tree.insertItems(items, depth, leftB, topB, rightB, 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); } } }