使用 pgfplots 绘制三维平面图

使用 pgfplots 绘制三维平面图

我想绘制平面wx + 3y - z = 2并显示与参数化线的交点(x,y,z) = t(1,1,5) + (0,0,-2)。我不确定如何开始,\addplot3 coordinate我找到的命令在文档中看起来有点前途,但我并不真的想复制

答案1

我对 太乐观了\closedcycle。您可以使用 绘制此设置\addplot3。您需要通过分析确定交点(这在您的例子中很容易,它位于t=0)并按适当的顺序绘制拉伸。此顺序取决于视角。

\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[declare function={planez(\x,\y,\w)=\w*\x+3*\y-2;
linex(\t)=\t;liney(\t)=\t;linez(\t)=5*\t-2;
myw=-1;}]%<- put your favorite w here
\begin{axis}[xmin=-5,xmax=5,ymin=-5,ymax=5,zmin=-6,zmax=6,axis equal,hide axis]
 \addplot3[no marks,blue] coordinates {({linex(2)},{liney(2)},{linez(2)}) 
 ({linex(0)},{liney(0)},{linez(0)})};
 \addplot3[fill=gray,fill opacity=0.8] coordinates {(-2,-2,{planez(-2,-2,myw)})  
 (-2,2,{planez(-2,2,myw)}) 
  (2,2,{planez(2,2,myw)})  (2,-2,{planez(2,-2,myw)}) (-2,-2,{planez(-2,-2,myw)})};
 \addplot3[no marks,blue] coordinates {({linex(-2)},{liney(-2)},{linez(-2)}) 
 ({linex(0)},{liney(0)},{linez(0)})};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

pgfplots允许您使用 调整两个视角view={<el>}{<az>}。对于大多数用途来说,这已经足够了,但这不允许您旋转 z 轴。如果您还需要旋转 z 轴,您可能需要寻找包tikz-3dplot。在这里,您可以使用 安装“主”视图\tdplotsetmaincoords{<theta>}{<phi>},并通过切换到“旋转坐标”来旋转 z 轴\tdplotsetrotatedcoords{<alpha>}{<beta>}{<gamma>}。这是使用此包的一个快速示例。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{50}
\begin{tikzpicture}[tdplot_main_coords,declare function={planez(\x,\y,\w)=\w*\x+3*\y-2;
linex(\t)=\t;liney(\t)=\t;linez(\t)=5*\t-2;
myw=-1;}]%<- put your favorite w here
 \draw plot coordinates {({linex(2)},{liney(2)},{linez(2)}) 
 ({linex(0)},{liney(0)},{linez(0)})};
 \draw[fill=gray,fill opacity=0.8] 
  (-2,-2,{planez(-2,-2,myw)}) -- (-2,2,{planez(-2,2,myw)}) --
  (2,2,{planez(2,2,myw)}) --  (2,-2,{planez(2,-2,myw)})-- cycle;
 \draw plot coordinates {({linex(-2)},{liney(-2)},{linez(-2)}) 
 ({linex(0)},{liney(0)},{linez(0)})}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:编写一个解析计算交点的代码其实并不太难。在我看来,主要的障碍是执行向量运算。下面使用来自实验库 3dtools(将代码复制到同名文件中tikzlibrary3dtools.code.tex,并将其放在编译器可以找到的地方,例如示例文件所在的目录中。)然后使用以下方法获取(并标记)交点:

 \path[intersection of line and plane={line through=LT,line  direction=LD,plane through=PT,normal=PN}] node[circle,inner sep=1.5pt,fill]{}; 

代码及一些解释:

