From aa4c1e23a5430663c2e46e0901ba188dde6bba75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 24 Oct 2018 17:35:46 +0200 Subject: [PATCH] Added Exercise 4 --- ex_01_04.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 ex_01_04.py diff --git a/ex_01_04.py b/ex_01_04.py new file mode 100644 index 0000000..5fc55f9 --- /dev/null +++ b/ex_01_04.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 + +from util.io import readvalue + +def positive_float(v): + x = float(v) + if(x <= 0): + raise ValueError("{} is a non-positive float".format(v)) + return x + + +class Circle(object): + __slots__ = ["x", "y", "radius"] + + def __init__(self, x, y, radius): + self.x = x + self.y = y + self.radius = radius + + def has_intersection(self, other): + if(not isinstance(other, Circle)): + raise TypeError("cannot calculate intersection of Circle and {}".format(type(other))) + + distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5 + if(distance < self.radius + other.radius): + return True + return False + + def __contains__(self, other): + if(not isinstance(other, Circle)): + raise TypeError("cannot calculate wether {} is in Circle".format(type(other))) + + distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5 + + if(other.radius + distance < self.radius): + return True + return False + + @staticmethod + def read_Circle(): + radius = readvalue("radius> ", positive_float) + x = readvalue("x> ", float) + y = readvalue("y> ", float) + + return Circle(x, y, radius) + + +if( __name__ == "__main__"): + print("Create Circle 1:") + c1 = Circle.read_Circle() + print("Create Circle 2:") + c2 = Circle.read_Circle() + + print("\n") + + if(c1.has_intersection(c2)): + print("The two circles have an intersection.") + else: + print("The two circles have no intersection.") + + if(c2 in c1): + print("Circle 1 contains Circle 2") + if(c1 in c2): + print("Circle 2 contains Circle 1") +