tikz 中的距离计算

tikz 中的距离计算

我想进行距离计算,仅标记满足给定条件的点。更具体地说,设 p=(1,1,1),我想仅在满足 |1-x| + |1-y| + |1-z| = 1 的点 (x,y,z) 处放置一个实心圆....在此处输入图片描述

我也可以手动操作,但如果有通用方法,最好使用。这是我正在处理的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot} 
\usepackage{amssymb}
\usepackage{xifthen}

\begin{document}


\tdplotsetmaincoords{60}{125}
\tdplotsetrotatedcoords{0}{0}{0} %<- rotate around (z,y,z)

\begin{figure}
\begin{tikzpicture}[scale=2.0]

\foreach \x in {0,1,2}
   \foreach \y in {0,1,2}
     \foreach \z in {0,1,2}{
       %#####################################################
       \ifthenelse{  \lengthtest{\x pt < 2pt}  }
       {
         % True
            \draw [black]   (\x,\y,\z) -- (\x+1,\y,\z);
       }
       {% False
       }
       %#####################################################
       \ifthenelse{  \lengthtest{\y pt < 2pt}  }
       {
         % True
            \draw [black]   (\x,\y,\z) -- (\x,\y+1,\z);
       }
       {% False
       }
       %#####################################################
       \ifthenelse{  \lengthtest{\z pt < 2pt}  }
       {
         % True
            \draw [black]   (\x,\y,\z) -- (\x,\y,\z+1);
       }
       {% False
       }
       \draw (\x,\y,\z) circle(2pt); 

}



 \filldraw[fill opacity=0.3, draw=gray, fill=gray!20]
 (0,0,1)--(2,0,1)--(2,2,1)--(0,2,1)--cycle;

 \fill[black] (1,1,2) circle(2pt);
 \fill[black] (2,1,1) circle(2pt);
 \fill[black] (1,1,0) circle(2pt);
 \fill[black] (0,1,1) circle(2pt);

 \filldraw[fill opacity=0.3, draw=gray, fill=gray!20]
 (0,1,2)--(2,1,2)--(2,1,0)--(0,1,0)--cycle;

 \fill[black] (1,0,1) circle(2pt);
 \fill[black] (1,2,1) circle(2pt);



  \node[below right] at (1,1,1){$p$};
   \fill[black] (1,1,1) circle(1pt);

\end{tikzpicture}

 \end{figure}


\end{document}   

答案1

您可以使用\pgfmathparse{int(abs(1-\x)+abs(1-\y)+abs(1-\z))}来计算距离,然后\ifnum\pgfmathresult=1测试距离1。同样,我会使用\ifnum\x<1etc 而不是\ifthenelse...仅仅因为它更短。

通过这些更改,你的 MWE 会变得略微更短:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{amssymb}
\usepackage{xifthen}

\begin{document}

\tdplotsetmaincoords{60}{125}
\tdplotsetrotatedcoords{0}{0}{0} %<- rotate around (z,y,z)

\begin{figure}
\begin{tikzpicture}[scale=2.0]

  \foreach \x in {0,1,2}
     \foreach \y in {0,1,2}
       \foreach \z in {0,1,2}{i
         \pgfmathparse{int(abs(1-\x)+abs(1-\y)+abs(1-\z))}
         \ifnum\pgfmathresult=1
           \fill[black] (\x,\y,\z) circle(2pt);
         \else
           \draw (\x,\y,\z) circle(2pt);
         \fi
         %#####################################################
         \ifnum\x<2
              \draw [black]   (\x,\y,\z) -- (\x+1,\y,\z);
         \fi
         %#####################################################
         \ifnum\y<2
              \draw [black]   (\x,\y,\z) -- (\x,\y+1,\z);
         \fi
         %#####################################################
         \ifnum\z<2
              \draw [black]   (\x,\y,\z) -- (\x,\y,\z+1);
         \fi
  }

  \filldraw[fill opacity=0.3, draw=gray, fill=gray!20]
  (0,0,1)--(2,0,1)--(2,2,1)--(0,2,1)--cycle;

  \filldraw[fill opacity=0.3, draw=gray, fill=gray!20]
  (0,1,2)--(2,1,2)--(2,1,0)--(0,1,0)--cycle;

  \node[below right] at (1,1,1){$p$};
  \fill[black] (1,1,1) circle(1pt);

\end{tikzpicture}

 \end{figure}

\end{document}

输出是一样的。

答案2

这个解决方案与 Andrew 的解决方案非常相似,我也用\ifnum它来代替,\ifthenelse并且计算也完全相同。

但我添加了两个变量\newcommand\MaxSize{CubeSize}\def\P{{P_x,P_y,P_z}}定义立方体的大小和点 P。还添加了一些代码,以便有更通用的方法来创建灰色平原或点 P。

\def\P{{1,1,1}}带有and 的示例MaxSize =1,...,3(参见第一个 for 循环): 在此处输入图片描述

距离计算:

\pgfmathparse{abs(\P[0]-\x)+abs(\P[2]-\y)+abs(\P[2]-\z)-1}
\ifcase\pgfmathresult 
        \draw[fill] (\x,\y,\z) circle(2pt); 
 \else 
         \draw (\x,\y,\z) circle(2pt);  
\fi
  • ifcase: 那些满足 => 的点 (x,y,z)|1-x| + |1-y| + |1-z| -1= 0填充
  • 否则:满足|1-x| + |1-y| + |1-z| -1~= 0=> 的那些点 (x,y,z) 未填充

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot} 
\usepackage{xifthen}

\newcommand\MaxSize{2}
\def\P{{1,1,1}}

\begin{document}
\tdplotsetmaincoords{60}{125}
\tdplotsetrotatedcoords{0}{0}{0}

\foreach \size in {1,...,3}{

\renewcommand\MaxSize{\size}

\begin{tikzpicture}[scale=1.5]
\foreach \x in {0,1,...,\MaxSize}
    \foreach \y in {0,1,...,\MaxSize}
        \foreach \z in {0,1,...,\MaxSize}{

         \ifnum\x<\MaxSize
            \draw [black]   (\x,\y,\z) -- (\x+1,\y,\z);
         \fi

        \ifnum\y<\MaxSize
            \draw [black]   (\x,\y,\z) -- (\x,\y+1,\z);
        \fi

        \ifnum\z<\MaxSize
            \draw [black]   (\x,\y,\z) -- (\x,\y,\z+1);
        \fi

\pgfmathparse{abs(\P[0]-\x)+abs(\P[2]-\y)+abs(\P[2]-\z)-1}
\ifcase\pgfmathresult 
        \draw[fill] (\x,\y,\z) circle(2pt); 
 \else 
         \draw (\x,\y,\z) circle(2pt);  
\fi

}

\filldraw[fill opacity=0.3, draw=red, fill=gray!20](0,0,\P[2])--(\MaxSize,0,\P[2])--(\MaxSize,\MaxSize,\P[2])--(0,\MaxSize,\P[2])--cycle;

\filldraw[fill opacity=0.3, draw=red, fill=gray!20] (0,\P[1],\MaxSize)--(\MaxSize,\P[1],\MaxSize)--(\MaxSize,\P[1],0)--(0,\P[1],0)--cycle;

%Point P
\node[below right,green] at (\P[0],\P[1],\P[2]){$p$};
\fill[green] (\P[0],\P[1],\P[2]) circle(1pt);
\end{tikzpicture}

}

\end{document}  

相关内容