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

Add class for 2d vectors #19

Closed mwiebusch78 closed 4 years ago

mwiebusch78 commented 4 years ago

For a long time I have used asymptote for publication quality figures but I've recently started looking for Python alternative. It's great to see that pyx has all the essential functionality to serve as a replacement. Well done, guys!

However, one feature that I miss is the existence of a class for 2d vectors which integrates with the methods for path construction. In asymptote I normally start by defining a number of relevant points on the canvas and then build the paths from these points. And something like

point1 = point0 + voffset*UP + hoffset*LEFT

is simply more readable than

point1x = point0x - hoffset
point1y = point0y + voffset

I'm wondering if you would consider adding a 2D vector class and changing the path construction methods so that the accept 2D vectors rather than x and y coordinates.

I've had a look at the code and I think I understand it well enough to try and implement it myself but since it's a big change to the interface I first wanted to check if you'd be willing to make such a change.

gertingold commented 4 years ago

For the time being, you could use numpy arrays as demonstrated in the following example:

import numpy as np
from pyx import canvas, deco, path

c = canvas.canvas()
point_1 = np.array([1, 2])
vektor = np.array([4, -3])
point_2 = point_1 + vektor
c.stroke(path.line(*point_1, *point_2), [deco.earrow])
c.writePDFfile()

This approach works nicely in 3d as well which I use frequently together with the projectors provided by graph.graphxyz to produce schematic 3d drawings.

mwiebusch78 commented 4 years ago

Ah, I didn't know the tuple unpacking also works for numpy arrays. (And, to be honest, it didn't occur to me at all to use tuple unpacking.) I think there is still a case for a dedicated pyx vector class because lengths in pyx are not floats but instances of unit.length. For example, you may want to get a point on a path via the at method (which gives you a tuple of unit.length) and then do further calculations with it.

Would you still consider changing the path construction methods to accept vector arguments or do you think the tuple unpacking trick should be normal way to do things?

gertingold commented 4 years ago

This is for the developers to decide. I am just a happy long-time user of PyX :-)

Concerning unpacking of tuples: I started using this only recently and found it quite useful with PyX at least in some situations.

wobsta commented 4 years ago

This was an addition in Python 3.5, PEP 448. I remember quite well that I was very happy when learning about this feature. It is very handy for PyX. I also discussed it with Jörg and we kind of decided, that (a) it was good to not have used tuples in path elements constructors (and elsewhere) originally and (b) that due to this new feature we have even less reason to change anything in PyX. :-)