LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Is there a way to "trace" a path and define a coordinate?

Information and discussion about graphics, figures & tables in LaTeX documents.
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Is there a way to "trace" a path and define a coordinate?

I've been wondering this for a while, and I haven't found anything so I figured it wasn't possible.

But why not ask anyway, just to be sure?

I have a rather complicated set of equations that is bogging down even Mathematica. But, you know, if I could just start at a point, move down this path, then along another, I'll get to the point I want to define. TikZ could do the numerical work by following simple path instructions and not have to solve the equations that are messing me up.

So, to keep this simple (and to give an explicit example):
Start at the coordinate (A)=(3,0). Move along the arc[start angle=0, end angle=90, radius=2], then move along the arc[start angle=-45, end angle=-180, radius=3.2]. Define coordinate (B).

Can this be done?

Thanks!

-Dan

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Stefan Kottwitz
Posts: 10050
Joined: Mon Mar 10, 2008 9:44 pm

Is there a way to "trace" a path and define a coordinate?

Hi Dan,

you could put coordinate[at end, ...] in the path, or use \pgfpointlineattime from Points Traveling along Lines and Curves.

Stefan

FlorElbert
Posts: 2
Joined: Thu Sep 14, 2023 1:18 pm

Is there a way to "trace" a path and define a coordinate?

Yes, you can achieve this in TikZ by using the calc library to calculate the positions of points along the specified paths without explicitly solving the equations. Here's how you can define the coordinate (B) based on your description:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
% Define starting point A
\coordinate (A) at (3,0);

% Draw the first arc
\draw ($(A) + (0:2)$) arc (0:90:2);

% Calculate the end point of the first arc
\coordinate (temp) at ($(A) + (45:2)$);

% Draw the second arc
\draw ($(temp) + (-45:3.2)$) arc (-45:-180:3.2);

% Define point B
\coordinate (B) at ($(temp) + (-180:3.2)$);

% Just for visualization, draw lines from A to B
\draw (A) -- (B);

% Mark points A and B
\filldraw (A) circle (2pt) node[below] {A};
\filldraw (B) circle (2pt) node[below] {B};
\end{tikzpicture}
\end{document}


In this code, we start by defining the coordinate (A). Then, we draw the first arc using 'arc' command and calculate the endpoint of this arc using the 'calc' library. Next, we draw the second arc from the endpoint of the first arc to its endpoint, and finally, we define the coordinate (B) at the endpoint of the second arc. The result is a TikZ diagram with points A and B connected by the specified arcs.

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Is there a way to "trace" a path and define a coordinate?

Stefan Kottwitz wrote:Hi Dan,

you could put coordinate[at end, ...] in the path, or use \pgfpointlineattime from Points Traveling along Lines and Curves.

Stefan

Thank you. I had seen the PGF/TikZ package section, but as I might be using irregular paths, this isn't going to work that well.

I tried
\coordinate[at end] (A) (0,0) arc(0:90:2);

for example. Clearly my guess at the syntax is wrong, and I can't find anything on the net about it.

(If I do something, more correctly to my mind, like
\coordinate[at end] (A) at (0,0) arc(0:90:2);
it defines (A) to be the origin.)

How do you structure the command? Or is there yet another online manual that I might have missed that would address something like this?

Thanks again!

-Dan

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Is there a way to "trace" a path and define a coordinate?

FlorElbert wrote:Yes, you can achieve this in TikZ by using the calc library to calculate the positions of points along the specified paths without explicitly solving the equations. Here's how you can define the coordinate (B) based on your description:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
% Define starting point A
\coordinate (A) at (3,0);

% Draw the first arc
\draw ($(A) + (0:2)$) arc (0:90:2);

% Calculate the end point of the first arc
\coordinate (temp) at ($(A) + (45:2)$);

% Draw the second arc
\draw ($(temp) + (-45:3.2)$) arc (-45:-180:3.2);

% Define point B
\coordinate (B) at ($(temp) + (-180:3.2)$);

% Just for visualization, draw lines from A to B
\draw (A) -- (B);

% Mark points A and B
\filldraw (A) circle (2pt) node[below] {A};
\filldraw (B) circle (2pt) node[below] {B};
\end{tikzpicture}
\end{document}


In this code, we start by defining the coordinate (A). Then, we draw the first arc using 'arc' command and calculate the endpoint of this arc using the 'calc' library. Next, we draw the second arc from the endpoint of the first arc to its endpoint, and finally, we define the coordinate (B) at the endpoint of the second arc. The result is a TikZ diagram with points A and B connected by the specified arcs.

Thanks for the input. I honestly hadn't considered defining the intermediate points (so thank you for that!) but it won't do my any good, in general. I was able to find a solution via geometry in the case I mentioned I was working on (not the simple example I posted in the OP) but I would like to find a way to do this if I can't calculate the intermediate points. For example, if I wanted to trace along an ellipse or something.

