pyx-project / pyx

Repository of PyX, a Python package for the creation of PostScript, PDF, and SVG files.
https://pyx-project.org/
GNU General Public License v2.0
109 stars 18 forks source link

[Question] How to create paths with a dynamic number of elements? #38

Open Makogan opened 2 years ago

Makogan commented 2 years ago

The docs very skilfully show how to add a path that is known at "compile time", but since the input to path.path it's not immediately obvious how to create a path from say, a list of points.

I would like to suggest adding an example like that, or editing the current example, to show how one might deal with user generated points, or with points read from a file.

joerg-lehmann commented 1 year ago

If I understand you correctly, you are looking for something along the lines of the following code example:

from math import cos, sin, pi
from pyx import *

N = 9
r = 1

p = path.path(path.moveto(r, 0))

for i in range(N):
    phi = (i+1)/N*2*pi
    p.append(path.lineto(r*cos(phi), r*sin(phi)))

c = canvas.canvas()
c.stroke(p)
c.writePDFfile()

This shows how to append path elements to an existing path as needed.