\documentclass[fleqn]{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{3dtools}
\def\ComputeIntersectionLinePlane{\pgfmathsetmacro{\myBN}{TD("(\pgfkeysvalueof{/tikz/intersection of line and
plane/line direction})o(\pgfkeysvalueof{/tikz/intersection of line and
plane/normal})")}%
\pgfmathtruncatemacro{\itest}{ifthenelse(abs(\myBN)<0.02,1,0)}% 0.02 ad hoc tolerance
\ifnum\itest=1\relax%
\message{The line and plane do not intersect in one point.^^J}%
\pgfmathsetmacro{\mynum}{0}%
\else
\pgfmathsetmacro{\mynum}{ifthenelse(\itest > 0,0,TD("(\pgfkeysvalueof{/tikz/intersection of line and
plane/plane through})-(\pgfkeysvalueof{/tikz/intersection of line and
plane/line through})o(\pgfkeysvalueof{/tikz/intersection of line and
plane/normal})")/\myBN)}%
\fi}
\tikzset{intersection of line and plane/.style={%
/utils/exec=\tikzset{intersection of line and plane/.cd,#1}%
\ComputeIntersectionLinePlane,3d parse={(\pgfkeysvalueof{/tikz/intersection of line and
plane/line through})+\mynum*(\pgfkeysvalueof{/tikz/intersection of line and
plane/line direction})}},%
intersection of line and plane/.cd,line through/.initial=,line
direction/.initial=,plane through/.initial=,normal/.initial=}
\tikzset{projection of point on plane/.style={%
/utils/exec=\tikzset{projection of point on plane/.cd,#1},%
intersection of line and plane={%
line through=\pgfkeysvalueof{/tikz/projection of point on plane/point},%
line direction=\pgfkeysvalueof{/tikz/projection of point on plane/normal},%
plane through=\pgfkeysvalueof{/tikz/projection of point on plane/plane through},%
normal=\pgfkeysvalueof{/tikz/projection of point on plane/normal}}},%
projection of point on plane/.cd,point/.initial=,%
plane through/.initial=,normal/.initial=}

\tikzset{3d parse/.style={/utils/exec=\tdparse<#1>,%
insert path={(\pgfmathresult)}},3d coordinate/.style args={#1=#2}{%
/utils/exec=\tdparse<#2>,%
insert path={(\pgfmathresult) coordinate #1}}}
\begin{document}
Consider a line with direction $\vec B$ running through $\vec A$,
$\vec r(t)~=~\vec A+t\,\vec B$,
and a plane going through $\vec C$ and normal $\vec n$. The intersection between
the line and the plane is obtained by demanding
\[ \left(\vec r(t_0)-\vec C\right) \perp \vec n\quad\Longleftrightarrow\quad
\left(\vec A+t_0\,\vec B-\vec C\right)\cdot\vec n~=~0\;,\]
which is the equivalent to 
\[ t_0\,\vec B\cdot \vec n~=~\left(\vec C-\vec A\right)\cdot \vec n\;.\]
Let us assume that $\vec B\cdot \vec n\ne 0$. Then 
\[t_0~=~\frac{\left(\vec C-\vec A\right)\cdot \vec n}{\vec B\cdot \vec n}
\quad\mbox{and}\quad \vec r_\mathrm{int}~=~\vec A+t_0\,\vec B\;.\] 
If $\vec B\cdot \vec n= 0$, the line is ``parallel'' to the plane, meaning
either the line is fully contained in the plane or does not intersect. In this
case, the code will spit out a message ``The line and plane do not intersect in
one point.''. Note also that this very computation also provides us with the
projection of a point on a plane. One only has to replace the line direction
$\vec B$ by the normal $\vec n$.

\centering
\tdplotsetmaincoords{70}{50}
\begin{tikzpicture}[tdplot_main_coords,scale=0.6,line cap=round,
declare function={planez(\x,\y,\w)=\w*\x+3*\y-2;
linex(\t)=\t;liney(\t)=\t;linez(\t)=5*\t-2;
myw=-1;}]%<- put your favorite w here
 \path[overlay] (0,0,-2) coordinate (LT) % line through
  (1,1,5) coordinate (LD) % line direction
  (0,0,-2) coordinate (PT) % plane through
  (myw,3,-1) coordinate (PN) % normal
  (-1,-1,1.5) coordinate (T) % test
  ({linex(2)},{liney(2)},{linez(2)})  coordinate (L2)
  ({linex(-2)},{liney(-2)},{linez(-2)})  coordinate (L-2); 
 \path[intersection of line and plane={line through=LT,line direction=LD,
 plane through=PT,normal=PN}] coordinate(I); 
 \draw[blue,thick](L2)--(I);
 \draw[fill=gray,fill opacity=0.8] 
  (-2,-2,{planez(-2,-2,myw)}) -- (-2,2,{planez(-2,2,myw)}) --
  (2,2,{planez(2,2,myw)}) --  (2,-2,{planez(2,-2,myw)})-- cycle;
 \draw[blue,thick] (L-2) --(I) 
    node[circle,fill,inner sep=1.5pt,label=above:$I$]{}; 
 \path[projection of point on plane={point=T,plane through=PT,normal=PN}] coordinate(P);    
 \draw[dashed] (T) node[circle,fill,inner sep=1.5pt,label=above:$T$]{} -- 
 (P) node[circle,fill,inner sep=1.5pt,label=above:$P$]{};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容