Thanks again!

-Dan

rais
Posts: 390
Joined: Sun Nov 16, 2014 8:51 pm

Is there a way to "trace" a path and define a coordinate?

Hi Dan,
topsquark wrote:I tried
\coordinate[at end] (A) (0,0) arc(0:90:2);

well, \coordinate is basically a node without a size of its own, hence the arc will be put into nothing, so to speak.
For getting points along the arc (or whatever curve you have), you can use options like at start', near end', etc, or its pos=..' counterpart :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (P) at (-1,-1);
\draw[black!30] (0,0) arc(0:90:2)
coordinate[at start] (A)% or pos=0
coordinate[pos=0.25] (B)% or near start
coordinate[midway] (C)% or pos=0.5
coordinate[pos=0.75] (D)% or near end
coordinate[at end] (E)% or pos=1
arc(30:120:1)
coordinate[midway] (F)
coordinate[at end] (G);
\draw[blue] (P) node[left]{P} -- (A);
\draw[blue!50] (P) -- (B);
\draw[blue!10] (P) -- (C);
\draw[black!10] (P) -- (D);
\draw[red!10] (P) -- (E);
\draw[red!50] (P) -- (F);
\draw[red] (P) -- (G);
\end{tikzpicture}
\end{document}


If you just want to get to the end of your arc, you don't even need at end' or pos=1':
\begin{tikzpicture}
\coordinate (P) at (-1,-1);
\draw[black!30] (0,0) arc(0:90:2)
coordinate (Y)
arc(30:120:1)
coordinate (Z)
;
\draw[blue] (P) node[left]{P} -- (Y);
\draw[red] (P) -- (Z);
\end{tikzpicture}


See also in pgfmanual section Placing Nodes on a Line or Curve Explicitly'.

KR
Rainer

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Is there a way to "trace" a path and define a coordinate?

rais wrote:Hi Dan,
topsquark wrote:I tried
\coordinate[at end] (A) (0,0) arc(0:90:2);

well, \coordinate is basically a node without a size of its own, hence the arc will be put into nothing, so to speak.
For getting points along the arc (or whatever curve you have), you can use options like at start', near end', etc, or its pos=..' counterpart :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (P) at (-1,-1);
\draw[black!30] (0,0) arc(0:90:2)
coordinate[at start] (A)% or pos=0
coordinate[pos=0.25] (B)% or near start
coordinate[midway] (C)% or pos=0.5
coordinate[pos=0.75] (D)% or near end
coordinate[at end] (E)% or pos=1
arc(30:120:1)
coordinate[midway] (F)
coordinate[at end] (G);
\draw[blue] (P) node[left]{P} -- (A);
\draw[blue!50] (P) -- (B);
\draw[blue!10] (P) -- (C);
\draw[black!10] (P) -- (D);
\draw[red!10] (P) -- (E);
\draw[red!50] (P) -- (F);
\draw[red] (P) -- (G);
\end{tikzpicture}
\end{document}


If you just want to get to the end of your arc, you don't even need at end' or pos=1':
\begin{tikzpicture}
\coordinate (P) at (-1,-1);
\draw[black!30] (0,0) arc(0:90:2)
coordinate (Y)
arc(30:120:1)
coordinate (Z)
;
\draw[blue] (P) node[left]{P} -- (Y);
\draw[red] (P) -- (Z);
\end{tikzpicture}


See also in pgfmanual section Placing Nodes on a Line or Curve Explicitly'.

KR
Rainer

Ah! I see now. Honestly, I should have seen that. Thank you!

And thank you for the pdf of the manual. The search system for the pdf works incredibly better than the online version.

Thanks again to Stefan as well!

-Dan

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Is there a way to "trace" a path and define a coordinate?

OMG! I was able to use that to not only eliminate the 14 path intersections I was taking, but I was also able to collapse some two pages of code into 10 lines. And not only is the code more efficient, it runs much much faster.

Thank you again!

-Dan

Just for the record:
\documentclass{article}

\usepackage{tikz}

\begin{document}

\def\R{2}
\begin{tikzpicture}

\foreach \t in {0,...,35}{
\begin{scope}[rotate=\t*10]

\foreach \s in {1,...,14}{
\path (0:2*\R) arc(0:{(\s-1)*10}:\R) coordinate[at end] (A);
\path (0:2*\R) arc(0:{\s*10}:\R) coordinate[at end] (B);
\path[transform canvas] (B);
\pgfgetlastxy{\xcoord}{\ycoord}
\pgfmathsetmacro{\Xcoord}{scalar{\xcoord}/28.452756}
\pgfmathsetmacro{\Ycoord}{scalar{\ycoord}/28.452756}
}

\end{scope}
}

\end{tikzpicture}

\end{document}


FlorElbert
Posts: 2
Joined: Thu Sep 14, 2023 1:18 pm