{"id":2404,"date":"2015-05-21T01:23:31","date_gmt":"2015-05-21T01:23:31","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2404"},"modified":"2015-05-21T01:23:31","modified_gmt":"2015-05-21T01:23:31","slug":"rendering-an-svg-containing-a-zigzag-stitch-in-python","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rendering-an-svg-containing-a-zigzag-stitch-in-python\/","title":{"rendered":"Rendering an SVG containing a zigzag stitch in Python"},"content":{"rendered":"<p>The following will render an SVG resembling a zig-zag sewing stitch, using python:<\/p>\n<pre lang=\"python\">\nimport pysvg.structure\nimport pysvg.builders\nimport pysvg.text\nimport math\nimport numpy as np\n\nfrom IPython.core.display import SVG\n\nsvg_document = pysvg.structure.Svg()\n\nshape_builder = pysvg.builders.ShapeBuilder()\n\nstyle = pysvg.builders.StyleBuilder()\nstyle.setStrokeWidth(2)\nstyle.setStroke('black')\n\ndef complete(doc, file):\n\t\tdoc.save(file)\n\ndef line(a, b):\n    if (a == None):\n        return\n    if (b == None):\n        return\n    (x1, y1) = a\n    (x2, y2) = b\n\n    # allows for textures that return where they start\n    if (x1 == x2 and y1 == y2): \n        return\n\n    line = shape_builder.createLine(x1, y1, x2, y2, style)\n    svg_document.addElement(line)\n\npatternPattern = [(0, 0), (0.3, 1), (0.6, 0)]\npatternScale = (3, 20)\n\n(pxScale, pyScale) = patternScale\npattern = [(x * pxScale, y * pyScale) for (x, y) in patternPattern]\n\ndef width(p):\n  return \\\n    max([x for (x, y) in p]) - \\\n    min([x for (x, y) in p])\n\ndef height(p):\n  return \\\n    max([y for (x, y) in p]) - \\\n    min([y for (x, y) in p])\n\ndef minus(p1, p2):\n  (x1, y1) = p1\n  (x2, y2) = p2\n\n  return (x2 - x1, y2 - y2)\n\ndef perpendicular(v1):\n  rotation = np.matrix(<sup><a href=\"#footnote_0_2404\" id=\"identifier_0_2404\" class=\"footnote-link footnote-identifier-link\" title=\"0, -1), (1, 0\">1<\/a><\/sup> )\n  return rotation * v1\n\nresolution = 1000.0\n\n# todo not really right...\npoints = int(round(resolution \/ width(pattern)))\nwidth = 100.0\nheight = 100.0\n\npatternIdx = 0\n\nactualPre = None\npathPre = None\nfor index in xrange(points):\n    gridPos = index * resolution \/ points\n\n    (patternX, patternY) = pattern[patternIdx]\n\n    fn = math.sin\n\n    pathX = gridPos\n    pathY = height + height \/ 4 * fn(gridPos \/ 20.0)\n\n    pathPoint = np.array( (pathX, pathY) )\n    if (pathPre <> None):\n      delta = pathPoint - pathPre\n\n    actualPoint = \\\n\t\t\t(patternX + pathX, \\\n \t\t\t patternY + pathY)\n\n    line(actualPre, actualPoint)\n\n    patternIdx = (patternIdx + 1) % len(pattern)\n\n    actualPre = actualPoint\n    pathPre = pathPoint\n    \ncomplete(svg_document, \"test.svg\")\nSVG(\"test.svg\")\n<\/pre>\n<ol class=\"footnotes\"><li id=\"footnote_0_2404\" class=\"footnote\">0, -1), (1, 0<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_0_2404\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>The following will render an SVG resembling a zig-zag sewing stitch, using python: import pysvg.structure import pysvg.builders import pysvg.text import math import numpy as np from IPython.core.display import SVG svg_document = pysvg.structure.Svg() shape_builder = pysvg.builders.ShapeBuilder() style = pysvg.builders.StyleBuilder() style.setStrokeWidth(2) style.setStroke(&#8216;black&#8217;) def complete(doc, file): doc.save(file) def line(a, b): if (a == None): return if (b == &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rendering-an-svg-containing-a-zigzag-stitch-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rendering an SVG containing a zigzag stitch in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[298,446,447],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2404"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2404"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}