如何使用 tikz 绘制一个矩形减去一个圆

如何使用 tikz 绘制一个矩形减去一个圆

我想要绘制如下的图像。 在此处输入图片描述

我可以使用一些技巧来绘制阴影:我先绘制一个矩形,然后在角落用白色填充一个圆圈。但我不知道如何使红色部分清晰。有没有更好的方法来绘制阴影?

在此处输入图片描述

如果可能的话,我还希望圆与圆角相切。

我尽力制作了这张图片,下面是我的代码。它太丑了,我不喜欢我覆盖矩形角的方式。

\begin{tikzpicture}
    [
    scale = 1,
    set/.style = {draw, rounded corners},
    shadow/.style = {rounded corners, fill = gray}
    ]
    
    \node[shadow, minimum width = 3.4cm, minimum height = 2.1cm] (sG) at(0.1, -0.062) {};
    \node[rounded corners, fill = white, minimum width = 3.4cm, minimum height = 2.1cm] (G) at(0, 0) {};
    \node[set, minimum width = 2.1cm, minimum height = 1.3cm] (S) at(0.652, -0.398) {};
    \node[circle, fill = gray, minimum size = 0.7cm] (sv1) at (1.38, -0.72) {};
    \node[circle, fill = white, minimum size = 0.7cm] (v1) at (1.48, -0.782) {};

    \draw (1.38, -0.72) circle (0.35);
    \draw[rounded corners] (-1.7, 1.05) rectangle (1.7, -1.05);
    \node[rectangle, fill = white, minimum size = 0.45cm] (sv2) at (1.59, -0.89) {};
    \node[rectangle, fill = white, minimum size = 0.481cm ] (sv3) at (1.51, -0.818) {};
    \draw[white, very thick] (1.278, -1.04) -- (1.38, -1.09);
\end{tikzpicture}

答案1

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}[very thick]
\newcommand{\myr}{0.5 cm}
\newcommand{\myx}{4 cm}
\newcommand{\myy}{3 cm}
\fill[white, drop shadow, save path=\pathA] {[rounded corners=\myr] (\myr,0) -- (0,0) -- (0,\myy) -- (\myx,\myy)} -- (\myx,\myr) arc[radius=\myr, start angle=0, end angle=270] -- cycle;
\clip[use path=\pathA];
\draw[line width/.expanded={2*\the\pgflinewidth}] [use path=\pathA];
\end{tikzpicture}
\end{document}

圆角矩形缺失圆角

相关内容