在两个 TikZ 路径的交点处相减

在两个 TikZ 路径的交点处相减

我有两条路径,我想创建一条新路径,该路径从第一条路径开始,当两条路径相交时从路径一切换到路径二,然后当两条路径再次相交时再次遵循路径一。这是我所拥有的:

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{intersections}  
\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\minX{2}
    \pgfmathsetmacro\maxX{10}
    \pgfmathsetmacro\minY{4}
    \pgfmathsetmacro\maxY{10}
    \pgfmathsetmacro\CX{11}
    \pgfmathsetmacro\CY{7}
    \pgfmathsetmacro\CR{2}
    \pgfmathsetmacro\Roundness{0.2}
    \begin{scope} [local bounding box=BoxWest]
    \def\pathone{(.5*\maxX + .5*\minX,\maxY) 
        -- (-\Roundness + \maxX,\maxY)
        arc (90:0:\Roundness)
     -- (\maxX,\minY +\Roundness)
        arc (360:270:\Roundness)
        -- (.5*\maxX+ .5*\minX,\minY )}
        \path [name path=pathone, draw=green] \pathone;         
        \path [name path=pathtow, draw=blue](\CX,\CY)
        circle (\CR);   
         \path [name intersections={of = pathone and pathtwo}];
         \coordinate (A)  at (intersection-1);
         \coordinate (B)  at (intersection-2);
\end{scope}
\end{tikzpicture}
\end{document}

到目前为止,我已经能够创建两条路径并找到它们的相交点,但我不知道如何创建新路径,正如我所解释的那样。The Two paths

答案1

完成修订:fillbetween 库已实现所有这些功能,我在这个问题

\documentclass{article}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.15}
\usetikzlibrary{intersections,fillbetween} 
%\tikzset{fill between/optimize name intersections=true}
\begin{document}
It turns out that the \verb|tikzlibraryfillbetween| library has macros for these
cases.
Excerpts from the file \verb|tikzlibraryfillbetween.code.tex|:
\begin{verbatim}
\path[intersection segments={of=first and second, 
sequence=A0 -- B1 -- B3  A3[reverse] -- A1}];
\end{verbatim}
This seems to suggest that one should refer to paths as \verb|A| and \verb|B|.
However, from \verb|tikzlibraryfillbetween.code.tex| one can read off that the
relevant path names are \verb|A|, \verb|B|, \verb|L| and \verb|R|. From the
order it appears that \verb|A| and \verb|B| are more accurate if the first path
(\verb|A|) is above the second one (\verb|B|), whereas  \verb|L| and \verb|R|
apply if the first path (\verb|L|) is left of the second one (\verb|R|). One
also finds in \verb|tikzlibraryfillbetween.code.tex|:
\begin{verbatim}
% FIXME : this optimization needs much more work... I believe it
% would be stable enough, but it covers too few cases.
%/tikz/fill between/optimize name intersections=true,
\end{verbatim}
which is probably to be interpreted as that not everything works as it should.
Nonetheless, in the example at hand, it works fine.

\begin{tikzpicture}
    \pgfmathsetmacro\minX{2}
    \pgfmathsetmacro\maxX{10}
    \pgfmathsetmacro\minY{4}
    \pgfmathsetmacro\maxY{10}
    \pgfmathsetmacro\CX{11}
    \pgfmathsetmacro\CY{7}
    \pgfmathsetmacro\CR{2}
    \pgfmathsetmacro\Roundness{0.2}
    \def\pathone{(.5*\maxX + .5*\minX,\maxY) 
        -- (-\Roundness + \maxX,\maxY)
        arc (90:0:\Roundness)
     -- (\maxX,\minY +\Roundness)
        arc (360:270:\Roundness)
        -- (.5*\maxX+ .5*\minX,\minY )}
    \path[name path = pathone, draw,green] \pathone;
    \path[name path = pathtwo, draw,blue] (\CX,\CY) circle (\CR);
    \draw[red,very thick,rounded corners, intersection segments={of=pathone and pathtwo,
    sequence=L1--R2 L3}];
