latex3 icon indicating copy to clipboard operation
latex3 copied to clipboard

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

Open muzimuzhi opened this issue 1 year ago • 8 comments
trafficstars

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>}.

muzimuzhi avatar Mar 02 '24 10:03 muzimuzhi

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

josephwright avatar Mar 02 '24 10:03 josephwright

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

muzimuzhi avatar Mar 02 '24 10:03 muzimuzhi

%   \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.

muzimuzhi avatar Mar 02 '24 10:03 muzimuzhi

@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 avatar Mar 02 '24 11:03 josephwright

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

josephwright avatar Mar 02 '24 11:03 josephwright

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

josephwright avatar Mar 02 '24 11:03 josephwright

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

car222222 avatar Mar 02 '24 11:03 car222222

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.

jasperhabicht avatar Mar 02 '24 12:03 jasperhabicht