loopspace / spath3

TikZ/PGF package for manipulating soft paths, includes the knots and calligraphy TikZ libraries.
16 stars 2 forks source link

gap insert incorrect shape #19

Closed asepsiswu closed 1 year ago

asepsiswu commented 2 years ago

I try to insert the gap by split at in with develop vesion spath3 2.6 and encounter some problems:

  1. path with cycle will connect to the original path start point
  2. path with rectangle will rotate Screenshot_2022-07-16_22-09-54

In addtion, I could not generate spath3.pdf with command pdflatex spath3_code.dtx with error:

! Package doc Error: Checksum not passed (9067<>10133).
\documentclass{article}
%\url{https://tex.stackexchange.com/q/639719/86}
\usepackage{tikz}
\usetikzlibrary{ spath3 } % spath3  2.6 

\begin{document}
% Method 1: Splice into gaps
\begin{tikzpicture}[
  original path/.style={
    ultra thick,
    red,
    spath/save=#1
  }
]

\draw[spath/save=triangle,black,yshift=1cm] (0,0) -- ++(1,1) --(120:1) --cycle;
\path[original path=line] (0,0) -- (3,0);
\tikzset{
  spath/split at={line}{0.8},
  spath/split at={line}{0.6/.8*1/2},
  spath/split at={line}{0.4/.6*1/3},
  spath/split at={line}{0.2/.4*1/4},
    spath/insert gaps after components={line}{10pt},
  spath/join components with={line}{triangle},
}
\draw[spath/use=line,blue];

\path[original path=line2] (0,-1) -- (3,-1);
\draw[spath/save=rect,yshift=-3cm] (0,0) rectangle (1,1);
\tikzset{
  spath/split at={line2}{0.8},
  spath/split at={line2}{0.6/.8*1/2},
  spath/split at={line2}{0.4/.6*1/3},
  spath/split at={line2}{0.2/.4*1/4},
    spath/insert gaps after components={line2}{10pt},
  spath/join components with={line2}{rect},
}
\draw[spath/use=line2,red];

\end{tikzpicture}
\end{document}
loopspace commented 1 year ago

I haven't had a chance to test your code yet, but just reading through it then I have a couple of remarks and questions that will help when I do get a chance to look at it.

Firstly, ignore the checksum error. It's a technical thing for packages that I only pay attention to when uploading to CTAN. So just tell TeX to ignore the error and it should complete the document. That said, I suspect you may want to compile spath3.tex for the documentation for this library.

With the splicing, what are you trying to achieve? You are trying to insert a closed path (the triangle) into a gap so that the triangle spans the gap. But the way that the code works is to transform the path so that the initial point is at the start of the gap and the final post is at the end. For the triangle, the start and end points are the same so it is not possible to carry out this transformation. It does a closest possible transformation, but a full span isn't possible.

It works with the rectangle because a rectangular path contains an extra move at the end meaning that the initial and final points are not at the same place so the transformation is possible.

So, is the path with rectangles what you were expecting to see? And would you like the triangle version to be similar?

asepsiswu commented 1 year ago

I thought the insert path filled in the gap would be the expected shape, such as rectangle with any desired rotation.

As you mentioned above, "the initial point is at the start of the gap and the final post is at the end." It is not the way I had Imagined.

I will look at the code.

Thank you for your detailed reply!

loopspace commented 1 year ago

I've now had a chance to run your code.

As I suspected, the issue with the triangles is that the initial and final points are the same, so the triangle path can't be made to span the gap. In those circumstances, the code tries to do something sensible, which is to put the triangle path into the gap to start at the right place and be oriented the right way, but it can't be made to end in the right place so it doesn't rescale the path as it would with a more usual path (such as a semi-circle).

However, there's a further wrinkle. When the triangle is inserted into the gap, it is welded on to the previous segment of the path. Because of how path closing works, this means that the closing segment now goes to the start of the segment instead of where the triangle started.

But even with that, your triangle version shouldn't have looked the way it did. Each closing segment should have joined to the start of its segment, not the start of the entire path. I've now fixed that in https://github.com/loopspace/spath3/commit/793f321e227f71c5a86a709e0da4bd89f6101bd3

loopspace commented 1 year ago

Here's a way to put triangles in the gaps.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/639719/86}
\usepackage{tikz}
\usetikzlibrary{ spath3 } % spath3  2.6 

\begin{document}
% Method 1: Splice into gaps
\begin{tikzpicture}[
  original path/.style={
    ultra thick,
    red,
    spath/save=#1
  }
]

% Save the basic triangle shape
\draw[spath/save=pre triangle,black,yshift=1cm] (0,0) -- ++(1,1) --(120:1) --cycle;

% Append a starting and ending point to the triangle, the start point is the first corner; the end point is part way along one side (see the coordinates section of the documentation for details on how this part works).
\path[spath/save=triangle] (spath cs:pre triangle 0) [spath/use=pre triangle] (spath cs:pre triangle .5);

\path[original path=line] (0,0) -- (3,0);
\tikzset{
  spath/split at={line}{0.8},
  spath/split at={line}{0.6/.8*1/2},
  spath/split at={line}{0.4/.6*1/3},
  spath/split at={line}{0.2/.4*1/4},
    spath/insert gaps after components={line}{10pt},
        spath/join components with={line}{triangle},
        spath/show=line
}

\draw[spath/use=line,blue];

\path[original path=line2] (0,-1) -- (3,-1);
\draw[spath/save=rect,yshift=-3cm] (0,0) rectangle (1,1);
\tikzset{
  spath/split at={line2}{0.8},
  spath/split at={line2}{0.6/.8*1/2},
  spath/split at={line2}{0.4/.6*1/3},
  spath/split at={line2}{0.2/.4*1/4},
    spath/insert gaps after components={line2}{10pt},
  spath/join components with={line2}{rect},
}
\draw[spath/use=line2,red];

\end{tikzpicture}
\end{document}
asepsiswu commented 1 year ago

Something I want to do is to fill gap with some shapes. the start point and the end point might not connect to the corner of the shape, but some points in the egde of shape.

loopspace commented 1 year ago

That's what this line does:

\path[spath/save=triangle] (spath cs:pre triangle 0) [spath/use=pre triangle] (spath cs:pre triangle .5);

Having defined the shape originally as pre triangle, we define a new shape which consists of the pre triangle shape with a move at the start and end to particular locations on the triangle shape. The (spath cs: pre triangle ...) puts a location at a particular point on the triangle, so by changing the 0 and the .5 you would change the points on the triangle which were used to fit the triangle into the gap.

asepsiswu commented 1 year ago

Thanks you very much again. Now I understand the spath cs <name> <ratio> to make shape rotate.