//private Tuple<int, int> GetChildrenLocation(Point point) //{ // bool leftx = LeftX(point); // bool topy = TopY(point); // int ix = leftx ? 0 : 1; // int iy = topy ? 0 : 1; // return new Tuple<int, int>(ix, iy); //} public QuadNode <T> Insert(Point point) { bool leftx = LeftX(point); bool topy = TopY(point); int ix = leftx ? 0 : 1; int iy = topy ? 0 : 1; if (Children[ix, iy] != null) { return(Children[ix, iy]); } Point p1; Point p2; var middle = GetMiddle(); if (leftx && topy) { p1 = Boundary[0]; p2 = middle; } else if (leftx && !topy) { p1 = new Point(Boundary[0].X, middle.Y); p2 = new Point(middle.X, Boundary[1].Y); } else if (!leftx && topy) { p1 = new Point(middle.X, Boundary[0].Y); p2 = new Point(Boundary[1].X, middle.Y); } else { p1 = middle; p2 = Boundary[1]; } Children[ix, iy] = new QuadNode <T>(p1, p2); return(Children[ix, iy]); }
public bool Insert(QuadNode <T> subroot, Point point, T val) { if (subroot == null) { return(false); } if (!subroot.WithinBoundary(point)) { return(false); } if (!subroot.CanSubdivide(Resolution)) { // Add the node subroot.Value = val; return(true); } return(Insert(subroot.Insert(point), point, val)); }
public QuadTree(Point topLeft, Point botRight, int res = 1) { Resolution = res; Root = new QuadNode <T>(topLeft, botRight); }