我正在尝试制作一个中心位于正方形内的圆形图像。但是,当圆形应该离开正方形时,它应该遵循正方形的形状。你有什么关于如何做到这一点的提示吗?

这是它的草图。

6

  • 3
    欢迎来到 TeX.SX!你尝试了什么?


    – 

  • 4
    看一下该功能clip


    – 

  • 2
    如果你需要给圆的剪切边缘上色,可以使用intersectionsTikZ 库


    – 

  • @samcarter_is_at_topanswers.xyz 遗憾的是,intersection segments的关键词pgfplots存在一些不准确之处。否则,这将是一个完美的选择。


    – 


  • 2
    @JasperHabicht 其实不需要交点。你可以画第二个正方形,然后用圆圈夹住它


    – 



最佳答案
2

samcarter 在评论中已经给出了很好的建议,但是如果你放大图纸,你会发现裁剪后的圆形和正方形不太吻合。但你可以通过裁剪掉这些问题来避免这种情况✂(请注意,由于裁剪,线条的粗细是正常粗细的一半):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \path[save path=\square, clip] (0,0) rectangle (4,4);
    \draw[use path=\square, thick];
    \path[save path=\circle, clip] (2.5,1.5) circle[radius=1.75cm];
    \draw[use path=\circle, red, thick];
    \draw[use path=\square, red, thick];
\end{tikzpicture}
\end{document}


如果你不喜欢剪辑,你也可以尝试这个spath3库:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{spath3, intersections}

\begin{document}
\begin{tikzpicture}

    \draw[spath/save=square] (0,0) rectangle (4,4);
    \path[spath/save=circle] (2.5,1.5) circle[radius=1.75cm];

    \tikzset{
        spath/split at intersections={square}{circle},
        spath/get components of={square}\segmentsSquare,
        spath/get components of={circle}\segmentsCircle
    }
    
    \draw[red, 
        spath/insert={\getComponentOf{\segmentsSquare}{4}, reverse},
        spath/append={\getComponentOf{\segmentsCircle}{4}},
        spath/append={\getComponentOf{\segmentsSquare}{2}, reverse},
        spath/append={\getComponentOf{\segmentsCircle}{2}},
    ] -- cycle;

\end{tikzpicture}
\end{document}

5

  • 1
    更多剪辑似乎是解决所有问题的办法:)


    – 

  • @samcarter_is_at_topanswers.xyz 把它剪掉……但我不太喜欢这个解决方案。如果能真正重新绘制路径就更好了。但这需要计算,而我现在太累了,无法做这样的事情……


    – 

  • 注意:使用此方法,只有边缘的内部可见……


    – 

  • 1
    @PaulGaborit 是的,这就是我所说的“常规厚度的一半”。我添加了另一个使用spath3库的解决方案,我更喜欢这个解决方案。


    – 

  • 要恢复(双倍)线宽,可以使用line width/.expanded={2*\the\pgflinewidth}@Qrrbrbirlbel 在此处的回答:和此处的用法:


    – 

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{intersections, ext.paths.arcto}
\begin{document}
\begin{tikzpicture}[very thick]
\draw[name path=square] (-1.2,-0.8) rectangle +(2,2);
\path[name path=circle] (0,0) circle[radius=1];
\draw[orange, fill=gray!20, name intersections={of=square and circle}, radius=1] 
  (intersection-1) arc to[large] (intersection-3) --
  (intersection-4) arc to (intersection-2) -- cycle;
\end{tikzpicture}
\end{document}

1

  • 1
    这比我的方法好多了。我经常忘记这些 Ti k Z 扩展……


    –