更改实例变量后,类功能不是最新的

时间:2018-03-30 02:12:39

标签: python function class variables instance

很抱歉我的代码长度。此分配处理3D平面上的矩形。该类在用户输入centerX,centerY,centerZ,width,height和depth的xyz轴上创建一个包含8个点的框。类中的另一个函数setCenter更改centerX,centerY和centerZ。在下面的示例中,box1.overlaps(box2)应该返回True。

>box1 = Box(10.0, 5.0, 0.0, 2.0, 1.0, 1.0)
>
>box2 = Box(0, 0, 0, 3.5, 2.5, 1.0)
>
>box1.overlaps(box2)
>Out[4]: False
>
>box1.setCenter(2.75, 0.0, 0.0)
>
>box1.overlaps(box2)
>Out[6]: False

如果我初始化带有中心的方框,它会被更改为,它会返回True

>box1 = Box(2.75,0,0,2,1,1)
>
>box1.overlaps(box2)
>Out[10]: True

我该如何解决这个问题?这是因为我正在使用实例变量,我应该使用类变量吗?如果是这样我怎么能这样做?

编辑:更新以更改点仍然无法使其按预期工作

class Box:
    def __init__(self, centerX, centerY, centerZ, width, height, depth):
        self.prop = [centerX,centerY,centerZ,width,height,depth]
        self.topleft1 = [(centerX - (0.5*width)), 
            (centerY + (0.5*height)),
            (centerZ + (0.5*depth))]
        self.topright1 = [(centerX + (0.5*width)), 
            (centerY + (0.5*height)),
            (centerZ + (0.5*depth))]
        self.bottomleft1 = [(centerX - (0.5*width)), 
            (centerY - (0.5*height)),
            (centerZ + (0.5*depth))]
        self.bottomright1 = [(centerX + (0.5*width)), 
            (centerY - (0.5*height)),
            (centerZ + (0.5*depth))]
        self.topleft2 = [(centerX - (0.5*width)), 
            (centerY + (0.5*height)),
            (centerZ - (0.5*depth))]
        self.topright2 = [(centerX + (0.5*width)),
            (centerY + (0.5*height)),
            (centerZ - (0.5*depth))]
        self.bottomleft2 = [(centerX - (0.5*width)), 
        (centerY - (0.5*height)),
        (centerZ - (0.5*depth))]
        self.bottomright2 = [(centerX + (0.5*width)), 
                (centerY - (0.5*height)),
                (centerZ - (0.5*depth))]
        self.coord = [self.topleft1,self.topright1,self.bottomleft1,self.bottomright1,self.topleft2,
                  self.topright2,self.bottomleft2,self.bottomright2]
    def setCenter(self, x1, y1, z1):
        self.prop[0] = x1
        self.prop[1] = y1
        self.prop[2] = z1
        self.topleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.topright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
    def setWidth(self, width):
        self.prop[3] = width
        self.topleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.topright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
    def setHeight(self, height):
        self.prop[4] = height
        self.topleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.topright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
    def setDepth(self, depth):
        self.prop[5] = depth
        self.topleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomleft1 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.bottomright1 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] + (0.5*self.prop[5]))]
        self.topleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.topright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] + (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomleft2 = [(self.prop[0] - (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
        self.bottomright2 = [(self.prop[0] + (0.5*self.prop[3])), 
        (self.prop[1] - (0.5*self.prop[4])),
        (self.prop[2] - (0.5*self.prop[5]))]
    def volume(self):
        return (self.prop[3]*self.prop[4]*self.prop[5])
    def surfaceArea(self):
        return (self.prop[3]*self.prop[4])*2+(self.prop[3]*self.prop[5])*2+(self.prop[4]*self.prop[5])*2
    def insideOf(self, point):
        if all([point[0]>=self.topleft1[0],point[0]<=self.topright1[0],point[1]>=self.bottomleft1[1],point[1]<=self.topleft1[1],point[2]>=self.topleft2[2],point[2]<=self.topleft1[2]]):
            return True
        else:
            return False
    def overlaps(self, otherBox):
        result = False
        for i in otherBox.coord:
            if self.insideOf(i) == True:
                result = True
        for i in self.coord:
            if otherBox.insideOf(i) == True:
                result = True
        return result
    def contains(self, otherBox):
        if self.insideOf(otherBox.topleft1) and self.insideOf(otherBox.bottomright1) and self.insideOf(otherBox.topleft2) and self.insideOf(otherBox.bottomright2) == True:
            return True
        else:
            return False
    def __repr__(self):
        if self.prop[3] == int(self.prop[3]):
            self.x = int(self.prop[3])
        else:
            self.x = self.prop[3]
        if self.prop[4] == int(self.prop[4]):
            self.y = int(self.prop[4])
        else:
            self.y = self.prop[4]
        if self.prop[5] == int(self.prop[5]):
            self.z = int(self.prop[5])
        else:
            self.z = self.prop[5]
        self.dimensions = str(self.x)+'-by-'+str(self.y)+'-by-'+str(self.z)
        if self.prop[5] == 0:
            self.D = ' 2D'
        else:
            self.D = ' 3D'
            self.center = '('+str(self.prop[0])+','+str(self.prop[1])+','+str(self.prop[2])+')'

        return '< '+self.dimensions+self.D+' box with center at '+self.center+' >'

1 个答案:

答案 0 :(得分:0)

问题在于你的setCenter()方法。在其中,您正在更新框的中心,但您没有更新topleft1,topright1,bottomleft1等。

更改setCenter()方法以更新这些属性,它应该可以工作。 (您还必须将此更改添加到其他setX方法。您应该将这些计算抽象到自己的私有方法中,并在每个setX方法的末尾调用它。)。