Rendering an SVG containing a zigzag stitch in Python

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('black')

def complete(doc, file):
		doc.save(file)

def line(a, b):
    if (a == None):
        return
    if (b == None):
        return
    (x1, y1) = a
    (x2, y2) = b

    # allows for textures that return where they start
    if (x1 == x2 and y1 == y2): 
        return

    line = shape_builder.createLine(x1, y1, x2, y2, style)
    svg_document.addElement(line)

patternPattern = [(0, 0), (0.3, 1), (0.6, 0)]
patternScale = (3, 20)

(pxScale, pyScale) = patternScale
pattern = [(x * pxScale, y * pyScale) for (x, y) in patternPattern]

def width(p):
  return \
    max([x for (x, y) in p]) - \
    min([x for (x, y) in p])

def height(p):
  return \
    max([y for (x, y) in p]) - \
    min([y for (x, y) in p])

def minus(p1, p2):
  (x1, y1) = p1
  (x2, y2) = p2

  return (x2 - x1, y2 - y2)

def perpendicular(v1):
  rotation = np.matrix(1 )
  return rotation * v1

resolution = 1000.0

# todo not really right...
points = int(round(resolution / width(pattern)))
width = 100.0
height = 100.0

patternIdx = 0

actualPre = None
pathPre = None
for index in xrange(points):
    gridPos = index * resolution / points

    (patternX, patternY) = pattern[patternIdx]

    fn = math.sin

    pathX = gridPos
    pathY = height + height / 4 * fn(gridPos / 20.0)

    pathPoint = np.array( (pathX, pathY) )
    if (pathPre <> None):
      delta = pathPoint - pathPre

    actualPoint = \
			(patternX + pathX, \
 			 patternY + pathY)

    line(actualPre, actualPoint)

    patternIdx = (patternIdx + 1) % len(pattern)

    actualPre = actualPoint
    pathPre = pathPoint
    
complete(svg_document, "test.svg")
SVG("test.svg")
  1. 0, -1), (1, 0 []

Leave a Reply

Your email address will not be published.