def test_line_line_half_illegal_joint(): # The outside edge meets, but the inside is too short to meet. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(2) p.turn_left(165) p.line_forward(2) assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L5.80,0.50 L0.20,-1.00 ' 'L-0.06,-0.03 L1.87,0.48 L2.00,-0.50 L0.00,-0.50 z' ) ) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(2) p.turn_right(165) p.line_forward(2) assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L2.00,0.50 L1.87,-0.48 ' 'L-0.06,0.03 L0.20,1.00 L5.80,-0.50 L0.00,-0.50 z' ) )
def test_outliner_mode(): # We can set up a pattern in one mode, p = Pen() p.set_mode(StrokeOutlineMode(sqrt3, 0.2 * sqrt3, 'blue', 'black')) p.move_to((0, 0)) p.turn_to(0) p.line_forward(5, end_slant=60) # Then continue it in another mode without caring what the first mode was. old_mode = p.mode p.set_mode(p.mode.outliner_mode()) p.turn_to(60) p.move_forward(1.0) p.turn_left(60) p.line_forward(2.0) p.turn_right(120) p.line_forward(2.0) p.turn_right(120) p.line_forward(2.0) p.turn_to(60) p.move_forward(3.0) p.turn_to(120) p.set_mode(old_mode) p.line_forward(5, start_slant=60) assert_svg_file( p, 3, 'test_outliner_mode.svg' )
def test_arc_angle_error(): # Endpoints with certain angles do not go all the way across the # stroke, and are disallowed. p = Pen() p.stroke_mode(1.0) p.arc_left(90, 10, start_slant=0) seg = p.last_segment() assert seg.start_joint_illegal assert not seg.end_joint_illegal p = Pen() p.stroke_mode(1.0) p.arc_left(90, 10, end_slant=90) seg = p.last_segment() assert not seg.start_joint_illegal assert seg.end_joint_illegal p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.arc_left(90, radius=5, start_slant=25) seg = p.last_segment() assert seg.start_joint_illegal assert not seg.end_joint_illegal # A combination of angles can also create a degenerate arc. p = Pen() p.stroke_mode(1.0) p.turn_toward((1, 0)) p.turn_left(1) p.arc_to((1, 0), start_slant=40, end_slant=-40) seg = p.last_segment() assert seg.start_joint_illegal assert seg.end_joint_illegal
def test_line_line_half_illegal_joint(): # The outside edge meets, but the inside is too short to meet. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(2) p.turn_left(165) p.line_forward(2) assert_path_data(p, 2, ('M0.00,-0.50 L0.00,0.50 L5.80,0.50 L0.20,-1.00 ' 'L-0.06,-0.03 L1.87,0.48 L2.00,-0.50 L0.00,-0.50 z')) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(2) p.turn_right(165) p.line_forward(2) assert_path_data(p, 2, ('M0.00,-0.50 L0.00,0.50 L2.00,0.50 L1.87,-0.48 ' 'L-0.06,0.03 L0.20,1.00 L5.80,-0.50 L0.00,-0.50 z'))
def test_offwidth_arc_joins(): # Join arcs and lines of different widths. p = Pen() p.move_to((0, 0)) p.turn_to(0) p.stroke_mode(0.8) p.line_forward(5) p.turn_left(45) p.stroke_mode(3.0) p.arc_left(90, 5) p.turn_to(-180) p.line_forward(5) p.turn_left(45) p.stroke_mode(0.8) p.arc_left(45, 5) p.turn_right(90) p.stroke_mode(3.0) p.arc_right(90, 4) assert_svg_file( p, 3, 'test_offwidth_arc_joins.svg' )
def test_start_slant_legal_joint(): # Create a joint that is only legal because of the start slant. p = Pen() p.outline_mode(1.0, 0.1) p.move_to((0, 0)) p.turn_to(0) p.line_forward(0.8, start_slant=-45) p.turn_left(90) p.outline_mode(2.0, 0.1) p.line_forward(5)
def test_long_line_thick(): p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) for _ in range(2): p.line_forward(5) p.turn_right(90) p.line_forward(5) p.turn_left(90) assert_path_data( p, 0, 'M0,-1 L0,1 L4,1 L4,6 L9,6 L9,10 L11,10 L11,4 L6,4 L6,-1 L0,-1 z' )
def test_offwidth_joint(): p = Pen() p.stroke_mode(1.0) p.turn_to(0) p.move_forward(-3) p.line_forward(3) p.stroke_mode(0.5) p.turn_left(90) p.line_forward(3) assert_path_data( p, 2, ( 'M-3.00,-0.50 L-3.00,0.50 L0.25,0.50 L0.25,-3.00 ' 'L-0.25,-3.00 L-0.25,-0.50 L-3.00,-0.50 z' ), )
def test_arc_line_joint(): p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(3) p.turn_left(90) p.arc_left(180, 3) assert_path_data( p, 3, ( 'M0.000,-0.500 L0.000,0.500 L3.464,0.500 ' 'A 3.500,3.500 0 1 0 -3.500,0.000 L-2.500,0.000 ' 'A 2.500,2.500 0 0 1 2.449,-0.500 L0.000,-0.500 z' ), )
def test_various_joins(): p = Pen() p.stroke_mode(0.5) p.move_to((-2, 0)) p.turn_to(0) p.line_forward(1) p.turn_left(90) p.line_forward(1) p.turn_right(90) p.arc_right(90, 1) p.arc_left(90, 1) p.turn_left(90) p.line_forward(1) p.paper.override_bounds(-3, -3, 3, 3) assert_svg_file( p, 2, 'test_various_joins.svg', )
def test_arc_start_slant_bug(): # Some arcs are not reporting their start and end slants correctly. # Set up positions on a circle at angles -120 and 30 p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(30) p.move_forward(3) p1 = p.position p.turn_left(90) h1 = p.heading p.move_to((0, 0)) p.turn_to(-120) p.move_forward(3) p2 = p.position # Create an arc using arc_left. p = Pen() p.fill_mode() p.move_to(p1) p.turn_to(h1) p.arc_left(210, 3) arc = p.last_segment() assert_almost_equal(arc.start_heading, 120) assert_almost_equal(arc.end_heading, 330) # Create the same arc using arc_to. p = Pen() p.fill_mode() p.move_to(p1) p.turn_to(h1) p.arc_to(p2) arc = p.last_segment() assert_almost_equal(arc.start_heading.theta, 120) assert_almost_equal(arc.end_heading.theta, 330)
def test_joint_loop_color(): p = Pen() p.move_to((0, 0)) p.turn_to(0) # Draw a square with one side a different color. It joins to the # beginning correctly. p.stroke_mode(2.0, color='black') p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.stroke_mode(2.0, color='red') p.line_forward(5) assert_equal(len(p.paper.paths), 1) assert_path_data( p, 0, [ 'M1,-1 L-1,1 L6,1 L6,-6 L-1,-6 L1,-4 L4,-4 L4,-1 L1,-1 z', 'M1,-4 L-1,-6 L-1,1 L1,-1 L1,-4 z', ] )
def test_mode(): # Fill mode square. p = Pen() p.fill_mode() p.move_to((0, 0)) p.turn_to(0) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.line_forward(5) assert_path_data( p, 0, 'M0,0 L5,0 L5,-5 L0,-5 L0,0 z', )
def test_joint_loop(): p = Pen() p.stroke_mode(2.0) p.move_to((0, 0)) p.turn_to(0) # Draw a square. p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.line_forward(5) p.turn_left(90) p.line_forward(5) assert_path_data( p, 0, ( 'M-1,1 L6,1 L6,-6 L-1,-6 L-1,1 z ' 'M1,-1 L1,-4 L4,-4 L4,-1 L1,-1 z' ) )
def test_too_sharp_joint(): # Joint is considered too sharp, so the joint is not made. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(10) p.turn_left(175) p.line_forward(10) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L10.00,0.50 L10.04,-0.50 L0.08,-1.37 ' 'L-0.01,-0.37 L9.96,0.50 L10.00,-0.50 L0.00,-0.50 z' ) ) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(10) p.turn_right(175) p.line_forward(10) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L10.00,0.50 L9.96,-0.50 L-0.01,0.37 ' 'L0.08,1.37 L10.04,0.50 L10.00,-0.50 L0.00,-0.50 z' ) ) # Joint is considered too sharp, so the outside is not drawn, but the # inside joint works. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(20) p.turn_left(175) p.line_forward(20) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L20.00,0.50 L20.04,-0.50 ' 'L0.12,-2.24 L0.03,-1.25 L8.55,-0.50 L0.00,-0.50 z' ) ) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(20) p.turn_right(175) p.line_forward(20) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ( 'M0.00,-0.50 L0.00,0.50 L8.55,0.50 L0.03,1.25 ' 'L0.12,2.24 L20.04,0.50 L20.00,-0.50 L0.00,-0.50 z' ) )
def test_too_sharp_joint(): # Joint is considered too sharp, so the joint is not made. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(10) p.turn_left(175) p.line_forward(10) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ('M0.00,-0.50 L0.00,0.50 L10.00,0.50 L10.04,-0.50 L0.08,-1.37 ' 'L-0.01,-0.37 L9.96,0.50 L10.00,-0.50 L0.00,-0.50 z')) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(10) p.turn_right(175) p.line_forward(10) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data( p, 2, ('M0.00,-0.50 L0.00,0.50 L10.00,0.50 L9.96,-0.50 L-0.01,0.37 ' 'L0.08,1.37 L10.04,0.50 L10.00,-0.50 L0.00,-0.50 z')) # Joint is considered too sharp, so the outside is not drawn, but the # inside joint works. p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(20) p.turn_left(175) p.line_forward(20) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data(p, 2, ('M0.00,-0.50 L0.00,0.50 L20.00,0.50 L20.04,-0.50 ' 'L0.12,-2.24 L0.03,-1.25 L8.55,-0.50 L0.00,-0.50 z')) p = Pen() p.stroke_mode(1.0) p.move_to((0, 0)) p.turn_to(0) p.line_forward(20) p.turn_right(175) p.line_forward(20) line1, line2 = p.last_path().segments assert line1.end_joint_illegal assert line2.start_joint_illegal assert_path_data(p, 2, ('M0.00,-0.50 L0.00,0.50 L8.55,0.50 L0.03,1.25 ' 'L0.12,2.24 L20.04,0.50 L20.00,-0.50 L0.00,-0.50 z'))
def test_arc_arc_joint(): top = (0, 5) left = (-2, 0) right = (2, 0) # Convex-convex. p = Pen() p.stroke_mode(1.0) p.move_to(left) p.turn_toward(top) p.turn_left(5) p.arc_to(top, start_slant=0) p.turn_toward(right) p.turn_left(5) p.arc_to(right, end_slant=0) assert_path_data( p, 3, ( 'M-2.522,0.000 L-1.477,0.000 ' 'A 30.394,30.394 0 0 1 0.000,-3.853 ' 'A 30.394,30.394 0 0 1 1.477,0.000 ' 'L2.522,0.000 ' 'A 31.394,31.394 0 0 0 0.000,-6.076 ' 'A 31.394,31.394 0 0 0 -2.522,0.000 z' ) ) # Concave-concave. p = Pen() p.stroke_mode(1.0) p.move_to(left) p.turn_toward(top) p.turn_right(5) p.arc_to(top, start_slant=0) p.turn_toward(right) p.turn_right(5) p.arc_to(right, end_slant=0) assert_path_data( p, 3, ( 'M-2.561,0.000 L-1.441,0.000 ' 'A 31.394,31.394 0 0 0 0.000,-3.400 ' 'A 31.394,31.394 0 0 0 1.441,0.000 ' 'L2.561,0.000 ' 'A 30.394,30.394 0 0 1 0.000,-6.923 ' 'A 30.394,30.394 0 0 1 -2.561,0.000 z' ) ) # Convex-concave. p = Pen() p.stroke_mode(1.0) p.move_to(left) p.turn_toward(top) p.turn_left(5) p.arc_to(top, start_slant=0) p.turn_toward(right) p.turn_right(5) p.arc_to(right, end_slant=0) assert_path_data( p, 3, ( 'M-2.522,0.000 L-1.477,0.000 ' 'A 30.394,30.394 0 0 1 -0.090,-3.656 ' 'A 31.394,31.394 0 0 0 1.441,0.000 ' 'L2.561,0.000 ' 'A 30.394,30.394 0 0 1 0.144,-6.339 ' 'A 31.394,31.394 0 0 0 -2.522,0.000 z' ) ) # Concave-convex. p = Pen() p.stroke_mode(1.0) p.move_to(left) p.turn_toward(top) p.turn_right(5) p.arc_to(top, start_slant=0) p.turn_toward(right) p.turn_left(5) p.arc_to(right, end_slant=0) assert_path_data( p, 3, ( 'M-2.561,0.000 L-1.441,0.000 ' 'A 31.394,31.394 0 0 0 0.090,-3.656 ' 'A 30.394,30.394 0 0 1 1.477,0.000 ' 'L2.522,0.000 ' 'A 31.394,31.394 0 0 0 -0.144,-6.339 ' 'A 30.394,30.394 0 0 1 -2.561,0.000 z' ) )