def __getMirrorFocus(self, center, focus): Validator.validatePoint(center, "Center point invalid") Validator.validatePoint(focus, "Focus point invalid") dx = focus.x - center.x dy = focus.y - center.y x = focus.x - dx * 2 y = focus.y - dy * 2 return Point(x, y)
def __constructWithCoords(self, x1, y1, x2, y2): try: self.__point1 = Point(x1, y1) self.__point2 = Point(x2, y2) Validator.validateLine(value=self, errorMessage="Line invalid") return True except ShapeException: return False
def scale(self, factor): Validator.validatePositiveDouble(factor, "Scale Factor not a valid double") for point in self.points: dx = point.x - self.center.x dy = point.y - self.center.y point.x = dx * factor + self.center.x point.y = dy * factor + self.center.y
def testValidateTriangle(self): t1 = Triangle(1, 1, -1, -5, -3, -2) Validator.validateTriangle(t1, "Triangle unexpectedly invalid") self.assertRaises( ShapeException, Validator.validateLine, "(1, 1, -1, -5, -3, -2)", "String \'(1, 1, -1, -5, -3, -2)\' is not a valid triangle") self.assertRaises(ShapeException, Validator.validateLine, Point(1, 1), "Point is not a valid triangle")
def testValidateLine(self): l1 = Line(1, 1, -1, -5) Validator.validateLine(l1, "Line unexpectedly invalid") self.assertRaises(ShapeException, Validator.validateLine, "(1, 1, -1, -5)", "String \'(1, 1, -1, -5)\' is not a valid line") self.assertRaises(ShapeException, Validator.validateLine, Point(1, 1), "Point is not a valid line")
def validatePoint(value, errorMessage): """ Method that validates that value is a valid point :raises: ShapeException: If value is not a valid point """ if not isinstance(value, Point): raise ShapeException(errorMessage) Validator.validateDouble(value.x, "Invalid x-location") Validator.validateDouble(value.y, "Invalid y-location")
def __constructWithPoints(self, point1, point2, point3): try: self.__line1 = Line(point1, point2) self.__line2 = Line(point2, point3) self.__line3 = Line(point3, point1) Validator.validateTriangle(value=self, errorMessage="Triangle invalid") return True except ShapeException: return False
def __constructWithCoords(self, x1, y1, x2, y2, x3, y3): try: self.__line1 = Line(x1, y1, x2, y2) self.__line2 = Line(x2, y2, x3, y3) self.__line3 = Line(x3, y3, x1, y1) Validator.validateTriangle(value=self, errorMessage="Triangle invalid") return True except ShapeException: return False
def testValidateRectangle(self): s1 = Square(1, 1, 3, 1, 3, 3, 1, 3) Validator.validateSquare(s1, "Square unexpectedly invalid") self.assertRaises( ShapeException, Validator.validateSquare, "(1, 1, 3, 1, 3, 3, 1, 3)", "String \'(1, 1, 3, 1, 3, 3, 3, 3)\' is not a valid square") self.assertRaises(ShapeException, Validator.validateSquare, Point(1, 1), "Point is not a valid square")
def testValidateRectangle(self): r1 = Rectangle(1, 1, 4, 1, 4, 3, 1, 3) Validator.validateRectangle(r1, "Rectangle unexpectedly invalid") self.assertRaises( ShapeException, Validator.validateRectangle, "(1, 1, 4, 1, 4, 3, 1, 3)", "String \'(1, 1, 4, 1, 4, 3, 4, 3)\' is not a valid rectangle") self.assertRaises(ShapeException, Validator.validateRectangle, Point(1, 1), "Point is not a valid rectangle")
def testValidateCircle(self): c1 = Circle(0, 0, 2, 0, 0, 2, 3, 0, 0, 3) Validator.validateCircle(c1, "Circle unexpectedly invalid") self.assertRaises( ShapeException, Validator.validateCircle, "(0, 0, 2, 0, 0, 2, 3, 0, 0, 3)", "String \'(0, 0, 2, 0, 0, 2, 3, 0, 0, 3)\' is not a valid circle") self.assertRaises(ShapeException, Validator.validateCircle, Point(1, 1), "Point is not a valid circle")
def testValidateEllipse(self): e1 = Ellipse(0, 0, 2, 0, 0, 1, 3, 0, 0, 2) Validator.validateEllipse(e1, "Ellipse unexpectedly invalid") self.assertRaises( ShapeException, Validator.validateEllipse, "(0, 0, 2, 0, 1, 0, 3, 0, 0, 2)", "String \'(0, 0, 2, 0, 0, 1, 3, 0, 0, 2)\' is not a valid ellipse") self.assertRaises(ShapeException, Validator.validateEllipse, Point(1, 1), "Point is not a valid ellipse")
def __constructWithLines(self, line1, line2, line3): try: self.__line1 = line1 self.__line2 = line2 self.__line3 = line3 Validator.validateTriangle(value=self, errorMessage="Triangle invalid") return True except ShapeException: return False
def __constructWithLines(self, line1, line2, line3, line4): try: self.__line1 = line1 self.__line2 = line2 self.__line3 = line3 self.__line4 = line4 Validator.validateRectangle(value=self, errorMessage="Invalid Rectangle") return True except ShapeException: return False
def validateLine(value, errorMessage): """ Method that validates that a line is valid. :raises: ShapeException: If the line is invalid """ if not isinstance(value, Line): raise ShapeException(errorMessage) Point.validatePoint(value.point1, "Invalid point1") Point.validatePoint(value.point2, "Invalid point2") Validator.validateLineHasLength( value, "A Line must have a length greater than 0")
def validateRectangle(value, errorMessage): if not isinstance(value, Rectangle): raise ShapeException(errorMessage) Line.validateLine(value.line1, "Line 1 is not a valid line.") Line.validateLine(value.line2, "Line 2 is not a valid line.") Line.validateLine(value.line3, "Line 3 is not a valid line.") Line.validateLine(value.line4, "Line 4 is not a valid line.") Validator.validateLinesFormLoop([value.line1, value.line2, value.line3, value.line4], "Lines do not form an enclosed rectangle.") Validator.validateLinesFormRightAngles([value.line1, value.line2, value.line3, value.line4], "Lines do not form 90 degree angles.")
def validateEllipse(value, errorMessage): if not isinstance(value, Ellipse): raise ShapeException(errorMessage) Line.validateLine(value.axis1, "Axis1 is not a valid line") Line.validateLine(value.axis2, "Axis2 is not a valid line") if value.computeArea() <= 0: raise ShapeException(errorMessage) Validator.validateLinesFormRightAngles([value.axis1, value.axis2], "Axis are not perpendicular")
def validateTriangle(value, errorMessage): if not isinstance(value, Triangle): raise ShapeException(errorMessage) Line.validateLine(value.line1, "Line 1 has is not a valid line.") Line.validateLine(value.line2, "Line 2 has is not a valid line.") Line.validateLine(value.line3, "Line 3 has is not a valid line.") Validator.validateSlopesAreDifferent( [value.line1, value.line2, value.line3], "Angles of lines form an invalid triangle") Validator.validateLinesFormLoop( [value.line1, value.line2, value.line3], "Lines do not form an enclosed triangle")
def __constructWithPoints(self, point1, point2, point3, point4, point5): try: self.__center = point1 focus1 = point2 focus2 = point3 mirror1 = self.__getMirrorFocus(self.__center, focus1) mirror2 = self.__getMirrorFocus(self.__center, focus2) self.__foci1 = [focus1, mirror1] self.__foci2 = [focus2, mirror2] self.__axis1 = Line(self.__center.copy(), point4) self.__axis2 = Line(self.__center.copy(), point5) Validator.validateEllipse(value=self, errorMessage="Ellipse is invalid") return True except ShapeException: return False
def __constructWithCoords(self, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5): try: self.__center = Point(x1, y1) focus1 = Point(x2, y2) focus2 = Point(x3, y3) mirror1 = self.__getMirrorFocus(self.__center, focus1) mirror2 = self.__getMirrorFocus(self.__center, focus2) self.__foci1 = [focus1, mirror1] self.__foci2 = [focus2, mirror2] self.__axis1 = Line(self.__center.copy(), Point(x4, y4)) self.__axis2 = Line(self.__center.copy(), Point(x5, y5)) Validator.validateEllipse(value=self, errorMessage="Ellipse is invalid") return True except ShapeException: return False
def testValidatePositiveDouble(self): Validator.validatePositiveDouble(123.456, "Double unexpectedly invalid") Validator.validatePositiveDouble(0, "Double unexpectedly invalid") self.assertRaises(ShapeException, Validator.validatePositiveDouble, None, "None is not a valid positive double") self.assertRaises(ShapeException, Validator.validatePositiveDouble, float('inf'), "Inf is not a valid positive double") self.assertRaises(ShapeException, Validator.validatePositiveDouble, float('-inf'), "-Inf is not a valid positive double") self.assertRaises(ShapeException, Validator.validatePositiveDouble, "foo", "String \'foo\' is not a valid positive double") self.assertRaises(ShapeException, Validator.validatePositiveDouble, -123.456, "-123.456 is not a valid positive double")
def scale(self, scaleFactor): Validator.validatePositiveDouble(scaleFactor, "Scale Factor not a valid double") f1_dx = self.foci1[0].x - self.__center.x f1_dy = self.foci1[0].y - self.__center.y f2_dx = self.foci2[0].x - self.__center.x f2_dy = self.foci2[0].y - self.__center.y a1_dx = self.axis1.point2.x - self.__center.x a1_dy = self.axis1.point2.y - self.__center.y a2_dx = self.axis2.point2.x - self.__center.x a2_dy = self.axis2.point2.y - self.__center.y self.foci1[0].x = f1_dx * scaleFactor + self.__center.x self.foci1[0].y = f1_dy * scaleFactor + self.__center.y self.foci1[1].x = f1_dx * scaleFactor + self.__center.x self.foci1[1].y = f1_dy * scaleFactor + self.__center.y self.foci2[0].x = f2_dx * scaleFactor + self.__center.x self.foci2[0].y = f2_dy * scaleFactor + self.__center.y self.foci2[1].x = f2_dx * scaleFactor + self.__center.x self.foci2[1].y = f2_dy * scaleFactor + self.__center.y self.axis1.point2.x = a1_dx * scaleFactor + self.__center.x self.axis1.point2.y = a1_dy * scaleFactor + self.__center.y self.axis2.point2.x = a2_dx * scaleFactor + self.__center.x self.axis2.point2.y = a2_dy * scaleFactor + self.__center.y
def __constructWithPoints(self, point1, point2): try: Validator.validatePoint(point1, "Invalid point1") Validator.validatePoint(point2, "Invalid point2") self.__point1 = point1 self.__point2 = point2 Validator.validateLine(value=self, errorMessage="Line invalid") return True except ShapeException: return False
def validateSquare(value, errorMessage): Rectangle.validateRectangle(value, errorMessage) Validator.validateLinesAreSameLength( [value.line1, value.line2, value.line3, value.line4], "Lines of square are not same length.")
def testValidatePoint(self): p1 = Point(1, 1) Validator.validatePoint(p1, "Point unexpectedly invalid") self.assertRaises(ShapeException, Validator.validatePoint, "(1, 1)", "String \'(1, 1)\' is not a valid point")
def validateImage(value, errorMessage): if not isinstance(value, Image): raise ShapeException(errorMessage)\ Validator.validateSource(value.source)
def move(self, deltaX, deltaY): Validator.validateDouble(deltaX, "Invalid delta-x value") Validator.validateDouble(deltaY, "Invalid delta-y value") self.__x += deltaX self.__y += deltaY
def __constructWithCoords(self, x1, y1): self.__x = x1 self.__y = y1 Validator.validatePoint(value=self, errorMessage="Point construction failed")
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) Validator.validateCircle(self, "Circle is invalid")