-
My code assumes all the items returned from the packer to be in the same order as they were passed in. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @jjblock21 ! There is no guarantee the rectangles will be returned in the same order after packing, that's what the id exists for. This is because the algorithm internally sorts the rectangles differently, and sorting back to their original position takes time and performance, and not all people need it. My suggestion is to set the rectangle ids to their index in the array before sorting, and then use that to number to know where they were originally. If you don't want to have to change the rest of your code, you may set the ids before packing and sort back to their original order afterwards like so: PackingRectangle[] rectangles = ...
for (int i = 0; i < rectangles.Length; i++)
rectangles[i].Id = i;
RectanglePacker.Pack(rectangles, out PackingRectangle bounds);
Array.Sort(rectangles, (x, y) => x.Id.CompareTo(y.Id)); (I haven't tested that code, but the idea is to set each ids to the array index and then sort by id, bringing them back to their original indeces) Hope that helps! |
Beta Was this translation helpful? Give feedback.
Hi @jjblock21 ! There is no guarantee the rectangles will be returned in the same order after packing, that's what the id exists for.
This is because the algorithm internally sorts the rectangles differently, and sorting back to their original position takes time and performance, and not all people need it.
My suggestion is to set the rectangle ids to their index in the array before sorting, and then use that to number to know where they were originally. If you don't want to have to change the rest of your code, you may set the ids before packing and sort back to their original order afterwards like so: