python练习:实现判断圆的位置关系--主要练习类的内置函数


import math


class Circle:
    def __init__(self, x=0, y=0, radius=1.0):
        self.__x = x;
        self.__y = y;
        self.__radius = radius

    def getX(self):
        return self.__x

    def setX(self, x):
        self.__x = x

    def getY(self):
        return self.__y

    def set(self, y):
        self.__y = y

    def getRadius(self):
        return self.__radius

    def setRadius(self, radius):
        self.__radius = radius

    def getArea(self):
        return self.__radius * self.__radius * math.pi

    def getPerimeter(self):
        return 2 * self.__radius * math.pi

    def PointDistance(self, x, y):
        DistancePinfang = (self.__x - x) * (self.__x - x) + (self.__y - y) * (self.__y - y)
        return math.sqrt(DistancePinfang)

    # 判断依据:设两个圆的半径为R和r,圆心距为d。 则有以下四种关系:
    # 1.d>R+r 两圆外离; 两圆的圆心距离之和大于两圆的半径之和。
    # 2.d=R+r 两圆外切; 两圆的圆心距离之和等于两圆的半径之和。
    # 3.R+r
    # 4.d=R-r 两圆内切; 两圆的圆心距离之和等于两圆的半径之差。
    # 5.d

    def __relation(self, other):  # other指定是另一个圆,(other[0],other[1])是圆心,other[1]是半径
        pointDistance = self.PointDistance(other[0], other[1])
        if pointDistance > abs(self.__radius + other[2]):
            return 1
        elif pointDistance == abs(self.__radius + other[2]):
            return 2
        elif pointDistance == abs(self.__radius - other[2]):
            return 4
        elif pointDistance < abs(self.__radius - other[2]):
            return 5
        else:
            return 3

    def contain(self, other):
        if self.__relation(other) == 5:
            return True
        else:
            return False

    def containsPoint(self, x, y):
        pointDistance = self.PointDistance(x, y)
        if pointDistance > self.__radius:
            return False
        else:
            return True

    def __getitem__(self, item):
        if item == 0:
            return self.__x
        elif item == 1:
            return self.__y
        else:
            return self.__radius

    def overlaps(self, other):
        if self.__relation(other) == 3:
            return True
        else:
            return False

    def __cmp__(self, other):
        selfArea = self.getArea()
        otherArea = other.getArea()
        if selfArea > otherArea:
            return 1
        elif selfArea == otherArea:
            return 0
        else:
            return -1

    def __lt__(self, other):
        return self.__cmp__(other) < 0

    def __le__(self, other):
        return self.__cmp__(other) <= 0

    def __eq__(self, other):
        return self.__cmp__(other) == 0

    def __ne__(self, other):
        return self.__cmp__(other) != 0

    def __gt__(self, other):
        return self.__cmp__(other) > 0

    def __ge__(self, other):
        return self.__cmp__(other) >= 0


def main():
    x1, y1, r1 = map(eval, input("Enter x1,y1,radius1:").split(","))
    c1 = Circle(x1, y1, r1)
    x2, y2, r2 = map(eval, input("Enter x2,y2,radius2:").split(","))
    c2 = Circle(x2, y2, r2)
    print("Area for c1 is ", c1.getArea())
    print("Perimeter for c1 is ", c1.getPerimeter())
    print("Area for c2 is ", c2.getArea())
    print("Perimeter for c2 is ", c2.getPerimeter())
    print("c1 contains the center of c2?", c1.containsPoint(c2.getX(), c2.getY()))
    print("c1 contains c2?", c1.contain(c2))
    print("c1 overlaps c2?", c1.overlaps(c2))


main()

python练习:实现判断圆的位置关系--主要练习类的内置函数_第1张图片

你可能感兴趣的:(python)