Hello, I'm a beginner who just started Python. I'm new to the idea of class, and I'm creating a class that's associated with square columns. I've almost finished everything else, but I don't know how to complete def overlaps and def is Inside, so I'm asking you a question. Overlaps expresses whether the two square pillars overlap with True and False, and even if the vertices overlap, isInside becomes True if the square pillars overlap completely, and it becomes True if the corners overlap with each other. I don't think it's that hard, but I don't know the algorithm, so I'm asking. I'd appreciate it if you could answer! I have attached the current code below :)
class Box:
def __init__(self, centerX = 0.0, centerY = 0.0, centerZ = 0.0, width = 1.0, height = 1.0, depth = 1.0):
self.centerX = centerX
self.centerY = centerY
self.centerZ = centerZ
self.width = width
self.height = height
self.depth = depth
def setCenter(self, x, y, z):
self.centerX = x
self.centerY = y
self.centerZ = z
def setWidth(self, width):
self.width = width
def setHeight(self, height):
self.height = height
def setDepth(self, depth):
self.depth = depth
def volume(self):
return self.width * self.height * self.depth
def surfaceArea(self):
return 2 * (self.width * self.height + self.width * self.depth + self.height * self.depth)
def overlaps(self, otherBox):
return
def isInside(self, otherBox):
return
def __repr__(self):
return '< {} by {} by {} 3D box centered at ({},{},{}) >'.format(self.width, self.height, self.depth, self.centerX, self.centerY, self.centerZ)
overlaps
has overlapping parts, isInside
is that one rectangular parallelepiped is contained in another rectangular parallelepiped.
I'm not sure. Just in case, just keep that in mind
overlap the case of
each from the center of a rectangular parallelepiped to the apex of any distance between the centre of two rectangular parallelepiped think will be shorter than the sum of
You can actually get each one
def overlaps(self, otherBox):
a = abs(self.x - otherBox.x) * 2 <= (self.width + otherBox.width)
b = abs(self.y - otherBox.y) * 2 <= (self.height + otherBox.height)
c = abs(self.z - otherBox.z) * 2 <= (self.depth + otherBox.depth)
if a and b and c:
return True
else:
return False
In this way, if the sum of the height, width, and depth of each of the two rectangular paralleles is longer than the distance between the centers of the two rectangular paralleles by the x-axis, y-axis, and z-axis, then the two rectangular paralleles overlap, right?
I think isInside
can be solved in a similar way.
We can solve it in the same way as above
© 2025 OneMinuteCode. All rights reserved.