Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a Box class, representing rectangular 3D boxes. The boxes can be any s

ID: 3806644 • Letter: 1

Question

1. Create a Box class, representing rectangular 3D boxes. The boxes can be any size and can be located anywhere in 3D space, but in this simple version their orientations are fixed so that their edges are always aligned with/parallel to coordinate system axes. You class must implement all the methods below:

__init__(self, centerX = 0.0, centerY = 0.0, centerZ = 0.0, width = 1.0, height = 1.0, depth = 1.0)

setCenter(self, x, y, z)

setWidth(self, width)

setHeight(self, height)

setDepth(self, depth)

volume(self)

surfaceArea(self)

overlaps(self, otherBox)

contains(self, otherBox)

__repr__(self)

Note that "width" is a box's extent along the x dimension, while "height" and "depth" are the y and z extents, respectively. box1.overlaps(box2) should return True if the two 3D boxes touch/intersect at all (even they just touch exactly at their edges or corners). box1.contains(box2) should return True if no point of box2 is outside of box1 Note: think carefully about the overlaps and contains test. It is easy to get these wrong. I recommend (before you start coding) sketching some pictures to help you analyze the possibilities.

Use of the class in a Python shell might look like this:

As part of your .py file for this problem, include a testBox function that demonstrates use of the Box class, printing helpful output.

Explanation / Answer

class Box:
   def __init__ ( self, centerX = 0.0, centerY = 0.0, centerZ = 0.0, width = 1.0, height = 1.0, depth = 1.0):
       self.x = centerX
       self.y = centerY
       self.z = centerZ
       self.w = width
       self.h = height
       self.d = depth
      
   def setCenter(self, x, y, z):
       self.x = x
       self.y = y
       self.z = z

   def setWidth(self, width):
       self.w = width
      
   def setHeight(self, height):
       self.h = height

   def setDepth(self, depth):
       self.d = depth

   def volume(self):
       return self.w * self.h * self.d

   def surfaceArea(self):
       return 2.0*(self.w + self.h + self.d)

   def overlaps(self, otherBox):
       otherMaxX = otherBox.x+(otherBox.w/2.0)
       otherMaxY = otherBox.y+(otherBox.h/2.0)
       otherMaxZ = otherBox.z+(otherBox.d/2.0)
      
       otherMinX = otherBox.x-(otherBox.w/2.0)
       otherMinY = otherBox.y-(otherBox.h/2.0)
       otherMinZ = otherBox.z-(otherBox.d/2.0)
      
       if( (self.x < otherMinX) or (self.y < otherMinY) or (self.z < otherMinZ) or (self.y > otherMaxY) or (self.z > otherMaxZ) or (self.x > otherMaxX) ):
           return False
       return True
      
   def contains(self, otherBox):
       otherMaxX = otherBox.x+(otherBox.w/2.0)
       otherMaxY = otherBox.y+(otherBox.h/2.0)
       otherMaxZ = otherBox.z+(otherBox.d/2.0)
      
       otherMinX = otherBox.x-(otherBox.w/2.0)
       otherMinY = otherBox.y-(otherBox.h/2.0)
       otherMinZ = otherBox.z-(otherBox.d/2.0)
      
       if( (self.x < otherMinX) or (self.y < otherMinY) or (self.z < otherMinZ) or (self.y > otherMaxY) or (self.z > otherMaxZ) or (self.x > otherMaxX) ):
           return False
       if(self.w<otherBox.w or self.h < otherBox.h or self.d < otherBox.d):
           return False
       return True
      
   def __repr__(self):
       return "centerX= "+str(self.x)+" centerY="+str(self.y)+" centerZ="+str(self.z)+" width="+str(self.w)+" height="+str(self.h)+" depth="+str(self.d)+" "
  
  
def testBox():
   box1 = Box(10.0, 5.0, 0.0, 2.0, 1.0, 1.0)
   print box1
   box2 = Box(0, 0, 0, 3.5, 2.5, 1.0)
   print box1.volume()
   print box2.surfaceArea()
   print box1.overlaps(box2)
  
   box1.setCenter(2.75, 0.0, 0.0)
   print box1.overlaps(box2)
          
   box1.setCenter(2.76, 0.0, 0.0)
   print box1.overlaps(box2)
  
   box1.setCenter(2.75, 1.75, 1.0)
   print box1.overlaps(box2)
  
   box1.setCenter(0.0, 0.0, 0.0)
   print box1.overlaps(box2)
  
   box1.setWidth(50.0)
   box1.overlaps(box2)
  
   box1.setDepth(50.0)
   print box2.overlaps(box1)
  
   box3 = Box(0, 0, 0)
   print box2.contains(box3)
  
   box4 = Box(10.0, 5.0, 0.0, 2.0, 1.0, 1.0)
   print box4.contains(box3)
  
def main():
   box1 = Box(10.0, 5.0, 0.0, 2.0, 1.0, 1.0)
   print box1
   testBox()
  
main()