def test_cubic_cubic(self): # q1 = Bezier(10,100, 90,30, 40,140, 220,220) # q2 = Bezier(5,150, 180,20, 80,250, 210,190) # console.log(q1.intersects(q2)) q1 = CubicBezier(Point(10, 100), Point(90, 30), Point(40, 140), Point(220, 220)) q2 = CubicBezier(Point(5, 150), Point(180, 20), Point(80, 250), Point(210, 190)) i = q1.intersections(q2) # self.assertEqual(len(i),3) # self.assertAlmostEqual(i[0].point.x,81.7904225873) # self.assertAlmostEqual(i[0].point.y,109.899396337) # self.assertAlmostEqual(i[1].point.x,133.186831292) # self.assertAlmostEqual(i[1].point.y,167.148173322) # self.assertAlmostEqual(i[2].point.x,179.869157678) # self.assertAlmostEqual(i[2].point.y,199.661989162) import matplotlib.pyplot as plt fig, ax = plt.subplots() path = BezierPath() path.closed = False path.activeRepresentation = SegmentRepresentation(path, [q1]) path.plot(ax) path.activeRepresentation = SegmentRepresentation(path, [q2]) path.plot(ax) for n in i: circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") ax.add_artist(circle)
def fromFontpartsGlyph(klass, glyph): """Returns an *array of BezierPaths* from a FontParts glyph object.""" paths = [] if hasattr(glyph, "contours"): contouriterator = glyph.contours else: contouriterator = glyph for c in contouriterator: path = BezierPath() path.closed = False nodeList = [] if hasattr(c, "points"): pointiterator = c.points else: pointiterator = c for p in pointiterator: if hasattr(p, "segmentType"): t = p.segmentType else: t = p.type nodeList.append(Node(p.x, p.y, t)) path.activeRepresentation = NodelistRepresentation(path, nodeList) if nodeList[0].point == nodeList[-1].point: path.closed = True paths.append(path) return paths
def test_addextremes(self): q = CubicBezier(Point(42, 135), Point(129, 242), Point(167, 77), Point(65, 59)) ex = q.findExtremes() self.assertEqual(len(ex), 2) path = BezierPath() path.closed = False path.activeRepresentation = SegmentRepresentation(path, [q]) path.addExtremes() path.balance() segs = path.asSegments() self.assertEqual(len(segs), 3)
def test_cubic_line(self): q = CubicBezier(Point(100, 240), Point(30, 60), Point(210, 230), Point(160, 30)) l = Line(Point(25, 260), Point(230, 20)) path = BezierPath() path.closed = False path.activeRepresentation = SegmentRepresentation(path, [q]) i = q.intersections(l) self.assertEqual(len(i), 3) self.assertEqual(i[0].point, q.pointAtTime(0.117517031451)) self.assertEqual(i[1].point, q.pointAtTime(0.518591792307)) self.assertEqual(i[2].point, q.pointAtTime(0.867886610031))
def fromFontpartsGlyph(klass, glyph): """Returns an *array of BezierPaths* from a FontParts glyph object.""" paths = [] for c in glyph.contours: path = BezierPath() path.closed = False nodeList = [] for p in c.points: nodeList.append(Node(p.x, p.y, p.type)) path.activeRepresentation = NodelistRepresentation(path, nodeList) if nodeList[0].point == nodeList[-1].point: path.closed = True paths.append(path) return paths
def fromFontpartsGlyph(klass, glyph): """Returns an *array of BezierPaths* from a FontParts glyph object.""" paths = [] for c in glyph.contours: path = BezierPath() path.closed = False nodeList = [] for p in c.points: nodeList.append(Node(p.x,p.y,p.type)) path.activeRepresentation = NodelistRepresentation(path, nodeList) if nodeList[0].point == nodeList[-1].point: path.closed = True paths.append(path) return paths
def not_a_test_offset(self): b = DotMap({ "closed": False, "nodes": [{ "x": 412.0, "y": 500.0, "type": "line" }, { "x": 308.0, "y": 665.0, "type": "offcurve" }, { "x": 163.0, "y": 589.0, "type": "offcurve" }, { "x": 163.0, "y": 504.0, "type": "curve" }, { "x": 163.0, "y": 424.0, "type": "offcurve" }, { "x": 364.0, "y": 321.0, "type": "offcurve" }, { "x": 366.0, "y": 216.0, "type": "curve" }, { "x": 368.0, "y": 94.0, "type": "offcurve" }, { "x": 260.0, "y": 54.0, "type": "offcurve" }, { "x": 124.0, "y": 54.0, "type": "curve" }] }) path = BezierPath() path.activeRepresentation = GSPathRepresentation(path, b) import matplotlib.pyplot as plt fig, ax = plt.subplots() path.addExtremes() path.plot(ax) for n in path.asSegments(): p = n.tunniPoint if p: circle = plt.Circle((p.x, p.y), 1, fill=False, color="blue") ax.add_artist(circle) n.balance() path.translate(Point(5, 5)) path.plot(ax, color="red") # o1 = path.offset(Point(10,10)) # o2 = path.offset(Point(-10,-10)) # o2.reverse() # o1.append(o2) # o1.plot(ax) plt.show()
def test_representations(self): b = DotMap({ "closed": True, "nodes": [{ "x": 385.0, "y": 20.0, "type": "offcurve" }, { "x": 526.0, "y": 79.0, "type": "offcurve" }, { "x": 566.0, "y": 135.0, "type": "curve" }, { "x": 585.0, "y": 162.0, "type": "offcurve" }, { "x": 566.0, "y": 260.0, "type": "offcurve" }, { "x": 484.0, "y": 281.0, "type": "curve" }, { "x": 484.0, "y": 407.0, "type": "offcurve" }, { "x": 381.0, "y": 510.0, "type": "offcurve" }, { "x": 255.0, "y": 510.0, "type": "curve" }, { "x": 26.0, "y": 281.0, "type": "line" }, { "x": 26.0, "y": 155.0, "type": "offcurve" }, { "x": 129.0, "y": 20.0, "type": "offcurve" }, { "x": 255.0, "y": 20.0, "type": "curve" }] }) path = BezierPath() path.activeRepresentation = GSPathRepresentation(path, b) nl = path.asNodelist() self.assertEqual(len(nl), 13) self.assertIsInstance(nl[1], Node) self.assertEqual(nl[1].type, "offcurve") self.assertAlmostEqual(nl[1].x, 526.0) segs = path.asSegments() self.assertEqual(len(segs), 5) self.assertIsInstance(segs[1], CubicBezier) self.assertIsInstance(segs[2], Line)