Get X and Y sizes of remaining surface area

95 Views Asked by At

I have bins that I'm packing components into, but have trouble with components of smaller sizes not "using" the available space in the current bin (so I am creating a new bin when it isn't required).

    private bool ComponentFitsInBin(PackedComponent component, Bin bin)
    {
        if (bin.RemainingSurfaceArea - component.GetSurfaceArea() < 0)
            return false;
        if (component.SizeX > bin.SizeX)
            return false;
        if (component.SizeY > bin.SizeY)
            return false;

        if (bin.SizeX - bin.PackedComponents.Sum(p => p.SizeX) <= component.SizeX)
            if (bin.SizeY - bin.PackedComponents.Sum(p => p.SizeY) <= component.SizeY)
                return false;

        return true;
    }

I realize that the last 2 if's with the Sum calculations are causing the problem, but I'm not sure how to better detect my case.

Below is a visual representation of the result for my test case: the small component in the second bin should be inside the first.

second bin's contents should be in the first bin

The shapes (both bins and components) are always rectangles or squares. The components can be rotated 90 degrees but never anything that causes a diagonal line.

I have a feeling it might be required to store the location of the already-placed components but I don't know how I would go about determining that location.

If I remove the check that I think is causing the problem, I get the following results:

enter image description here

As you can see the first bin has a component that's falling outside of the bin because the surface area is big enough but not the correct shape (ignore the numbers, they are required for the project and I only just now added them).

0

There are 0 best solutions below