def test_translate(): p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(3) p.arc_left(90, 3) p.turn_left(90) p.move_forward(3) p.fill_mode() p.circle(0.5) p.move_forward(3) p.square(1) p.paper.translate((1, 1)) assert_equal( p.paper.svg_elements(1), [ ( '<path d="M1.0,-1.5 L1.0,-0.5 L4.0,-0.5 A 3.5,3.5 0 0 0 ' '7.5,-4.0 L6.5,-4.0 A 2.5,2.5 0 0 1 4.0,-1.5 L1.0,-1.5 z" ' 'fill="#000000" />' ), ( '<path d="M4.5,-4.0 A 0.5,0.5 0 0 0 3.5,-4.0 ' 'A 0.5,0.5 0 0 0 4.5,-4.0 z" fill="#000000" />' ), ( '<path d="M0.5,-3.5 L1.5,-3.5 L1.5,-4.5 L0.5,-4.5 L0.5,-3.5 z" ' 'fill="#000000" />' ), ] )
def test_fuse_with_joint(): p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(180) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.break_stroke() p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) assert_path_data( p, 0, [ 'M0,1 L0,-1 L-6,-1 L-6,5 L-4,5 L-4,1 L0,1 z', 'M0,-1 L0,1 L5,1 L5,-1 L0,-1 z', ] ) p.paper.join_paths() p.paper.fuse_paths() assert_path_data( p, 0, 'M-6,5 L-4,5 L-4,1 L5,1 L5,-1 L-6,-1 L-6,5 z' )
def test_translate(): p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(3) p.arc_left(90, 3) p.turn_left(90) p.move_forward(3) p.fill_mode() p.circle(0.5) p.move_forward(3) p.square(1) p.paper.translate((1, 1)) assert_equal(p.paper.svg_elements(1), [ ('<path d="M1.0,-1.5 L1.0,-0.5 L4.0,-0.5 A 3.5,3.5 0 0 0 ' '7.5,-4.0 L6.5,-4.0 A 2.5,2.5 0 0 1 4.0,-1.5 L1.0,-1.5 z" ' 'fill="#000000" />'), ('<path d="M4.5,-4.0 A 0.5,0.5 0 0 0 3.5,-4.0 ' 'A 0.5,0.5 0 0 0 4.5,-4.0 z" fill="#000000" />'), ('<path d="M0.5,-3.5 L1.5,-3.5 L1.5,-4.5 L0.5,-4.5 L0.5,-3.5 z" ' 'fill="#000000" />'), ])
def test_fuse_with_joint(): p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(180) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.break_stroke() p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) assert_path_data(p, 0, [ 'M0,1 L0,-1 L-6,-1 L-6,5 L-4,5 L-4,1 L0,1 z', 'M0,-1 L0,1 L5,1 L5,-1 L0,-1 z', ]) p.paper.join_paths() p.paper.fuse_paths() assert_path_data(p, 0, 'M-6,5 L-4,5 L-4,1 L5,1 L5,-1 L-6,-1 L-6,5 z')
def test_join_paths_thick(): # Segments join together if possible when join_paths is called. p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) p.break_stroke() p.turn_left(90) p.line_forward(5) p.paper.join_paths() assert_path_data(p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z')
def test_join_paths_thick(): # Segments join together if possible when join_paths is called. p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) p.break_stroke() p.turn_left(90) p.line_forward(5) p.paper.join_paths() assert_path_data( p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z' )
def test_copy_custom_cap(): # Regression test for a bug where doing pen.copy() in a cap function would # break outline drawing. p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) p.turn_left(90) p.line_forward(5) def copy_cap(pen, end): pen.copy() pen.line_to(end) p.last_segment().end_cap = copy_cap assert_path_data(p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z')
def test_copy_custom_cap(): # Regression test for a bug where doing pen.copy() in a cap function would # break outline drawing. p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) p.turn_left(90) p.line_forward(5) def copy_cap(pen, end): pen.copy() pen.line_to(end) p.last_segment().end_cap = copy_cap assert_path_data( p, 0, 'M0,-1 L0,1 L6,1 L6,-5 L4,-5 L4,-1 L0,-1 z' )
def test_arc_segment_bounds(): # Arc which occupies its entire circle. p = Pen() p.fill_mode() p.move_to((1, 0)) p.turn_to(90) p.arc_left(359, 1) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(-1, -1, 1, 1) ) # Arc which pushes the boundary only with the endpoints. p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(30) p.move_forward(1) p.turn_left(90) p.arc_left(30, center=(0, 0)) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(0.5, 0.5, sqrt3 / 2, sqrt3 / 2) ) # Arc which pushes the boundary with the middle in one spot. p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(-45) p.move_forward(1) p.turn_left(90) p.arc_left(90, center=(0, 0)) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(sqrt2 / 2, -sqrt2 / 2, 1, sqrt2 / 2) ) # Arc which goes right. p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(45) p.arc_right(90, 3) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(0, 0, 3 * sqrt2, 3 - 1.5 * sqrt2) ) # Arc which pushes the boundary with the middle in two spots. p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(-45) p.move_forward(1) p.turn_left(90) p.arc_left(180, center=(0, 0)) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(-sqrt2 / 2, -sqrt2 / 2, 1, 1) ) # Half circle, right side p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(0) p.arc_right(180, 5) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(0, -10, 5, 0) ) # Thick circle, p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.move_forward(5) p.turn_left(90) p.arc_left(180, 5, start_slant=45) arc = p.last_segment() assert_equal( arc.bounds(), Bounds(-5.5, -0.5314980314970469, 5.5, 5.5) )
from canoepaddle import Pen p = Pen() p.fill_mode('green') p.move_to((0, 0)) p.turn_to(0) radius = 0.01 for _ in range(200): p.circle(radius) p.turn_left(20) new_radius = radius * 1.05 p.move_forward(radius + new_radius) radius = new_radius print(p.paper.format_svg())