From 7b4c5719f0a1516fed02447e7e70844adf4bee4a Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Tue, 11 Jun 2024 10:47:32 +0100 Subject: [PATCH] Black tests --- test/test_arclength.py | 32 ++-- test/test_booleanshapeoperations.py | 28 +-- test/test_bounds.py | 38 ++-- test/test_corners.py | 31 ++-- test/test_cubic.py | 96 +++++----- test/test_curvefit.py | 96 +++++----- test/test_intersections.py | 146 ++++++++-------- test/test_line.py | 14 +- test/test_offset.py | 75 ++++---- test/test_path.py | 260 ++++++++++++++-------------- test/test_quadratic.py | 83 ++++----- test/test_transformation.py | 69 ++++---- 12 files changed, 482 insertions(+), 486 deletions(-) diff --git a/test/test_arclength.py b/test/test_arclength.py index 203a51b..5884a65 100644 --- a/test/test_arclength.py +++ b/test/test_arclength.py @@ -3,27 +3,17 @@ from beziers.cubicbezier import CubicBezier from beziers.point import Point + class ArcLengthMethods(unittest.TestCase): - def test_quadraticLength(self): - b1 = QuadraticBezier( - Point(150,40), - Point(80,30), - Point(105,150) - ) - self.assertAlmostEqual(b1.length, 144.79561403359523) + def test_quadraticLength(self): + b1 = QuadraticBezier(Point(150, 40), Point(80, 30), Point(105, 150)) + self.assertAlmostEqual(b1.length, 144.79561403359523) - b2 = QuadraticBezier( - Point(707,596), - Point(645,596), - Point(592,623) - ) - self.assertAlmostEqual(b2.length, 119.25113694489232) + b2 = QuadraticBezier(Point(707, 596), Point(645, 596), Point(592, 623)) + self.assertAlmostEqual(b2.length, 119.25113694489232) - def test_cubicLength(self): - b1 = CubicBezier( - Point(100,25), - Point(10,90), - Point(110,100), - Point(132,192) - ) - self.assertAlmostEqual(b1.length, 202.20118972656385) + def test_cubicLength(self): + b1 = CubicBezier( + Point(100, 25), Point(10, 90), Point(110, 100), Point(132, 192) + ) + self.assertAlmostEqual(b1.length, 202.20118972656385) diff --git a/test/test_booleanshapeoperations.py b/test/test_booleanshapeoperations.py index 66de974..b228fde 100644 --- a/test/test_booleanshapeoperations.py +++ b/test/test_booleanshapeoperations.py @@ -4,18 +4,20 @@ from beziers.point import Point from beziers.path.geometricshapes import Circle, Square + class BooleanShapeOperations(unittest.TestCase): - def drawIt(self, s,c,paths): - import matplotlib.pyplot as plt - fig, ax = plt.subplots() - s.plot(ax,drawNodes=False) - c.plot(ax) - for p in paths: - p.plot(ax,drawNodes=False,color="red") - plt.show() + def drawIt(self, s, c, paths): + import matplotlib.pyplot as plt + + fig, ax = plt.subplots() + s.plot(ax, drawNodes=False) + c.plot(ax) + for p in paths: + p.plot(ax, drawNodes=False, color="red") + plt.show() - def not_a_test_square_circle_union(self): - subject = Square(10, origin=Point(5,5)) - clip = Circle(10, origin=Point(15,15)) - paths = subject.union(clip) - self.drawIt(subject,clip,paths) \ No newline at end of file + def not_a_test_square_circle_union(self): + subject = Square(10, origin=Point(5, 5)) + clip = Circle(10, origin=Point(15, 15)) + paths = subject.union(clip) + self.drawIt(subject, clip, paths) diff --git a/test/test_bounds.py b/test/test_bounds.py index b64cee6..651e3a0 100644 --- a/test/test_bounds.py +++ b/test/test_bounds.py @@ -3,24 +3,24 @@ from beziers.point import Point from beziers.boundingbox import BoundingBox + class BBoxMethods(unittest.TestCase): - def test_overlap(self): - b1 = BoundingBox() - b2 = BoundingBox() - b1.extend(Point(5,5)) - b1.extend(Point(10,10)) - b2.extend(Point(7,7)) - b2.extend(Point(14,14)) - print("%s v %s" % (b1,b2)) - self.assertTrue(b1.overlaps(b2)) - self.assertTrue(b2.overlaps(b1)) + def test_overlap(self): + b1 = BoundingBox() + b2 = BoundingBox() + b1.extend(Point(5, 5)) + b1.extend(Point(10, 10)) + b2.extend(Point(7, 7)) + b2.extend(Point(14, 14)) + print("%s v %s" % (b1, b2)) + self.assertTrue(b1.overlaps(b2)) + self.assertTrue(b2.overlaps(b1)) - def test_quadratic_bounds(self): - # console.log((new Bezier(150,40,80,30,105,150)).bbox()) - q = QuadraticBezier( - Point(150,40), Point(80,30), Point(105,150)) - b = q.bounds() - self.assertAlmostEqual(b.bl.x,98.42105263157895) - self.assertAlmostEqual(b.tr.x,150) - self.assertAlmostEqual(b.bl.y,39.23076923076923) - self.assertAlmostEqual(b.tr.y,150) + def test_quadratic_bounds(self): + # console.log((new Bezier(150,40,80,30,105,150)).bbox()) + q = QuadraticBezier(Point(150, 40), Point(80, 30), Point(105, 150)) + b = q.bounds() + self.assertAlmostEqual(b.bl.x, 98.42105263157895) + self.assertAlmostEqual(b.tr.x, 150) + self.assertAlmostEqual(b.bl.y, 39.23076923076923) + self.assertAlmostEqual(b.tr.y, 150) diff --git a/test/test_corners.py b/test/test_corners.py index 90f9f6d..20277d8 100644 --- a/test/test_corners.py +++ b/test/test_corners.py @@ -4,19 +4,20 @@ from beziers.point import Point from beziers.path.representations.Nodelist import NodelistRepresentation, Node + class CornerMethods(unittest.TestCase): - def test_corners(self): - nl = [ - Node(302.0,492.0,"line"), - Node(176.0,432.0,"line"), - Node(-51.0,325.0,"offcurve"), - Node(-74.0,484.0,"offcurve"), - Node(73.0,570.0,"curve"), - Node(85.0,764.0,"offcurve"), - Node(290.0,748.0,"offcurve"), - Node(418.0,688.0,"curve"), - ] - path = BezierPath.fromNodelist(nl) - path.closed = False - for seg1, seg2 in path.segpairs(): - print(seg1.endAngle * 57.2958, seg2.startAngle * 57.2958) + def test_corners(self): + nl = [ + Node(302.0, 492.0, "line"), + Node(176.0, 432.0, "line"), + Node(-51.0, 325.0, "offcurve"), + Node(-74.0, 484.0, "offcurve"), + Node(73.0, 570.0, "curve"), + Node(85.0, 764.0, "offcurve"), + Node(290.0, 748.0, "offcurve"), + Node(418.0, 688.0, "curve"), + ] + path = BezierPath.fromNodelist(nl) + path.closed = False + for seg1, seg2 in path.segpairs(): + print(seg1.endAngle * 57.2958, seg2.startAngle * 57.2958) diff --git a/test/test_cubic.py b/test/test_cubic.py index 95801ff..c6c6787 100644 --- a/test/test_cubic.py +++ b/test/test_cubic.py @@ -3,58 +3,56 @@ from beziers.point import Point from beziers.path import BezierPath -class CubicMethods(unittest.TestCase): - def test_extremes(self): - q = CubicBezier( - Point(65,59), Point(194,90), Point(220,260), Point(70,261) - ) - # console.log(Bezier(65,59, 194,90, 220,260, 70,261).extrema()) - r = q.findExtremes() - self.assertEqual(len(r), 1) - self.assertAlmostEqual(r[0], 0.5275787707261016) - r = q.findExtremes(inflections = True) - self.assertEqual(len(r), 2) - self.assertAlmostEqual(r[0], 0.4512987012987013) - self.assertAlmostEqual(r[1], 0.5275787707261016) +class CubicMethods(unittest.TestCase): + def test_extremes(self): + q = CubicBezier(Point(65, 59), Point(194, 90), Point(220, 260), Point(70, 261)) + # console.log(Bezier(65,59, 194,90, 220,260, 70,261).extrema()) + r = q.findExtremes() + self.assertEqual(len(r), 1) + self.assertAlmostEqual(r[0], 0.5275787707261016) + r = q.findExtremes(inflections=True) + self.assertEqual(len(r), 2) + self.assertAlmostEqual(r[0], 0.4512987012987013) + self.assertAlmostEqual(r[1], 0.5275787707261016) - def test_length(self): - q = CubicBezier( - Point(120,160), Point(35,200), Point(220,260), Point(220,40) - ) - self.assertAlmostEqual(q.length,272.87003168) + def test_length(self): + q = CubicBezier( + Point(120, 160), Point(35, 200), Point(220, 260), Point(220, 40) + ) + self.assertAlmostEqual(q.length, 272.87003168) - def test_align(self): - q = CubicBezier( - Point(120,160), Point(35,200), Point(220,260), Point(220,40) - ) - s = q.aligned() - self.assertAlmostEqual(s[0].x,0.0) - self.assertAlmostEqual(s[0].y,0.0) - self.assertAlmostEqual(s[1].x,-85.14452515537582) - self.assertAlmostEqual(s[1].y,-39.69143277919774) - self.assertAlmostEqual(s[2].x,-12.803687993289572) - self.assertAlmostEqual(s[2].y,140.84056792618557) - self.assertAlmostEqual(s[3].x,156.2049935181331) - self.assertAlmostEqual(s[3].y,0.0) + def test_align(self): + q = CubicBezier( + Point(120, 160), Point(35, 200), Point(220, 260), Point(220, 40) + ) + s = q.aligned() + self.assertAlmostEqual(s[0].x, 0.0) + self.assertAlmostEqual(s[0].y, 0.0) + self.assertAlmostEqual(s[1].x, -85.14452515537582) + self.assertAlmostEqual(s[1].y, -39.69143277919774) + self.assertAlmostEqual(s[2].x, -12.803687993289572) + self.assertAlmostEqual(s[2].y, 140.84056792618557) + self.assertAlmostEqual(s[3].x, 156.2049935181331) + self.assertAlmostEqual(s[3].y, 0.0) - def test_curvature(self): - q = CubicBezier( - Point(122,102), Point(35,200), Point(228,145), Point(190,46) - ) - self.assertAlmostEqual(q.curvatureAtTime(0.5),-103450.5) + def test_curvature(self): + q = CubicBezier( + Point(122, 102), Point(35, 200), Point(228, 145), Point(190, 46) + ) + self.assertAlmostEqual(q.curvatureAtTime(0.5), -103450.5) - def test_loop(self): - q = CubicBezier( - Point(171,272), Point(388,249), Point(167,444), Point(388,176) - ) - self.assertTrue(not q.hasLoop) + def test_loop(self): + q = CubicBezier( + Point(171, 272), Point(388, 249), Point(167, 444), Point(388, 176) + ) + self.assertTrue(not q.hasLoop) - q = CubicBezier( - Point(171,272), Point(595,249), Point(167,444), Point(388,176) - ) - roots = q.hasLoop - p1 = q.pointAtTime(roots[0]) - p2 = q.pointAtTime(roots[1]) - self.assertTrue(q.hasLoop) - self.assertEqual(p1,p2) \ No newline at end of file + q = CubicBezier( + Point(171, 272), Point(595, 249), Point(167, 444), Point(388, 176) + ) + roots = q.hasLoop + p1 = q.pointAtTime(roots[0]) + p2 = q.pointAtTime(roots[1]) + self.assertTrue(q.hasLoop) + self.assertEqual(p1, p2) diff --git a/test/test_curvefit.py b/test/test_curvefit.py index c4b0357..3d85189 100644 --- a/test/test_curvefit.py +++ b/test/test_curvefit.py @@ -3,54 +3,54 @@ from beziers.path import BezierPath from beziers.point import Point + class CurveFitterMethods(unittest.TestCase): - def test_cf1(self): - nodes = [ - Point(122,102), Point(35,200), Point(228,145), Point(190,46) - ] - t = CurveFit._leftTangent(nodes) - self.assertAlmostEqual(t.x, -0.663890062102) - self.assertAlmostEqual(t.y,0.747830184896) - t = CurveFit._rightTangent(nodes) - self.assertAlmostEqual(t.x,-0.3583470773350791) - self.assertAlmostEqual(t.y, -0.9335884383203376) + def test_cf1(self): + nodes = [Point(122, 102), Point(35, 200), Point(228, 145), Point(190, 46)] + t = CurveFit._leftTangent(nodes) + self.assertAlmostEqual(t.x, -0.663890062102) + self.assertAlmostEqual(t.y, 0.747830184896) + t = CurveFit._rightTangent(nodes) + self.assertAlmostEqual(t.x, -0.3583470773350791) + self.assertAlmostEqual(t.y, -0.9335884383203376) + + def test_cf2(self): + nodes = [ + Point(100, 50), + Point(50, 150), + Point(100, 220), + Point(200, 200), + Point(250, 80), + Point(220, 50), + ] + path = BezierPath.fromPoints(nodes) + segs = path.asSegments() + self.assertEqual(len(segs), 2) + self.assertEqual(segs[0].start, Point(100.0, 50.0)) + self.assertAlmostEqual(segs[0][1].x, 83.333333333) + self.assertAlmostEqual(segs[0][1].y, 83.333333333) + self.assertEqual(segs[0].end, Point(50.0, 150.0)) + self.assertAlmostEqual(segs[1][1].x, 50) + self.assertEqual(segs[1].end, Point(220.0, 50.0)) - def test_cf2(self): - nodes = [ - Point(100,50), - Point(50,150), - Point(100,220), - Point(200,200), - Point(250,80), - Point(220,50) - ] - path = BezierPath.fromPoints(nodes) - segs = path.asSegments() - self.assertEqual(len(segs),2) - self.assertEqual(segs[0].start, Point(100.0, 50.0)) - self.assertAlmostEqual(segs[0][1].x, 83.333333333) - self.assertAlmostEqual(segs[0][1].y, 83.333333333) - self.assertEqual(segs[0].end, Point(50.0, 150.0)) - self.assertAlmostEqual(segs[1][1].x, 50) - self.assertEqual(segs[1].end, Point(220.0, 50.0)) + def not_a_test_cf3(self): + import matplotlib.pyplot as plt + import math - def not_a_test_cf3(self): - import matplotlib.pyplot as plt - import math - fig, ax = plt.subplots() - points = [ - Point(100,50), - Point(50,150), - Point(150,250), - Point(200,220), - Point(250,80), - Point(220,50) - ] - path = BezierPath.fromPoints(points) - centroid = path.bounds().centroid - path.rotate(centroid, math.pi/2) - path.balance() - path.plot(ax) - path.offset(Point(5,5)).plot(ax, color="red") - path.offset(Point(-5,-5)).plot(ax, color="green") - plt.show() \ No newline at end of file + fig, ax = plt.subplots() + points = [ + Point(100, 50), + Point(50, 150), + Point(150, 250), + Point(200, 220), + Point(250, 80), + Point(220, 50), + ] + path = BezierPath.fromPoints(points) + centroid = path.bounds().centroid + path.rotate(centroid, math.pi / 2) + path.balance() + path.plot(ax) + path.offset(Point(5, 5)).plot(ax, color="red") + path.offset(Point(-5, -5)).plot(ax, color="green") + plt.show() diff --git a/test/test_intersections.py b/test/test_intersections.py index cc03973..df99cdd 100644 --- a/test/test_intersections.py +++ b/test/test_intersections.py @@ -5,88 +5,92 @@ from beziers.path import BezierPath from beziers.path.representations.Segment import SegmentRepresentation + class IntersectionMethods(unittest.TestCase): - def test_line_line(self): - l1 = Line(Point(310,389), Point(453,222)) - l2 = Line(Point(289,251), Point(447,367)) - # Sanity check - self.assertEqual(len(l1.intersections(l2)),1) + def test_line_line(self): + l1 = Line(Point(310, 389), Point(453, 222)) + l2 = Line(Point(289, 251), Point(447, 367)) + # Sanity check + self.assertEqual(len(l1.intersections(l2)), 1) - 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)) - # print q.intersections(l) - # import matplotlib.pyplot as plt - # fig, ax = plt.subplots() - # path.plot(ax) + 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)) + # print q.intersections(l) + # import matplotlib.pyplot as plt + # fig, ax = plt.subplots() + # path.plot(ax) - # path = BezierPath() - # path.closed = False - # path.activeRepresentation = SegmentRepresentation(path,[l]) - # path.plot(ax) + # path = BezierPath() + # path.closed = False + # path.activeRepresentation = SegmentRepresentation(path,[l]) + # path.plot(ax) - # for n in q.intersections(l): - # circle = plt.Circle((n.point.x, n.point.y), 1, fill=False) - # ax.add_artist(circle) + # for n in q.intersections(l): + # circle = plt.Circle((n.point.x, n.point.y), 1, fill=False) + # ax.add_artist(circle) - # plt.show() + # plt.show() - 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() + 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 - path = BezierPath() - path.closed = False - path.activeRepresentation = SegmentRepresentation(path,[q1]) - path.plot(ax) - path.activeRepresentation = SegmentRepresentation(path,[q2]) - path.plot(ax) + fig, ax = plt.subplots() - for n in i: - circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") - ax.add_artist(circle) + path = BezierPath() + path.closed = False + path.activeRepresentation = SegmentRepresentation(path, [q1]) + path.plot(ax) + path.activeRepresentation = SegmentRepresentation(path, [q2]) + path.plot(ax) - #plt.show() + for n in i: + circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") + ax.add_artist(circle) - def test_cubic_line_2(self): - s1 = CubicBezier.fromRepr("B<<584.0,126.03783241124995>-<402.0,163.0378324112499>-<220.00000000000003,200.03783241124995>-<38.0,237.03783241124995>>"), - ray = Line.fromRepr("L<<357.4,-9.99999999999999>--<357.6,250.2692949284206>>") - assert (s1[0].intersections(ray)) + # plt.show() - def test_cubic_line_3(self): - seg = CubicBezier.fromRepr("B<<320.0,454.0>-<277.0,454.0>-<230.0,439.0>-<189.0,417.0>>") - ray = Line.fromRepr("L<<254.5,221.5>--<254.5000000000001,887.6681469418963>>") - assert seg.intersections(ray) + def test_cubic_line_2(self): + s1 = ( + CubicBezier.fromRepr( + "B<<584.0,126.03783241124995>-<402.0,163.0378324112499>-<220.00000000000003,200.03783241124995>-<38.0,237.03783241124995>>" + ), + ) + ray = Line.fromRepr("L<<357.4,-9.99999999999999>--<357.6,250.2692949284206>>") + assert s1[0].intersections(ray) + def test_cubic_line_3(self): + seg = CubicBezier.fromRepr( + "B<<320.0,454.0>-<277.0,454.0>-<230.0,439.0>-<189.0,417.0>>" + ) + ray = Line.fromRepr("L<<254.5,221.5>--<254.5000000000001,887.6681469418963>>") + assert seg.intersections(ray) - def test_cubic_line_4(self): - seg = CubicBezier.fromRepr("B<<315.0,296.0>-<404.0,296.0>-<468.0,251.0>-<468.0,183.0>>") - ray = Line.fromRepr("L<<330.0000432054082,365.6789020602332>--<330,286>>") - assert seg.intersections(ray) + def test_cubic_line_4(self): + seg = CubicBezier.fromRepr( + "B<<315.0,296.0>-<404.0,296.0>-<468.0,251.0>-<468.0,183.0>>" + ) + ray = Line.fromRepr("L<<330.0000432054082,365.6789020602332>--<330,286>>") + assert seg.intersections(ray) diff --git a/test/test_line.py b/test/test_line.py index 56d73fe..819826f 100644 --- a/test/test_line.py +++ b/test/test_line.py @@ -2,12 +2,12 @@ from beziers.line import Line from beziers.point import Point -class LineMethods(unittest.TestCase): - def test_slope(self): - l = Line(Point(0,10),Point(20,20.4)) - self.assertAlmostEqual(l.slope, 0.52) - def test_intercept(self): - l = Line(Point(0,10),Point(20,20.4)) - self.assertAlmostEqual(l.intercept, 10) +class LineMethods(unittest.TestCase): + def test_slope(self): + l = Line(Point(0, 10), Point(20, 20.4)) + self.assertAlmostEqual(l.slope, 0.52) + def test_intercept(self): + l = Line(Point(0, 10), Point(20, 20.4)) + self.assertAlmostEqual(l.intercept, 10) diff --git a/test/test_offset.py b/test/test_offset.py index 67aad17..9b4f607 100644 --- a/test/test_offset.py +++ b/test/test_offset.py @@ -1,4 +1,3 @@ - import unittest from beziers.path.representations.GSPath import GSPathRepresentation from beziers.path.representations.Segment import SegmentRepresentation @@ -9,38 +8,44 @@ from beziers.path import BezierPath from dotmap import DotMap + class PathTests(unittest.TestCase): - 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() \ No newline at end of file + 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() diff --git a/test/test_path.py b/test/test_path.py index 5e77b3c..a31bc9d 100644 --- a/test/test_path.py +++ b/test/test_path.py @@ -11,140 +11,144 @@ class PathTests(unittest.TestCase): - # 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) + # 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) + # segs = path.asSegments() + # self.assertEqual(len(segs), 5) + # self.assertIsInstance(segs[1], CubicBezier) + # self.assertIsInstance(segs[2], Line) - 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) - # import matplotlib.pyplot as plt - # fig, ax = plt.subplots() - # path.plot(ax) - # plt.show() + 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) + # import matplotlib.pyplot as plt + # fig, ax = plt.subplots() + # path.plot(ax) + # plt.show() - def test_overlap(self): - nodes = [ Node(698.0,413.0,"offcurve"), - Node(401.0,179.0,"offcurve"), - Node(401.0,274.0,"curve"), - Node(401.0,368.0,"offcurve"), - Node(315.0,445.0,"offcurve"), - Node(210.0,445.0,"curve"), - Node(104.0,445.0,"offcurve"), - Node(18.0,368.0,"offcurve"), - Node(18.0,274.0,"curve"), - Node(18.0,179.0,"offcurve"), - Node(439.0,400.0,"offcurve"), - Node(533.0,405.0,"curve") - ] - p = BezierPath.fromNodelist(nodes) - p.closed = True - i = p.getSelfIntersections() - self.assertEqual(len(i),1) - self.assertAlmostEqual(i[0].point.x, 377.71521068) + def test_overlap(self): + nodes = [ + Node(698.0, 413.0, "offcurve"), + Node(401.0, 179.0, "offcurve"), + Node(401.0, 274.0, "curve"), + Node(401.0, 368.0, "offcurve"), + Node(315.0, 445.0, "offcurve"), + Node(210.0, 445.0, "curve"), + Node(104.0, 445.0, "offcurve"), + Node(18.0, 368.0, "offcurve"), + Node(18.0, 274.0, "curve"), + Node(18.0, 179.0, "offcurve"), + Node(439.0, 400.0, "offcurve"), + Node(533.0, 405.0, "curve"), + ] + p = BezierPath.fromNodelist(nodes) + p.closed = True + i = p.getSelfIntersections() + self.assertEqual(len(i), 1) + self.assertAlmostEqual(i[0].point.x, 377.71521068) - # import matplotlib.pyplot as plt - # fig, ax = plt.subplots() - # p.plot(ax) - # for n in i: - # circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") - # ax.add_artist(circle) - # plt.show() + # import matplotlib.pyplot as plt + # fig, ax = plt.subplots() + # p.plot(ax) + # for n in i: + # circle = plt.Circle((n.point.x, n.point.y), 2, fill=True, color="red") + # ax.add_artist(circle) + # plt.show() - p = BezierPath.fromNodelist([ - Node(310.0,389.0,"line"), - Node(453.0,222.0,"line"), - Node(289.0,251.0,"line"), - Node(447.0,367.0,"line"), - Node(578.0,222.0,"line"), - Node(210.0,-8.0,"line"), - ]) + p = BezierPath.fromNodelist( + [ + Node(310.0, 389.0, "line"), + Node(453.0, 222.0, "line"), + Node(289.0, 251.0, "line"), + Node(447.0, 367.0, "line"), + Node(578.0, 222.0, "line"), + Node(210.0, -8.0, "line"), + ] + ) - i = p.getSelfIntersections() - self.assertEqual(len(i),1) - self.assertEqual(i[0].point,Point(374.448829525,313.734583702)) + i = p.getSelfIntersections() + self.assertEqual(len(i), 1) + self.assertEqual(i[0].point, Point(374.448829525, 313.734583702)) - def test_splitatpoints(self): - p = BezierPath.fromNodelist([ - Node(297.0,86.0,"offcurve"), - Node(344.0,138.0,"offcurve"), - Node(344.0,203.0,"curve"), - Node(344.0,267.0,"offcurve"), - Node(297.0,319.0,"offcurve"), - Node(240.0,319.0,"curve"), - Node(183.0,319.0,"offcurve"), - Node(136.0,267.0,"offcurve"), - Node(136.0,203.0,"curve"), - Node(136.0,138.0,"offcurve"), - Node(183.0,86.0,"offcurve"), - Node(240.0,86.0,"curve"), - ]) - splitlist = [] - for seg in p.asSegments(): - for t in seg.regularSampleTValue(5): - splitlist.append((seg,t)) - p.splitAtPoints(splitlist) - self.assertEqual(len(p.asSegments()),24) + def test_splitatpoints(self): + p = BezierPath.fromNodelist( + [ + Node(297.0, 86.0, "offcurve"), + Node(344.0, 138.0, "offcurve"), + Node(344.0, 203.0, "curve"), + Node(344.0, 267.0, "offcurve"), + Node(297.0, 319.0, "offcurve"), + Node(240.0, 319.0, "curve"), + Node(183.0, 319.0, "offcurve"), + Node(136.0, 267.0, "offcurve"), + Node(136.0, 203.0, "curve"), + Node(136.0, 138.0, "offcurve"), + Node(183.0, 86.0, "offcurve"), + Node(240.0, 86.0, "curve"), + ] + ) + splitlist = [] + for seg in p.asSegments(): + for t in seg.regularSampleTValue(5): + splitlist.append((seg, t)) + p.splitAtPoints(splitlist) + self.assertEqual(len(p.asSegments()), 24) - def test_inside(self): - p = BezierPath.fromNodelist([ - Node(329,320,"line"), - Node(564,190,"line"), - Node(622,332,"offcurve"), - Node(495,471,"offcurve"), - Node(329,471,"curve"), - Node(164,471,"offcurve"), - Node(34,334,"offcurve"), - Node(93,190,"curve") - ]) - self.assertTrue(p.pointIsInside(Point(326,423))) - self.assertFalse(p.pointIsInside(Point(326,123))) - self.assertFalse(p.pointIsInside(Point(326,251))) - self.assertTrue(p.pointIsInside(Point(526,251))) - self.assertTrue(p.pointIsInside(Point(126,251))) - - def test_area(self): - p = Rectangle(200, 100) - self.assertEqual(p.area, 200 * 100) - self.assertEqual(p.signed_area, -200 * 100) - self.assertEqual(p.direction, -1) - p.reverse() - self.assertEqual(p.signed_area, 200 * 100) - self.assertEqual(p.direction, 1) + def test_inside(self): + p = BezierPath.fromNodelist( + [ + Node(329, 320, "line"), + Node(564, 190, "line"), + Node(622, 332, "offcurve"), + Node(495, 471, "offcurve"), + Node(329, 471, "curve"), + Node(164, 471, "offcurve"), + Node(34, 334, "offcurve"), + Node(93, 190, "curve"), + ] + ) + self.assertTrue(p.pointIsInside(Point(326, 423))) + self.assertFalse(p.pointIsInside(Point(326, 123))) + self.assertFalse(p.pointIsInside(Point(326, 251))) + self.assertTrue(p.pointIsInside(Point(526, 251))) + self.assertTrue(p.pointIsInside(Point(126, 251))) + def test_area(self): + p = Rectangle(200, 100) + self.assertEqual(p.area, 200 * 100) + self.assertEqual(p.signed_area, -200 * 100) + self.assertEqual(p.direction, -1) + p.reverse() + self.assertEqual(p.signed_area, 200 * 100) + self.assertEqual(p.direction, 1) diff --git a/test/test_quadratic.py b/test/test_quadratic.py index adaf41d..7884888 100644 --- a/test/test_quadratic.py +++ b/test/test_quadratic.py @@ -2,54 +2,45 @@ from beziers.quadraticbezier import QuadraticBezier from beziers.point import Point -class QuadraticMethods(unittest.TestCase): - def test_split(self): - # console.log((new Bezier(150,40,80,30,105,150)).split(0.2)) - q = QuadraticBezier( - Point(150,40), Point(80,30), Point(105,150)) - left, right = q.splitAtTime(0.2) - self.assertEqual(left[1],Point(136,38)) - self.assertEqual(right[1],Point(85,54)) - - def test_extremes(self): - q = QuadraticBezier( - Point(70,250), Point(13,187), Point(209,58)) - r = q.findExtremes() - self.assertEqual(len(r), 1) - self.assertAlmostEqual(r[0], 0.22529644268774704) - def test_extremes2(self): - # console.log((new Bezier(127,242,71,150,210,60)).extrema()) - q = QuadraticBezier( - Point(127,242), Point(71,150), Point(210,60)) - r = q.findExtremes() - self.assertEqual(len(r), 1) - self.assertAlmostEqual(r[0], 0.28717948717948716) +class QuadraticMethods(unittest.TestCase): + def test_split(self): + # console.log((new Bezier(150,40,80,30,105,150)).split(0.2)) + q = QuadraticBezier(Point(150, 40), Point(80, 30), Point(105, 150)) + left, right = q.splitAtTime(0.2) + self.assertEqual(left[1], Point(136, 38)) + self.assertEqual(right[1], Point(85, 54)) - def test_extremes3(self): - # console.log((new Bezier(127,242,27,5,210,60)).extrema()) - q = QuadraticBezier( - Point(127,242), Point(27,5), Point(210,60)) - r = q.findExtremes() - self.assertEqual(len(r), 2) - self.assertAlmostEqual(r[0], 0.35335689045936397) - self.assertAlmostEqual(r[1], 0.8116438356164384) - # from beziers.path import BezierPath - # import matplotlib.pyplot as plt - # path = BezierPath.fromSegments([q]) - # fig, ax = plt.subplots() - # path.closed = False - # path.addExtremes() - # path.plot(ax) - # plt.show() + def test_extremes(self): + q = QuadraticBezier(Point(70, 250), Point(13, 187), Point(209, 58)) + r = q.findExtremes() + self.assertEqual(len(r), 1) + self.assertAlmostEqual(r[0], 0.22529644268774704) + def test_extremes2(self): + # console.log((new Bezier(127,242,71,150,210,60)).extrema()) + q = QuadraticBezier(Point(127, 242), Point(71, 150), Point(210, 60)) + r = q.findExtremes() + self.assertEqual(len(r), 1) + self.assertAlmostEqual(r[0], 0.28717948717948716) - def test_extremes4(self): - q = QuadraticBezier( - Point(664,1075), - Point(732,1167), - Point(800,1239) - ) - r = q.findExtremes() - self.assertEqual(len(r), 0) + def test_extremes3(self): + # console.log((new Bezier(127,242,27,5,210,60)).extrema()) + q = QuadraticBezier(Point(127, 242), Point(27, 5), Point(210, 60)) + r = q.findExtremes() + self.assertEqual(len(r), 2) + self.assertAlmostEqual(r[0], 0.35335689045936397) + self.assertAlmostEqual(r[1], 0.8116438356164384) + # from beziers.path import BezierPath + # import matplotlib.pyplot as plt + # path = BezierPath.fromSegments([q]) + # fig, ax = plt.subplots() + # path.closed = False + # path.addExtremes() + # path.plot(ax) + # plt.show() + def test_extremes4(self): + q = QuadraticBezier(Point(664, 1075), Point(732, 1167), Point(800, 1239)) + r = q.findExtremes() + self.assertEqual(len(r), 0) diff --git a/test/test_transformation.py b/test/test_transformation.py index 9d1f274..3863fa1 100644 --- a/test/test_transformation.py +++ b/test/test_transformation.py @@ -4,43 +4,44 @@ from beziers.affinetransformation import AffineTransformation import math + class AffineTransformationMethods(unittest.TestCase): - def test_translate(self): - p = Point(30,70) - p.rotate(Point(0,0), math.pi/4) - self.assertEqual(p.x, -28.284271247461906) - self.assertEqual(p.y, 70.71067811865476) + def test_translate(self): + p = Point(30, 70) + p.rotate(Point(0, 0), math.pi / 4) + self.assertEqual(p.x, -28.284271247461906) + self.assertEqual(p.y, 70.71067811865476) - p = Point(30,70) - m = AffineTransformation.rotation(math.pi/4) - p.transform(m) - self.assertAlmostEqual(p.x, -28.284271247461906) - self.assertAlmostEqual(p.y, 70.71067811865476) + p = Point(30, 70) + m = AffineTransformation.rotation(math.pi / 4) + p.transform(m) + self.assertAlmostEqual(p.x, -28.284271247461906) + self.assertAlmostEqual(p.y, 70.71067811865476) - p = Point(0,10) - m = AffineTransformation.translation(Point(5,-2)) - p.transform(m) - self.assertEqual(p.x, 5) - self.assertEqual(p.y, 8) + p = Point(0, 10) + m = AffineTransformation.translation(Point(5, -2)) + p.transform(m) + self.assertEqual(p.x, 5) + self.assertEqual(p.y, 8) - def test_scale(self): - p = Point(4,5) - m = AffineTransformation.scaling(2) - p.transform(m) - self.assertEqual(p.x, 8) - self.assertEqual(p.y, 10) + def test_scale(self): + p = Point(4, 5) + m = AffineTransformation.scaling(2) + p.transform(m) + self.assertEqual(p.x, 8) + self.assertEqual(p.y, 10) - p = Point(4,5) - m = AffineTransformation.scaling(1.5, -2) - p.transform(m) - self.assertEqual(p.x, 6) - self.assertEqual(p.y, -10) + p = Point(4, 5) + m = AffineTransformation.scaling(1.5, -2) + p.transform(m) + self.assertEqual(p.x, 6) + self.assertEqual(p.y, -10) - def test_multiple_application(self): - p = Point(10,10) - m = AffineTransformation() - m.translate(Point(6,5)) - m.scale(1.5,2) - p.transform(m) - self.assertEqual(p.x, 24) - self.assertEqual(p.y, 30) + def test_multiple_application(self): + p = Point(10, 10) + m = AffineTransformation() + m.translate(Point(6, 5)) + m.scale(1.5, 2) + p.transform(m) + self.assertEqual(p.x, 24) + self.assertEqual(p.y, 30)