본문 바로가기
프로그램/Python 1000제

Python 193제] 파이썬 EXPRESS CHAPTER 8. Programming 4 p401

by 건티 2023. 5. 22.
728x90

출처 : 반크_반크 20년 백서

 

참고풀이]

class Rectangle:
    #매개 변수 x, y, w, h를 가지는 생성자 함수
    def __init__(self, x, y, w, h):
        self.x=x
        self.y=y
        self.width=w
        self.height=h

    #사각형의 좌표와 크기를 문자열로 반환하는 함수
    def __str__(self):
        Str="사각형의 좌측상단 좌표(x,y) : (" + str(self.x) + ", " + str(self.y) + ")"
        Str+="사각형의 너비와 높이 : (" + str(self.width) + ", " + str(self.height) + ")"
        return Str
    

    #각 속성의 설정자 함수들
    def setX(self, x):
        self.x=x
    def setY(self, y):
        self.y=y
    def setWidth(self, w):
        self.width=w
    def setHeight(self, h):
        self.height=h

    #각 속성의 접근자 함수들
    def getX(self):
        return self.x
    def getY(self):
        return self.y
    def getWidth(self):
        return self.width
    def getHeight(self):
        return self.height

    #사각형의 면적을 계산하여 반환한다.
    def getArea(self):
        return self.width*self.height
 

   #현재 사각형과 전달된 사각형이 겹치면 Treu, 그렇지 않으면 False를 반환한다.
    def overlap(self, other):
        if other.x>self.x+self.width and other.y>self.y+self.height:
            return True
        else:
            return False

#Main부분
r1=Rectangle(0,0,100,100)
r2=Rectangle(10,10,100,100)
Result=r1.overlap(r2)

if Result:
    print("r1과 r2는 서로 겹치지 않습니다.")
else:
    print("r1과 r2는 서로 겹칩니다.")

 

 

참고풀이 결과]

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 겨울

 

반응형

댓글