TransWikia.com

TikZ smooth plot, retrieves points along the path in parametric way

TeX - LaTeX Asked on February 1, 2021

Suppose I have a certain curve, whose "control" points I specify using pre-set coordinates.

documentclass{standalone}
usepackage{tikz}
usetikzlibrary{shapes,calc,arrows,decorations.pathmorphing,intersections}

begin{document}
begin{tikzpicture}
    coordinate (A) at (0,0);
    coordinate (B) at (42,0);
    coordinate (C) at (0,42);
    draw plot[smooth] coordinates{(A) (B) (C)};
end{tikzpicture}
end{document}

Now we can imagine that this is some trajectory and some "object" has been at coordinate (A) at time t = 0 and has arrived at (C) at time t = 1. I would like to know if it is possible to specify a certain spot along this trajectory, saying t = 0.5 for example, that would effectively mean "halfway through" (A) and (B)? I need this functionality so that I can specify a "naturally-looking" shape, and only then draw things on it.

One Answer

This can be done using path decorations:

documentclass[tikz,border=2mm]{standalone}
usetikzlibrary{decorations.markings}

begin{document}
begin{tikzpicture}[
    define coord/.style 2 args={
      decoration={
        markings,
        mark=at position #2 with {coordinate (#1);},
      },
      postaction=decorate,
    }
  ]
  coordinate (A) at (0,0);
  coordinate (B) at (5,0);
  coordinate (C) at (0,5);
  draw[blue, define coord={P}{0.2}, define coord={I}{0.5},
              define coord={Q}{0.75}]
    plot[smooth] coordinates { (A) (B) (C) };
  fill[red] (P) circle[radius=2pt] node[black, below] {At time $0.2$};
  node[red!80!black, pin=20:$I$] at (I) {middle};
  node[above right] at (Q) {At $3/4$ of the curve};
end{tikzpicture}
end{document}

enter image description here

As you noted, in a given path, the style must be used with increasing coordinates, otherwise the points aren't placed as expected.

Answered by frougon on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP