TikZ:绘制包含给定点且边与坐标轴平行的最小矩形

TikZ:绘制包含给定点且边与坐标轴平行的最小矩形

我需要一个 TikZ 宏来围绕点列表绘制最小矩形,其边与坐标轴平行。在理想情况下,它可以处理大于 1 的任何点数,但如果它只处理 3 个点,我会很高兴。这些点通常采用以下形式

([yshift=10pt] A.north)

也就是说,它们通常是形状的锚点,可能会手动移动。

答案1

代码

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc,fit,positioning}

\begin{document}

\begin{tikzpicture}
    % some nodes
    \node[fill=red,circle,inner sep=2mm] (A) at (0,0) {};
    \node[fill=blue,circle,inner sep=4mm] (B) at (4,-1) {};
    \node[fill=violet,circle,inner sep=3mm] (C) at (1,3) {};

    % a node, relatively shifted
    \node[fill=orange,circle] (D) at ([yshift=5mm,xshift=15mm] A.north) {};

    % outer fit
    \node[thick,draw=red,fit=(A)(B)(C)] {};
    \node[thick,draw=blue,fit=(A)(B)(C),inner sep=0] {};

    % inner fit
    \node[thick,draw=red,fit=(A.east)(B.north west)(C.south),densely dotted] {};
    \node[thick,draw=blue,fit=(A.east)(B.north west)(C.south),inner sep=0,densely dotted] {};
    \node[thick,draw=violet,fit=(A.east)(B.north west)(C.south),inner sep=-3pt,densely dotted] {};

    % center fit with shifted node, as ([options] node) does not work
    \node[thick,draw=orange,fit=(A.center)(B.center)(D.center),densely dashed,inner sep=0] {};

    % same as above with calc's ($()+()$); note that you can't use coordinates as (x,y), you have
    %   to enclose the coordinates in braces: ({x,y})
    \node[thick,draw=orange,fit=(A.center)(B.center)($(A.north)+({1.5,0.5})$),densely dashed] {};
\end{tikzpicture}

\end{document}

输出

在此处输入图片描述

相关内容