# -----------------------------------------------------------------------
# simpleplot.py | www.sqrt.ch
# Writes values for a function into a simple path in a standalone SVG
# Piecewise linear approximation
# -----------------------------------------------------------------------

import math

def path(a, b):
    return f'\t\t{a:.5f} {b:.15f}'

# Note that most SVG Renderer take the Origin to the upper LEFT
# and have the positive y-axis DOWNWARDS!

print('<?xml version="1.0" encoding="UTF-8"?>\n'
      '<svg shape-rendering="geometricPrecision" version="1.1" viewBox="-5 -5 10 10" xmlns="http://www.w3.org/2000/svg">\n'
      '\t<path d="M ')

x0 = -5  # first x-value
x1 = 5  # last x-value
step = 0.005  # delta x
p = .05  # stroke-width
up = int((x1 - x0) / step + 1)  # upper count Limit

for i in range(0, up):
    x = x0 + i * step
    y = 2 * math.sin(5 * x)
    print(path(x, y))

print(f'\t\t" fill="none" stroke="#000" stroke-width="{p}"/>\n'
      '\t<desc>www.sqrt.ch</desc>\n</svg>')

# Example for typing in the terminal:
# python simpleplot.py > plot.svg
#
# For reduction of the size (yet not the precision) you may use "Scour".
