latex3 / latex3

The expl3 (LaTeX3) Development Repository
https://latex-project.org/latex3.html
LaTeX Project Public License v1.3c
1.89k stars 183 forks source link

New l3draw functions to extract x/y/z-coordinate of a point #1491

Open muzimuzhi opened 6 months ago

muzimuzhi commented 6 months ago

Discussed in https://github.com/latex3/latex3/issues/1488#issuecomment-1974746683.

pgf provides \pgfextractx and \pgfextracty with syntax \pgfextractx{<dimension>}{<point>}, see https://tikz.dev/base-points#sec-101.6.

One possibility: \draw_point_extract_x:Nn <dim var> { <point> }.

If needed \draw_point_extract_last_x:N which extracts the x-coordinate of last updated point can also be provided. pgf provides \pgfgetlastxy{<macro for x>}{<macro for y>}.

josephwright commented 6 months ago

See comments in the sources: the are deliberately not includes pending solid use cases. I suspect that 'local' auxiliaries are likey the base plan.

muzimuzhi commented 6 months ago

https://github.com/latex3/latex3/blob/2ea12efc6a163bf1ae3fd0ad6d05dabc3701cf2a/l3experimental/l3draw/l3draw-points.dtx#L67-L78

muzimuzhi commented 6 months ago
%   \item \cs{pgfextractx}, \cs{pgfextracty}: Available by applying
%     \cs{use_i:nn}/\cs{use_ii:nn} or similar to the \texttt{e}-type
%     expansion of a point expression.

But public \draw_point_... functions expand points to points themselves, aka two-element comma-separated lists. Only the internal \__draw_point_process:nn expands a point to {<x-coord>}{<y-coord>}, which then can be passed to \use_i:nn/\use_ii:nn to extract x/y-coordinate.

That's why @jasperhabicht uses \clist_item:en in the demonstration in https://github.com/latex3/latex3/issues/1488#issuecomment-1974746683.

josephwright commented 6 months ago

@muzimuzhi Yes, my code comment is inaccurate but the general idea stands. In pgf you need the 'extract' commands as the information is internal. In l3draw we can do everything using an fp expression by expansion, so can easily create an 'extract the value' command. But more widely, there are two things. First, as we can use tuples throughout, I'm not sure point extraction is very useful - we don't need to access just-x or just-y. Second, as this is really a general tuple feature, I suspect it should be handled within l3fp, something like \fp_eval:n { select(2, (<tuple>) } -> y-value.

josephwright commented 6 months ago

(Whole reason this code is in l3experimental is of course that this sort of stuff needs exploring.)

josephwright commented 6 months ago

BTW, points don't have a z-co-ordinate ;)

car222222 commented 6 months ago

But this is l3draw, so surely it is in 3D?? :-)

jasperhabicht commented 6 months ago

I think there can be situations where you need just the x or the y part of a point coordinate, for example if you want to calculate trigonometric stuff where you can't always use tuples (for example when using atand(), you need both values separately ). Or you might want to use the y part of one point coordinate and the x* part of another to create a new point coordinate.

I agree that extracting a value from a tuple should rather be a feature of l3fp. However, currently there is no such a feature and it would most probably be based on a loop. So, in the end there are still arguments against and in favor of implementing this in l3draw directly.


*) See https://tex.stackexchange.com/a/711906/47927 for a use case. I think with only tuples, it would be difficult to calculate the angle, but I might be wrong.