RectanglePackerObject cloneNode(RectanglePackerObject n) { var P = new RectanglePackerObject() { ReferencedObject = n.ReferencedObject, X = n.X, Y = n.Y, Width = n.Width, Height = n.Height }; AllNodes.Add(P); return(P); }
PointD recursiveFindCoords(RectanglePackerObject node, double w, double h) { if (node.lft != null) { var coords = recursiveFindCoords(node.lft, w, h); if (coords == null && (node.rgt != null)) { coords = recursiveFindCoords(node.rgt, w, h); } return(coords); } else { if (node.used || w > node.Width || h > node.Height) { return(null); } if (w == node.Width && h == node.Height) { node.used = true; return(new PointD(node.X, node.Y)); } node.lft = cloneNode(node); node.rgt = cloneNode(node); if (node.Width - w > node.Height - h) { node.lft.Width = w; node.rgt.X = node.X + w; node.rgt.Width = node.Width - w; } else { node.lft.Height = h; node.rgt.Y = node.Y + h; node.rgt.Height = node.Height - h; } return(recursiveFindCoords(node.lft, w, h)); } }