Search code examples
javadatabasealgorithmsizedimensions

2D Dimensions, quantities and packing algorithms?


Been looking online and changing how I word my question to find something similar, but not entirely sure how to approach this problem properly in Java. I have a web app that takes in a 2D dimension of (A x B) and I then go to my database and retrieve all items with a dimension bigger than or equal to (A x B). I then find the best cost item and return that along with the total multiplied by the quantity requested.

But just now if for example I have a cheap item of 1000 x 1000 but I have requested 10 of 100 x 100. The 1000 x 1000 will be returned as it is the best cost but I am then setting the total to 10 of 1000 x 1000 which is way too much.

How is best to take that large dimension no matter what it is and try figure out how many of the requested size can be split from it and drop the total x quantity to a smarter amount?

Hope this makes sense.

So far, I have tried

private Double FindSmartTotal(double cheapestTotal, String sizeDB, String desc, Integer quantity) { 
    desc = LookForSizes(desc);
    if(desc!=null){
        LookForSizesOfDB(sizeDB);
        LookForSizesOfDescription(desc);
        Dimension d1 = new Dimension(dbitemSizeA, dbitemSizeB);
        Dimension d2 = new Dimension(homeviewitemSizeA, homeviewitemSizeB);
        if(d1.getWidth() > d2.getWidth() && d1.getHeight( )> d2.getHeight()){
            double dividedWidth = d1.getWidth()/d2.getWidth();
            double dividedHeight = d1.getHeight()/d2.getHeight();
            double roundedWidth = round(dividedWidth,1, BigDecimal.ROUND_HALF_UP);
            double roundedHeight = round(dividedHeight,1, BigDecimal.ROUND_HALF_UP);
            System.out.println("here");
        }
    }
    return null;
}

but I do not know what I would do next, or if there is a better way to approach it and scrap the code above for new.


Solution

  • Figured this one out. More maths based.

    Multiply both dimensions to get an area for each.

    Then for loop, with each loop subtracting the smaller area from the larger one and incrementing a counter. Do this until the subtraction is >0. This will supply the quantity than can be cut from the larger area.