def test_draw_polyline_and_polygon_examples(self): image = mathsvg.SvgImage(pixel_density=20, view_window=((0, 0), (8, 8))) point_list = [(2.5, 5), (4.5, 7), (2.5, 4), (0.5, 3), (6, 2)] image.draw_polyline(point_list) xml_data = image.svgwrite_object.get_xml()[1] self.assertEqual('polyline', xml_data.tag) coords = [ round(float(c)) for c in xml_data.attrib['points'].replace(',', ' ').split() ] self.assertSequenceEqual([50, 61, 90, 21, 50, 81, 10, 101, 120, 121], coords) image = mathsvg.SvgImage(pixel_density=20, view_window=((0, 0), (8, 8))) point_list = [(2.5, 5), (4.5, 7), (2.5, 4), (0.5, 3), (6, 2)] image.draw_polygon(point_list) xml_data = image.svgwrite_object.get_xml()[1] self.assertEqual('polygon', xml_data.tag) coords = [ round(float(c)) for c in xml_data.attrib['points'].replace(',', ' ').split() ] self.assertSequenceEqual([50, 61, 90, 21, 50, 81, 10, 101, 120, 121], coords)
def test_saved_file_contains_svg(self): image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.save("test.svg") check_file_content(self, "test.svg", "<svg.*</svg>$", "saved file doesnt contain svg") os.remove("test.svg")
def test_set_arrow_options_example(self): image = mathsvg.SvgImage(pixel_density=20, view_window=((-4, -4), (4, 4))) image.set_arrow_options(curvature=0.55) image.draw_arrow([-2, -2], [2, 1.7]) image.set_arrow_options(width=4 * image.arrow_width_svgpx, units='svg') image.draw_arrow([-2, -2], [2, 1.2]) image.reset_arrow_options() image.set_arrow_options(curvature=0) image.draw_arrow([-2, -2], [2, 0.6]) xml_data = image.svgwrite_object.get_xml() tags = [d.tag for d in xml_data[1:7]] self.assertSequenceEqual( ['line', 'path', 'line', 'path', 'line', 'path'], tags) self._check_line_coords(xml_data[1].attrib, 40, 120, 121, 47) self._check_line_coords(xml_data[3].attrib, 40, 120, 121, 57) self._check_line_coords(xml_data[5].attrib, 40, 120, 121, 69) path = xml_data[2].attrib['d'].strip().split() self.assertEqual(21, len(path)) self.assertEqual('M', path[0]) self.assertEqual('C', path[7]) self.assertEqual('Z', path[-1]) path = xml_data[4].attrib['d'].strip().split() self.assertEqual(21, len(path)) self.assertEqual('M', path[0]) self.assertEqual('C', path[7]) self.assertEqual('Z', path[-1]) path = xml_data[6].attrib['d'].strip().split() self.assertEqual(21, len(path)) self.assertEqual('M', path[0]) self.assertEqual('C', path[7]) self.assertEqual('Z', path[-1])
def test_default_drawing_options(self): image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.draw_arrow([-2, -2], [2, 2]) image.save("test.svg", do_overwrite=True) check_file_content(self, "test.svg", default_drawing_options, "default drawing options not in use for line") os.remove("test.svg")
def test_default_arrow_svg(self): image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.draw_arrow([-2, -2], [2, 2]) image.save("test.svg", do_overwrite=True) arrow_regex = "<line.*x1 *=.*x2 *=.*y1 *=.*y2 *=.*/><path *d *= *\"" arrow_regex += " *M" + number_list_re_pattern + "C" + number_list_re_pattern + "Z *\" *" arrow_regex += default_drawing_options check_file_content(self, "test.svg", arrow_regex, "arrow not drawn")
def test_save_image(self): clean_files([ "test.svg", ]) image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.save("test.svg") self.assertTrue(os.path.exists("test.svg"), "file save failed") os.remove("test.svg")
def test_main_doc_example(self): image = mathsvg.SvgImage(pixel_density=100, view_window=((-1, -1), (1, 1))) image.draw_circle([0, 0], 1.1) xml_data = image.svgwrite_object.get_xml()[1] self.assertEqual(xml_data.tag, 'ellipse') self.assertEqual(int(xml_data.attrib['cx']), 100) self.assertEqual(int(xml_data.attrib['cy']), 101) self.assertEqual(round(float(xml_data.attrib['rx'])), 110) self.assertEqual(round(float(xml_data.attrib['ry'])), 110)
def test_default_arrow_images(self): image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.draw_arrow([-2, -2], [2, 2]) image.save("test-default-arrow.svg") check_actual_image( self, "test-default-arrow.svg", os.path.join(test_models_path, "test-default-arrow.png"), "default arrow image very different from model") clean_files(['test-default-arrow.svg'])
def test_set_dash_mode_example(self): image = mathsvg.SvgImage(pixel_density=20, view_window=((0, 0), (10, 10))) image.set_dash_mode("dash") image.draw_line_segment([0, 0], [10, 10]) image.set_dash_mode("dot") image.draw_line_segment([0, 10], [10, 0]) image.set_svg_options(dash_array=[18, 3, 1, 3, 7, 3, 1, 3], units='svg') image.set_dash_mode("dasharray") image.draw_planar_potato([5, 5], 2, 4, 8) xml_data = image.svgwrite_object.get_xml() line_data = xml_data[1] self.assertEqual('line', line_data.tag) line_data = line_data.attrib self.assertEqual(0, round(float(line_data['x1']))) self.assertEqual(200, round(float(line_data['x2']))) self.assertEqual(201, round(float(line_data['y1']))) self.assertEqual(1, round(float(line_data['y2']))) style = line_data['style'] match = re.search(r'stroke-dasharray *: *([0-9\.]+), *([0-9\.]+);', style) self.assertIsNotNone(match) self.assertAlmostEqual(4., float(match.groups()[0]), places=3) self.assertAlmostEqual(4., float(match.groups()[1]), places=3) line_data = xml_data[2] self.assertEqual('line', line_data.tag) line_data = line_data.attrib self.assertEqual(0, round(float(line_data['x1']))) self.assertEqual(200, round(float(line_data['x2']))) self.assertEqual(1, round(float(line_data['y1']))) self.assertEqual(201, round(float(line_data['y2']))) style = line_data['style'] match = re.search(r'stroke-dasharray *: *([0-9\.]+), *([0-9\.]+);', style) self.assertIsNotNone(match) self.assertAlmostEqual(1., float(match.groups()[0]), places=3) self.assertAlmostEqual(2., float(match.groups()[1]), places=3) line_data = xml_data[3] self.assertEqual('path', line_data.tag) style = line_data.attrib['style'] match = re.search(r'stroke-dasharray *: *([0-9\., ]+);', style) self.assertIsNotNone(match) values = [round(float(v)) for v in match.groups()[0].split(', ')] self.assertEqual(len(values), 8) self.assertSequenceEqual([18, 3, 1, 3, 7, 3, 1, 3], values)
def test_set_svg_options_examples(self): image = mathsvg.SvgImage(pixel_density=200, view_window=((0, 0), (12, 2))) image.set_svg_options(stroke_color="red") self.assertEqual('red', image.stroke_color) image.draw_point((1, 1)) image.reset_svg_options() self.assertEqual('black', image.stroke_color) image.draw_point((4, 1)) xml_data = image.svgwrite_object.get_xml()[1] self.assertEqual('circle', xml_data.tag) style = xml_data.attrib['style'] match = re.search(r'; *stroke *: *([a-zA-Z]+) *;', style) self.assertIsNotNone(match) self.assertEqual('red', match.groups()[0]) xml_data = image.svgwrite_object.get_xml()[2] self.assertEqual('circle', xml_data.tag) style = xml_data.attrib['style'] match = re.search(r'; *stroke *: *([a-zA-Z]+) *;', style) self.assertIsNotNone(match) self.assertEqual('black', match.groups()[0])
def test_rectangle_and_polygons(self): image = mathsvg.SvgImage(pixel_density=20, view_window=((0, 0), (8, 8))) point_list = [(2.5, 5), (4.5, 7), (2.5, 4), (0.5, 3), (6, 2)] image.draw_polygon(point_list) image.draw_rectangle(7, 1, 1, 7) xml_data = image.svgwrite_object.get_xml() self.assertEqual(xml_data[1].tag, 'polygon') coords = [ round(float(c)) for c in xml_data[1].attrib['points'].replace(',', ' ').split() ] self.assertSequenceEqual(coords, [50, 61, 90, 21, 50, 81, 10, 101, 120, 121]) xml_rect_data = xml_data[2] self.assertEqual(xml_rect_data.tag, 'polygon') coords = [ round(float(c)) for c in xml_rect_data.attrib['points'].replace(',', ' ').split() ] self.assertSequenceEqual(coords, [20, 21, 20, 141, 140, 141, 140, 21, 20, 21])
ellipse_shift = shift_factor * r focuses = [ [- d, ellipse_shift], [d, ellipse_shift] ] focuses = [ [ f[0] + torus_position[0], f[1] + torus_position[1] ] for f in focuses ] image.draw_ellipse_arc(focuses, b, math.pi, two_pi) focuses = [ [- d, - ellipse_shift], [d, - ellipse_shift] ] focuses = [ [ f[0] + torus_position[0], f[1] + torus_position[1] ] for f in focuses ] angle_start = math.asin(ellipse_shift / b) image.draw_ellipse_arc(focuses, b, angle_start, math.pi - angle_start) image = mathsvg.SvgImage(pixel_density = rescaling, view_window = ((-4, -4), (4, 4))) draw_a_torus(image, [0, 0], 6.2) draw_a_torus(image, [3.1,-2.2], 0.63) draw_a_torus(image, [2.07,-2.7], 0.63) draw_a_torus(image, [2.81,-3.01], 0.63) draw_a_torus(image, [2.5, 2.5], 1.3, thinness = 0.8) draw_a_torus(image, [-2.5, 2.5], 1.3, thinness = 0.2) image.set_dash_mode("dash") draw_a_torus(image, [-2.9,-2.2], 1.78, thinness = 0.35) image.save("torus.svg", do_overwrite = True)
# Author: alexn11 ([email protected]) # Created: 2019-05-25 # Copyright (C) 2019, 2020, Alexandre De Zotti # License: MIT License import mathsvg image = mathsvg.SvgImage(view_window=((0., 0.), (4., 4.)), pixel_density=100) image.draw_circle((1., 3.), 0.6) image.draw_circle((3., 3.), 0.6) image.draw_circle((1., 1.), 0.6) image.draw_arrow((1., 1.7), (1., 2.3)) image.draw_arrow((1.5, 1.5), (2.5, 2.5)) image.draw_arrow((1.7, 3.), (2.3, 3.)) image.put_text("Z", (.9, .9), font_size=.3) image.put_text("A", (.9, 2.9), font_size=.3) image.put_text("B", (2.9, 2.9), font_size=.3) image.save("put-text-example.svg")
# Author: alexn11 ([email protected]) # Created: 2018-10-21 # Copyright (C) 2018, 2020, Alexandre De Zotti # License: MIT License import mathsvg image = mathsvg.SvgImage(pixel_density=100, view_window=((0, 0), (10, 10))) open_curve_point_list = [[2.5, 5], [4.5, 7], [2.5, 4], [0.5, 3]] closed_curve_point_list = [[7.4, 2], [5.6, 4], [7.3, 6], [4.3, 5.2], [8.3, 9.1]] image.set_svg_options(stroke_color="red") image.draw_polyline(open_curve_point_list) image.set_svg_options(stroke_color="green") image.draw_polyline(closed_curve_point_list) image.draw_line_segment(closed_curve_point_list[-1], closed_curve_point_list[0]) image.set_svg_options(stroke_color="black") image.draw_smoothly_interpolated_open_curve(open_curve_point_list) image.draw_smoothly_interpolated_closed_curve(closed_curve_point_list) image.save("interpolated-curves.svg")
# Copyright (C) 2020, 2021 Alexandre De Zotti # License: MIT License import math import os import sys sys.path.insert(0, os.path.abspath(os.path.pardir)) import mathsvg def a_function_with_one_parameter(t, r): return (r * math.cos(2. * math.pi * t), r * math.sin(2. * math.pi * t)) image = mathsvg.SvgImage(pixel_density=100, view_window=((-1.1, -1.5), (2.5, 2.1))) graph_function = lambda t: (math.sin(10 * math.pi * t) + 0.1, math.cos(6 * math.pi * t)) image.set_svg_options(stroke_color="black") image.draw_parametric_graph(graph_function, 0, 1, 40, curve_type="autosmooth", is_closed=False) image.set_svg_options(stroke_color="blue") image.draw_parametric_graph(graph_function, 0, 1, 40,
if (y < 0): y += height if (y >= height): y -= height return y # derived params height = 2**it # main prog tongue_family = tonglib.tongue_system(map_family) im = mathsvg.SvgImage(pixel_density=100, view_window=((-padding, -padding), (1 + padding, height + padding))) old_stroke_width = im.stroke_width im.stroke_width *= 0.5 im.set_dash_mode("dash") im.draw_arrow((-0.5 * padding, 0.), (1. + 0.5 * padding, 0.)) im.draw_arrow((0., -0.5 * padding), (0., height + 0.5 * padding)) im.draw_line_segment((0., height), (1., height)) im.draw_line_segment((1., height), (1., 0.)) im.point_size = 0.5 * old_stroke_width nb_x = height * 57 x_step = 1 / (nb_x - 1) for j in range(nb_x): x = j * x_step
break return lengths_list image_name = object_type + ".svg" image_main_scale = 800 padding = 0.1 lengths = compute_lengths(max_length, smallest_interval, density) #print(lengths) if (object_type == "disconnected-straight-brush"): view_window = ((-padding, -padding), (1 + padding, 1 + padding)) image = mathsvg.SvgImage(pixel_density=image_main_scale, view_window=view_window) for element in lengths: image.draw_line_segment([element[0], 0], [element[0], element[1]]) elif (object_type == "compact-cantor-bouquet"): view_window = ((-1 - padding, -1 - padding), (1 + padding, 1 + padding)) image = mathsvg.SvgImage(pixel_density=image_main_scale, view_window=view_window) for element in lengths: endpoint = element[1] * cmath.exp(2. * math.pi * element[0] * 1.j) image.draw_line_segment([0, 0], [endpoint.real, endpoint.imag]) elif (object_type == "one-sided-hairy-circle"): view_window = ((-1 - padding, -1 - padding), (1 + padding, 1 + padding)) image = mathsvg.SvgImage(pixel_density=image_main_scale, view_window=view_window) image.draw_circle((0, 0), 0.5) for element in lengths:
def prepare_simple_canvas(window_size=1., pixel_density=100): return mathsvg.SvgImage(view_window=((0, 0), (window_size, window_size)), pixel_density=pixel_density)
# Author: alexn11 ([email protected]) # Created: 2018-10-21 # Copyright (C) 2018, 2020, Alexandre De Zotti # License: MIT License import math import cmath import mathsvg image = mathsvg.SvgImage(pixel_density=100, view_window=((-2, -2), (2, 2))) center = [0, 0] inner_radius = 0.5 outer_radius = 1.5 image.set_dash_mode("dots") image.draw_circle(center, inner_radius) image.draw_circle(center, outer_radius) image.set_dash_mode("none") image.draw_planar_potato(center, inner_radius, outer_radius, 5) image.save("potato-regions.svg")
image.set_svg_options(stroke_color="red") image.draw_cross(center) image.set_svg_options(stroke_color=drawings_stroke_color) # if (do_draw_intermediate_levels): draw_triangle(image, center, size) next_centers += compute_next_centers( center, next_centers_relative_positions) size = next_size centers = next_centers # last level doesn't need to compute next centers etc. for center in centers: if (do_draw_centers): # debugging image.set_svg_options(stroke_color="red") image.draw_cross(center) image.set_svg_options(stroke_color="black") # draw_triangle(image, center, size) return image = mathsvg.SvgImage(pixel_density=image_size / 2, view_window=view_window) image.set_svg_options(fill_color=drawings_fill_color, stroke_color=drawings_stroke_color) draw_triforce(image, start_size, rescaling_factor, nb_levels) image.save("selfsim-triforce.svg")
# Author: alexn11 ([email protected]) # Created: 2018-10-21 # Copyright (C) 2018, 2020, Alexandre De Zotti # License: MIT License import sys import math import cmath import mathsvg general_size = 10 bottom = -1.5 * general_size left = -1.5 * general_size top = -bottom right = -left image_file_name = sys.argv[0][:-3] + ".svg" image = mathsvg.SvgImage(pixel_density=42, view_window=((left, bottom), (right, top))) image.draw_planar_potato([0.25 * general_size, 0], 0.2 * general_size, general_size, 66) image.save(image_file_name)
# Author: alexn11 ([email protected]) # Created: 2018-10-21 # Copyright (C) 2018, 2020, Alexandre De Zotti # License: MIT License import math import cmath import mathsvg image = mathsvg.SvgImage(pixel_density=1, view_window=((0, 0), (400, 150))) image.draw_random_wavy_line([20, 75], [380, 75], 2, 55) image.save("scribble.svg")
# Author: alexn11 ([email protected]) # Created: 2018-10-21 # Copyright (C) 2018, 2020, Alexandre De Zotti # License: MIT License import mathsvg image = mathsvg.SvgImage(pixel_density=100, view_window=((-4, -4), (4, 4))) image.draw_point([0.2, -0.2]) image.set_dash_mode("dots") image.draw_curved_arrow([0.2, -.2], [1.7, 1.8], curvedness=-1.) image.set_dash_mode("none") image.draw_curved_arrow([-2.7, 2], [-0.3, 2], asymmetry=-0.8) image.draw_curved_arrow([-2.7, 1], [-0.3, 1], asymmetry=-0.2) image.draw_curved_arrow([-2.7, 0], [-0.3, 0], asymmetry=0.2) image.draw_curved_arrow([-2.7, -1], [-0.3, -1], asymmetry=0.5) image.draw_curved_arrow([-2.7, -2], [-0.3, -2], curvedness=-0.2, asymmetry=1.2) image.save("more-curved-arrows.svg")
curvature=0, units='svg') g.set_point_size(0.01) g.draw_line_segment((x0, 0), (x0, x0)) for i, x in enumerate(xs[:-2]): x_next = xs[i + 1] g.draw_polyline([(x, x), (x, x_next), (x_next, x_next)]) mid_value = 0.5 * (x + x_next) draw_mid_point_arrows(x, x_next) g.draw_polyline([(xs[-2], xs[-2]), (xs[-2], xs[-1])]) g.draw_point((xs[-2], xs[-1])) draw_mid_point_arrows(xs[-2], xs[-1], both=False) g.reset_dash_and_dot_structures() # --------------------------------------------------------------------- eval_map = lambda x: eval_logistic_map(r, x) graph = mathsvg.SvgImage(pixel_density=600, view_window=((0 - spacing, 0 - spacing), (1 + spacing, 1 + spacing))) arrow_size = graph.arrow_width_svgpx draw_frame(graph) draw_graph(graph, eval_map) draw_iterations(graph, eval_map, x0, n) graph.save('iteration-graph.svg')