파이썬 클래스 관련 질문드립니다

조회수 1876회

안녕하세요, 파이썬 시작한지 얼마 되지 않은 초보입니다. 클래스라는 개념을 처음 접하는데, 사각기둥에 관련된 클래스를 만들고 있습니다. 다른건 거의 다 완성을 했는데, def overlaps 와 def isInside 를 완성할줄을 모르겠어서 질문드립니다. Overlaps는 두 사각기둥이 겹치는지 아닌지를 True and False로 표현하는데, 꼭지점만 겹쳐도 겹치는걸로 해당되고, isInside는 사각기둥이 완전히 겹치면 True, 모서리에 겹치는것도 True가 됩니다. 별로 어렵지는 않은 것 같은데 알고리즘을 모르겠어서 질문드립니다. 답변해주시면 감사하겠습니다! 현재 코드는 아래에 첨부했습니다 :)

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)
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 이건 파이썬 이전에 도형기하가 문제인 거 같네요. 주변의 수학/공학 하시는분들한테 물어보세요. 엽토군 2020.4.13 09:34

1 답변

  • overlaps은 겹치는 부분이 있다, isInside는 한 직육면체가 다른 직육면체에 포함되어 있다, 인 것으로 생각하겠습니다.

    저도 긴가민가해서.. 혹시 모르니 참고만 하세요

    overlap의 경우는 두 직육면체의 중심 사이의 거리가 각 직육면체의 중심에서 임의의 꼭짓점까지의 거리의 합 보다 짧으면 될 것 같네요

    실제로 각각을 구해도 되겠습니다만,

    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
    

    이런 식으로 두 직육면체의 height, width, depth 각각의 합(을 반으로 나눈 값)이 x축, y축, z축 기준으로 두 직육면체의 중심 사이의 거리보다 길다면 두 직육면체가 겹치겠죠?

    isInside도 비슷한 방식으로 해결할 수 있을 것 같습니다.

    1. 두 직육면체 중 어떤 쪽이 더 큰지 확인한다.
    2. 위와 같이 세 축에 대해, 두 직육면체의 중심 사이의 거리 + 더 작은 직육면체의 width(또는 height, depth)의 절반 <= 큰 직육면체의 width(또는 height, depth)의 절반 이 모두 참이라면 큰 직육면체 내부에 작은 직육면체가 있다.

    위와 같은 방식으로 해결할 수 있겠네요

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)