\end{tikzpicture}
\end{document}

enter image description here

无需剪辑,无需手工计算,仅此而已。

但请注意,这在原始版本中不起作用scope(但无论如何我不确定我是否理解它的用途)。

原始答案:这是一个作弊的解决方案,您甚至不需要计算交点,因为它\clip已经为您完成了工作。

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{intersections}  
\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\minX{2}
    \pgfmathsetmacro\maxX{10}
    \pgfmathsetmacro\minY{4}
    \pgfmathsetmacro\maxY{10}
    \pgfmathsetmacro\CX{11}
    \pgfmathsetmacro\CY{7}
    \pgfmathsetmacro\CR{2}
    \pgfmathsetmacro\Roundness{0.2}
    \begin{scope} [local bounding box=BoxWest]
    \def\pathone{(.5*\maxX + .5*\minX,\maxY) 
        -- (-\Roundness + \maxX,\maxY)
        arc (90:0:\Roundness)
     -- (\maxX,\minY +\Roundness)
        arc (360:270:\Roundness)
        -- (.5*\maxX+ .5*\minX,\minY )}
        \path [name path=pathone, draw=green] \pathone;         
        \path [name path=pathtwo, draw=blue](\CX,\CY)
        circle (\CR);   
         \path [name intersections={of =pathone and pathtwo}];
         \coordinate (A)  at (intersection-1);
         \coordinate (B)  at (intersection-2);
         \begin{scope}
         \clip (current bounding box.south west) rectangle (current bounding
         box.north east) --
          (\CX,\CY) circle (\CR);
         \draw [red] \pathone; 
         \end{scope}
         \begin{scope}
         \clip \pathone;
          (\CX,\CY) circle (\CR);
         \draw [red] (\CX,\CY) circle (\CR); 
         \end{scope}

\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

Heiko 的路径是通过将最后一个剪辑更改为

     \clip (current bounding box.south east) rectangle (current bounding
     box.north west) --\pathone;

enter image description here

答案2

可以轻松计算出圆弧的交点和角度。有一个直角三角形,其圆心、交点以及垂直线和水平线。垂直线的长度可以用毕达哥拉斯定理,直角三角形的一个角度(弧度角的一半)可以通过以下公式计算余弦定律

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}[x=10pt, y=10pt]
    \pgfmathsetmacro\minX{2}
    \pgfmathsetmacro\maxX{10}
    \pgfmathsetmacro\minY{4}
    \pgfmathsetmacro\maxY{10}
    \pgfmathsetmacro\CX{11}
    \pgfmathsetmacro\CY{7}
    \pgfmathsetmacro\CR{2}
    \pgfmathsetmacro\Roundness{0.2}

    \pgfmathsetmacro\OneWestX{.5*\maxX + .5*\minX}
    \pgfmathsetmacro\OneEastX{\maxX}
    \pgfmathsetmacro\OneNorthY{\maxY}
    \pgfmathsetmacro\OneSouthY{\minY}

    \pgfmathsetmacro\DiffX{\CX-\OneEastX}
    \pgfmathsetmacro\DiffY{sqrt(\CR * \CR - \DiffX * \DiffX)}
    \pgfmathsetmacro\DiffAngle{acos(\DiffX/\CR)}

    \draw[rounded corners]
      (\OneWestX, \OneNorthY)
      -- (\OneEastX, \OneNorthY)
      -- (\OneEastX, \CY + \DiffY) % North intersection point
      arc[
        start angle=180 - \DiffAngle,
        delta angle=-360+2*\DiffAngle,
        radius=\CR,
      ]
      -- (\OneEastX, \OneSouthY)
      -- (\OneWestX, \OneSouthY)
    ;
  \end{tikzpicture}
\end{document}

Result

根据您的需要调整线连接(从问题中看不清楚)。示例使用rounded corners让 TikZ 进行计算。另外,我使用了圆的右侧部分(也不清楚)。左侧部分更容易指定。

相关内容