python編程基礎訓練0

一、定義Point類

class Point:
    def __init__(self,x,y):
        self.__x=x
        self.__y=y
    def setPoint(self,x,y):
        self.__x=x
        self.__y=y
    def getPoint(self):
        return self.__x,self.__y

二、定義Rectangle類

class Rectangle:
    def __init__(self,x1,y1,x2,y2):
        self.__x1=x1
        self.__y1=y1
        self.__x2=x2
        self.__y2=y2
    def setRectangle(self,x1,y1,x2,y2):
        self.__x1=x1
        self.__y1=y1
        self.__x2=x2
        self.__y2=y2
    def getRectangle(self):
        return self.__x1,self.__y1,self.__x2,self.__y2

三、定義Circle類

class Circle:
    def __init__(self,point,radius):
        self.__point=point
        self.__radius=radius
    def setCircle(self,point,radius):
        self.__point=point
        self.__radius=radius
    def getCircle(self):
        return self.__point,self.__radius
    def point_in_circle(self,point):
        x,y=point.getPoint()
        cx,cy=self.__point.getPoint()
        if ((x-cx)**2+(y-cy)**2)<=self.__radius**2:
            return True
        else:
            return False
    def rect_circle_overlap(self,rect):
        x1,y1,x2,y2=rect.getRectangle()
        point1=Point(x1,y1)
        point2=Point(x2,y1)
        point3=Point(x1,y2)
        point4=Point(x2,y2)
        if self.point_in_circle(point1) or self.point_in_circle(point2) or self.point_in_circle(point3) or self.point_in_circle(point4):
            return True
        return False
    def rect_in_circle(self,rect):
        x1,y1,x2,y2=rect.getRectangle()
        point1=Point(x1,y1)
        point2=Point(x2,y1)
        point3=Point(x1,y2)
        point4=Point(x2,y2)
        if self.point_in_circle(point1) and self.point_in_circle(point2) and self.point_in_circle(point3) and self.point_in_circle(point4):
            return True
        return False

四、調用測試

circle=Circle(Point(150,100),75)
rect=Rectangle(0,0,150,100)
print(circle.rect_circle_overlap(rect))
print(circle.rect_in_circle(rect))
print(circle.point_in_circle(Point(150,100)))

螞蟻進化史,就瞎寫~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章