I am working on a code that focuses on efficiently packing boxes into a package car. Right now, I am in the beginning stages of the code. I'm in the process of learning python while doing this code. I have generated a set of boxes and sorted them by their bottom surface area. I am now trying to plot them in a way where the largest surface area is placed first and the rest follow.
This is what I have so far, but I am getting an error when I try to generate the coordinates for the rectangles. I know there has to be more to it. For example, once the first box is placed the second has to be placed where the the end of the surface is. UPDATED CODE 5:01 PM EST. No error message, but not plotting the rectangles
#Random Integer/Number Generator
import numpy
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patches as patches
#%matplotlib inline
import random
def createBox():
#this is what randomly generates the dimensions of the boxes and saves to list
N= int(input ("Enter number of boxes :"))
list1 = [];
list2 = [];
list3 = [];
list4 = [];
for i in range(0,N):
x = random.randint(1,4) #non integers would be random.random or random.unif
y = random.randint(1,4)
z = random.randint(1,4)
box = [x,y,z];
area = [x,y]
list1.append(box)
list2.append(area)
print(list1)
print(list2)
#Here is where the organizes the dimensions of the box
for i in list2:
result = numpy.prod(i)
list3.append(result)
list3.sort(reverse = True)
print(list3)
for box in list2:
l = box[0]
w = box[1]
res = [l,w]
list4.append(res)
list4.sort(reverse = True)
print(list4)
lims = (0,100)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect = 'equal')
#ax1.add_patch(patches.Rectangle((0,0),list4[0][0],list4[0][1]))
plt.ylim(lims)
plt.xlim(lims)
patchlist = [];
for box in list4:
x = box[0]
y = box[1]
patchlist.append(patches.Rectangle((x,y),0,0))
ax1.add_collection(PatchCollection(patchlist))
plt.show()
I got the bottom code from How to speed up the plot of a large number of rectangles with Matplotlib? .
I'm not sure if it makes complete sense, but any guidance would be greatly appreciated!
Thanks!