private void PlaceImages(Bitmap[] images, ref int[] x, ref int[] y) { Node start = new Node(); start.rect = new Rect(0, 0, SIZE, SIZE); for (int i = 0; i < images.Length; i++) { Node thisnode = start.Insert(new Rect(0, 0, images[i].Width, images[i].Height)); if(thisnode != null) { x[i] = thisnode.rect.x; y[i] = thisnode.rect.y; } } }
private void PlaceImages(Bitmap[] images, ref int[] x, ref int[] y, ref Texture[] texs) { Node start = new Node(); start.rect = new Rect(0, 0, SIZE, SIZE); for (int i = 0; i < images.Length; i++) { Node thisnode = start.Insert(new Rect(0, 0, images[i].Width, images[i].Height)); if (thisnode != null) { x[i] = thisnode.rect.x; y[i] = thisnode.rect.y; texs[i].u1 = (float)x[i] / (float)SIZE; texs[i].v1 = (float)y[i] / (float)SIZE; texs[i].u2 = (float)(x[i] + thisnode.rect.w) / (float)SIZE; texs[i].v2 = (float)(y[i] + thisnode.rect.h) / (float)SIZE; } } }
public Node Insert(Rect other) { //check if has a child, if, then check left when null(nofit) check right //if r == null, return null under this node there is no place //when this a left child parent gets null and checks right or if this right child parent has no place if (l != null) { Node nodeLeft = l.Insert(other); if (nodeLeft != null) return nodeLeft; return r.Insert(other); } //there is a rect here if (filled) return null; //this node is to small if (!other.FitsIn(this.rect)) return null; //the img fits exact if (other.Compare(this.rect) == 0) { filled = true; return this; } //make two childs l = new Node(); r = new Node(); int diffWidth = rect.w - other.w; int diffHeight = rect.h - other.h; //split hor or ver if (diffWidth > diffHeight) { l.rect = new Rect(rect.x, rect.y, other.w, rect.h); r.rect = new Rect(rect.x + other.w, rect.y, rect.w - other.w, rect.h); } else { l.rect = new Rect(rect.x, rect.y, rect.w, other.h); r.rect = new Rect(rect.x, rect.y + other.h, rect.w, rect.h - other.h); } //fit img in leaf(left child, leaf == node where (l == null && r == null)) return l.Insert(other); }
public Node() { l = null; r = null; filled = false; this.rect = null; }