def limitizeLogSpiral(center, n): '''Calculates limit of spiral constants "a" and "k"''' spiral = Spiral() I0 = center for i in range(n): r1 = points[i].distance(I0) r2 = points[i + 1].distance(I0) theta1 = angle(i, Line(I0, points[i]).slope) theta2 = angle(i + 1, Line(I0, points[i + 1]).slope) k = math.log(r1 / r2) / (theta1 - theta2) t = r1 * math.exp(-1 * k * theta1) if abs(t - spiral.t) < epsilon and abs(k - spiral.k) < epsilon: return ("logarithmicSpiral", { "comment": "Converged at expected values", "t": t, "k": k, "iteration": i }) else: return ("logarithmicSpiral", { "comment": "Did not converge at expected values of 't' and 'k'", "t": { "expected": spiral.t, "limitized": t, "error": spiral.t - t, }, "k": { "expected": spiral.k, "limitized": k, "error": spiral.k - k } })
def testValidateLine(self): l1 = ShapeFactory.build("line", Point(1, 1), Point(-1, -5)) Line.validateLine(l1, "Line unexpectedly invalid") self.assertRaises(ShapeException, Line.validateLine, "(1, 1, -1, -5)", "String \'(1, 1, -1, -5)\' is not a valid line") self.assertRaises(ShapeException, Line.validateLine, Point(1, 1), "Point is not a valid line")
def testValidConstruction(self): p1 = Point(1, 2) p2 = Point(1, 1) p3 = Point(2, 1) t1 = ShapeFactory.build("triangle", p1, p2, p3) self.assertEqual(p1, t1.point1) self.assertEqual(p2, t1.point2) self.assertEqual(p3, t1.point3) l1 = Line(Point(4.33, 8), Point(-4, 3.67)) l2 = Line(Point(-4, 3.67), Point(2.19, -5)) l3 = Line(Point(2.19, -5), Point(4.33, 8)) t2 = ShapeFactory.build("triangle", l1.point1, l2.point1, l3.point1) self.assertEqual(l1, t2.line1) self.assertEqual(l2, t2.line2) self.assertEqual(l3, t2.line3) t3 = ShapeFactory.build("triangle", Point(1, 2), Point(5, 1), Point(3, 3)) self.assertEqual(1, t3.point1.x) self.assertEqual(2, t3.point1.y) self.assertEqual(5, t3.point2.x) self.assertEqual(1, t3.point2.y) self.assertEqual(3, t3.point3.x) self.assertEqual(3, t3.point3.y)
def testValidConstruction(self): p1 = Point(1, 3) p2 = Point(5, 3) p3 = Point(5, 1) p4 = Point(1, 1) r1 = Rectangle(p1, p2, p3, p4) self.assertEqual(p1, r1.point1) self.assertEqual(p2, r1.point2) self.assertEqual(p3, r1.point3) self.assertEqual(p4, r1.point4) l1 = Line(-3, 1, 1, 1) l2 = Line(1, 1, 1, -5) l3 = Line(1, -5, -3, -5) l4 = Line(-3, -5, -3, 1) r2 = Rectangle(l1, l2, l3, l4) self.assertEqual(l1, r2.line1) self.assertEqual(l2, r2.line2) self.assertEqual(l3, r2.line3) self.assertEqual(l4, r2.line4) r3 = Rectangle(-.8, .4, 0, 2, .8, 1.6, 0, 0) self.assertEqual(-.8, r3.point1.x) self.assertEqual(.4, r3.point1.y) self.assertEqual(0, r3.point2.x) self.assertEqual(2, r3.point2.y) self.assertEqual(.8, r3.point3.x) self.assertEqual(1.6, r3.point3.y) self.assertEqual(0, r3.point4.x) self.assertEqual(0, r3.point4.y)
def testValidConstruction(self): p1 = Point(1, 2) p2 = Point(1, 1) p3 = Point(2, 1) t1 = Triangle(p1, p2, p3) self.assertEqual(p1, t1.point1) self.assertEqual(p2, t1.point2) self.assertEqual(p3, t1.point3) l1 = Line(4.33, 8, -4, 3.67) l2 = Line(-4, 3.67, 2.19, -5) l3 = Line(2.19, -5, 4.33, 8) t2 = Triangle(l1, l2, l3) self.assertEqual(l1, t2.line1) self.assertEqual(l2, t2.line2) self.assertEqual(l3, t2.line3) t3 = Triangle(1, 2, 5, 1, 3, 3) self.assertEqual(1, t3.point1.x) self.assertEqual(2, t3.point1.y) self.assertEqual(5, t3.point2.x) self.assertEqual(1, t3.point2.y) self.assertEqual(3, t3.point3.x) self.assertEqual(3, t3.point3.y)
def testValidConstruction(self): p1 = Point(1, 3) p2 = Point(5, 3) p3 = Point(5, 1) p4 = Point(1, 1) r1 = ShapeFactory.build("rectangle", p1, p2, p3, p4) self.assertEqual(p1, r1.point1) self.assertEqual(p2, r1.point2) self.assertEqual(p3, r1.point3) self.assertEqual(p4, r1.point4) l1 = Line(Point(-3, 1), Point(1, 1)) l2 = Line(Point(1, 1), Point(1, -5)) l3 = Line(Point(1, -5), Point(-3, -5)) l4 = Line(Point(-3, -5), Point(-3, 1)) r2 = ShapeFactory.build("rectangle", l1.point1, l2.point1, l3.point1, l4.point1) self.assertEqual(l1, r2.line1) self.assertEqual(l2, r2.line2) self.assertEqual(l3, r2.line3) self.assertEqual(l4, r2.line4) r3 = ShapeFactory.build("rectangle", Point(-.8, .4), Point(0, 2), Point(.8, 1.6), Point(0, 0)) self.assertEqual(-.8, r3.point1.x) self.assertEqual(.4, r3.point1.y) self.assertEqual(0, r3.point2.x) self.assertEqual(2, r3.point2.y) self.assertEqual(.8, r3.point3.x) self.assertEqual(1.6, r3.point3.y) self.assertEqual(0, r3.point4.x) self.assertEqual(0, r3.point4.y)
def testInvalidConstruction(self): p1 = Point(1, 2) p2 = Point(1, 1) p3 = Point(1, 3) self.assertRaises(ShapeException, ShapeFactory.build, "triangle", p1, p2, p3) l4 = Line(Point(1, 1), Point(1, 4)) l5 = Line(Point(2, 1), Point(2, 5)) l6 = Line(Point(3, 1), Point(3, 6)) self.assertRaises(ShapeException, ShapeFactory.build, "triangle", l4.point1, l5.point1, l6.point1)
def __init__(self, *args, **kwargs): if len(list(args)) == 4: center = Point.getCenterPoint(args[0], args[2]) super().__init__(center, *args, **kwargs) else: super().__init__(*args, **kwargs) self.lines.append(Line(self.point1, self.point2)) self.lines.append(Line(self.point2, self.point3)) self.lines.append(Line(self.point3, self.point4)) self.lines.append(Line(self.point4, self.point1)) Rectangle.validateRectangle(self, "Rectangle is invalid")
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 testComputeLength(self): l1 = Line(1, 2, 4, 10) self.assertAlmostEqual(8.544, l1.computeLength(), places=3) l2 = Line(1, 2, 1, 3) self.assertAlmostEqual(1, l2.computeLength(), places=3) l3 = Line(3, -2, -4, 10) self.assertAlmostEqual(13.892, l3.computeLength(), places=3)
def testInvalidConstruction(self): p1 = Point(1, 3) p2 = Point(5, 3) p3 = Point(5, 1) p4 = Point(1, -1) self.assertRaises(ShapeException, Rectangle, p1, p2, p3, p4) l1 = Line(-3, 1, 4, 1) l2 = Line(1, 1, 1, -5) l3 = Line(1, -5, -3, -5) l4 = Line(-3, -5, -3, 1) self.assertRaises(ShapeException, Rectangle, l1, l2, l3, l4) self.assertRaises(ShapeException, Rectangle, -1, 0, 0, 2, 1, 1, 0, -1)
def onAddLine(self, button): self.printToLog("onAddLine") name_entry = self.builder.get_object("EntryNameNewLine") x1_entry = self.builder.get_object("EntryX1Line") y1_entry = self.builder.get_object("EntryY1Line") x2_entry = self.builder.get_object("EntryX2Line") y2_entry = self.builder.get_object("EntryY2Line") l1 = Line(name_entry.get_text()) l1.addCoords(float(x1_entry.get_text()), float(y1_entry.get_text())) l1.addCoords(float(x2_entry.get_text()), float(y2_entry.get_text())) self.display_file.addObject(l1) self.add_object_window.hide()
def importFile(self, path): self.display_file.wipeOut() #print("{}".format(path)) vertices = dict() vertice_counter = 0 name = "" self.file = open(path, "r+") # read and write for line in self.file: if (line[0] == "v"): # store vertices in a dictionary vertice_counter += 1 vertices[vertice_counter] = line elif (line[0] == "o" ): # store temporarily the name of the object to come match = re.findall(r"\S+", line) name = match[1] elif (line[0] == "p"): # TODO: FINISH THIS match = re.findall(r"\S+", line) vertice_for_point = vertices[float(match[1])] match = re.findall(r"\S+", vertice_for_point) coord = {"x": float(match[1]), "y": float(match[2])} p1 = Point(name) p1.addCoords(coord["x"], coord["y"]) self.display_file.addObject(p1) elif (line[0] == "l"): match = re.findall(r"\S+", line) if (len(match) == 3): # line l = Line(name) else: # polygon l = None if (match[1] == match[-1]): l = Polygon(name) else: l = Curve(name) for item in match: if ( item != "l" ): # ignore the first character, only compute coordinates vertice_for_point = vertices[float(item)] match_vertice = re.findall(r"\S+", vertice_for_point) coord = { "x": float(match_vertice[1]), "y": float(match_vertice[2]) } l.addCoords(coord["x"], coord["y"]) if (match[1] == match[-1] ): # if polygon (last coords == first coords) l.popCoords() # remove repeated coords self.display_file.addObject(l)
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 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 __constructWithPoints(self, point1, point2, point3, point4): try: self.__line1 = Line(point1, point2) self.__line2 = Line(point2, point3) self.__line3 = Line(point3, point4) self.__line4 = Line(point4, point1) Validator.validateRectangle(value=self, errorMessage="Invalid Rectangle") return True except ShapeException: return False
def __constructWithCoords(self, x1, y1, x2, y2, x3, y3, x4, y4): try: self.__line1 = Line(x1, y1, x2, y2) self.__line2 = Line(x2, y2, x3, y3) self.__line3 = Line(x3, y3, x4, y4) self.__line4 = Line(x4, y4, x1, y1) Validator.validateRectangle(value=self, errorMessage="Invalid Rectangle") return True except ShapeException: return False
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 testValidConstruction(self): p1 = Point(1, 2) p2 = Point(4, 10) l1 = Line(p1, p2) self.assertEqual(p1, l1.point1) self.assertEqual(p2, l1.point2) p3 = Point(1.4, 2.5) p4 = Point(4.6, 10.7) l2 = Line(p3, p4) self.assertEqual(p3, l2.point1) self.assertEqual(p4, l2.point2) l3 = Line(1, 3.33, 4.444, 5.5555) self.assertEqual(1, l3.point1.x) self.assertEqual(3.33, l3.point1.y) self.assertEqual(4.444, l3.point2.x) self.assertEqual(5.5555, l3.point2.y)
def testValidConstruction(self): p1 = Point(1, 3) p2 = Point(5, 3) p3 = Point(5, -1) p4 = Point(1, -1) s1 = ShapeFactory.build("square", p1, p2, p3, p4) self.assertEqual(p1, s1.point1) self.assertEqual(p2, s1.point2) self.assertEqual(p3, s1.point3) self.assertEqual(p4, s1.point4) self.assertTrue(s1.line1.computeLength() == s1.line2.computeLength() == s1.line3.computeLength() == s1.line4.computeLength()) l1 = Line(Point(-3, 1), Point(1, 1)) l2 = Line(Point(1, 1), Point(1, -3)) l3 = Line(Point(1, -3), Point(-3, -3)) l4 = Line(Point(-3, -3), Point(-3, 1)) s2 = ShapeFactory.build("square", l1.point1, l2.point1, l3.point1, l4.point1) self.assertEqual(l1, s2.line1) self.assertEqual(l2, s2.line2) self.assertEqual(l3, s2.line3) self.assertEqual(l4, s2.line4) self.assertTrue(s2.line1.computeLength() == s2.line2.computeLength() == s2.line3.computeLength() == s2.line4.computeLength()) s3 = ShapeFactory.build("square", Point(-.8, .4), Point(1.2, 2.4), Point(3.2, .4), Point(1.2, -1.6)) self.assertEqual(-.8, s3.point1.x) self.assertEqual(.4, s3.point1.y) self.assertEqual(1.2, s3.point2.x) self.assertEqual(2.4, s3.point2.y) self.assertEqual(3.2, s3.point3.x) self.assertEqual(.4, s3.point3.y) self.assertEqual(1.2, s3.point4.x) self.assertEqual(-1.6, s3.point4.y) self.assertTrue(s3.line1.computeLength() == s3.line2.computeLength() == s3.line3.computeLength() == s3.line4.computeLength())
def testValidConstruction(self): p1 = Point(1, 3) p2 = Point(5, 3) p3 = Point(5, -1) p4 = Point(1, -1) s1 = Square(p1, p2, p3, p4) self.assertEqual(p1, s1.point1) self.assertEqual(p2, s1.point2) self.assertEqual(p3, s1.point3) self.assertEqual(p4, s1.point4) self.assertTrue(s1.line1.computeLength() == s1.line2.computeLength() == s1.line3.computeLength() == s1.line4.computeLength()) l1 = Line(-3, 1, 1, 1) l2 = Line(1, 1, 1, -3) l3 = Line(1, -3, -3, -3) l4 = Line(-3, -3, -3, 1) s2 = Square(l1, l2, l3, l4) self.assertEqual(l1, s2.line1) self.assertEqual(l2, s2.line2) self.assertEqual(l3, s2.line3) self.assertEqual(l4, s2.line4) self.assertTrue(s2.line1.computeLength() == s2.line2.computeLength() == s2.line3.computeLength() == s2.line4.computeLength()) s3 = Square(-.8, .4, 1.2, 2.4, 3.2, .4, 1.2, -1.6) self.assertEqual(-.8, s3.point1.x) self.assertEqual(.4, s3.point1.y) self.assertEqual(1.2, s3.point2.x) self.assertEqual(2.4, s3.point2.y) self.assertEqual(3.2, s3.point3.x) self.assertEqual(.4, s3.point3.y) self.assertEqual(1.2, s3.point4.x) self.assertEqual(-1.6, s3.point4.y) self.assertTrue(s3.line1.computeLength() == s3.line2.computeLength() == s3.line3.computeLength() == s3.line4.computeLength())
def testMove(self): l1 = Line(1, 2, 4, 10) l1.move(3, 4) self.assertEqual(4, l1.point1.x) self.assertEqual(6, l1.point1.y) self.assertEqual(7, l1.point2.x) self.assertEqual(14, l1.point2.y) l1.move(.4321, .7654) self.assertEqual(4.4321, l1.point1.x) self.assertEqual(6.7654, l1.point1.y) self.assertEqual(7.4321, l1.point2.x) self.assertEqual(14.7654, l1.point2.y) l1.move(-.4321, -.7654) self.assertEqual(4, l1.point1.x) self.assertEqual(6, l1.point1.y) self.assertEqual(7, l1.point2.x) self.assertEqual(14, l1.point2.y)
def testInvalidConstruction(self): p1 = Point(1, 2) p2 = Point(1, 1) p3 = Point(1, 3) self.assertRaises(ShapeException, Triangle, p1, p2, p3) l1 = Line(4.33, 8, -4, 3.67) l2 = Line(-4, 3.67, 2.19, -5) l3 = Line(2.19, -5, 4.33, 7) self.assertRaises(ShapeException, Triangle, l1, l2, l3) l4 = Line(1, 1, 1, 4) l5 = Line(2, 1, 2, 5) l6 = Line(3, 1, 3, 6) self.assertRaises(ShapeException, Triangle, l4, l5, l6)
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 paint(canvas): text = read_file() for line in text: if "rcl" in line or "va" in line: prop = line.split(", ") Oval(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6], prop[7]) elif "ua" in line or "rect" in line: prop = line.split(", ") Rectangle(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6], prop[7]) elif "tri" in line: prop = line.split(", ") Triangle(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6], prop[7], prop[8], prop[9]) elif "in" in line: prop = line.split(", ") Line(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6]) elif "arc" in line: prop = line.split(", ") Arc(canvas, prop[1], prop[2], prop[3], prop[4], prop[5], prop[6], prop[7], prop[8], prop[9])
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.lines.append(Line(self.center, self.point1)) self.lines.append(Line(self.center, self.point2)) Ellipse.validateEllipse(self, "Ellipse is invalid")
class Triangle: def __init__(self, *args, **kwargs): try: if not self.__constructWithPoints(args[0], args[1], args[2]): if not self.__constructWithLines(args[0], args[1], args[2]): if not self.__constructWithCoords( args[0], args[1], args[2], args[3], args[4], args[5]): raise ShapeException("Triangle construction failed") except IndexError: raise ShapeException("Invalid arguments for triangle construction") 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 __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 __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 @property def point1(self): return self.__line1.point1 @property def point2(self): return self.__line2.point1 @property def point3(self): return self.__line3.point1 @property def line1(self): return self.__line1 @property def line2(self): return self.__line2 @property def line3(self): return self.__line3 def move(self, deltaX, deltaY): self.__line1.move(deltaX, deltaY) self.__line2.move(deltaX, deltaY) self.__line3.move(deltaX, deltaY) def computeArea(self): """Compute area using Heron's formula""" a = self.__line1.computeLength() b = self.__line2.computeLength() c = self.__line3.computeLength() s = (a + b + c) / 2 return math.sqrt(s * (s - a) * (s - b) * (s - c))
def testComputeSlope(self): l1 = Line(2, 2, 4, 10) self.assertAlmostEqual(4, l1.computeSlope(), places=3) l2 = Line(2, 2, 10, 4) self.assertAlmostEqual(.25, l2.computeSlope(), places=3) l3 = Line(2, 2, 4, 2) self.assertAlmostEqual(0, l3.computeSlope(), places=3) l4 = Line(2, 2, 2, 4) self.assertAlmostEqual(float('inf'), l4.computeSlope(), places=3) l5 = Line(2, 4, 2, 2) self.assertAlmostEqual(float('-inf'), l5.computeSlope(), places=3)
def makeTriangles(n): for i in range(1, n): triangles.append( Triangle(triangles[i - 1].line2, Line(points[i - 1], points[i]), lines[i]))