我正在尝试绘制两个圆圈,它们是两个平面和一个球体的交点。
\documentclass[tikz,border=1mm, 12 pt]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds}
\makeatletter
% retrieves the 3D coordinates
\def\RawCoord(#1){\csname tikz@dcl@coord@#1\endcsname}%
\def\scalprod#1=#2.#3;{%
\edef\coordA{\RawCoord#2}%
\edef\coordB{\RawCoord#3}%
\pgfmathsetmacro\pgfutil@tmpa{scalarproduct({\coordA},{\coordB})}
\edef#1{\pgfutil@tmpa}}%
\makeatother
\newcommand{\spaux}[6]{(#1)*(#4)+(#2)*(#5)+(#3)*(#6)}
\pgfmathdeclarefunction{scalarproduct}{2}{% scalar product of two 3-vectors
\begingroup%
\pgfmathparse{\spaux#1#2}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\begin{document}
\tdplotsetmaincoords{65}{70}
\begin{tikzpicture}[scale=1/7,tdplot_main_coords,declare function={a=16;b=16;h=28;r=sqrt(a*a + b*b)/2;
alpha1(\th,\ph,\b)=\ph+asin(cot(\th)*tan(\b));%
alpha2(\th,\ph,\b)=-180+\ph-asin(cot(\th)*tan(\b));%
beta1(\th,\ph,\a)=90+atan(cot(\th)/sin(\a-\ph));%
beta2(\th,\ph,\a)=270+atan(cot(\th)/sin(\a-\ph));%
}]
\path (0,0,0) coordinate (O)
(-a/2,-b/2,0) coordinate (A)
(a/2,-b/2,0) coordinate (C)
at (a/2,b/2,0) coordinate (B)
(-a/2,b/2,0) coordinate (D)
(0,0,h) coordinate (O')
(-a/2,-b/2,h) coordinate (A')
(a/2,-b/2,h) coordinate (C')
at (a/2,b/2,h) coordinate (B')
(-a/2,b/2,h) coordinate (D')
(0,0,16/a) coordinate(Z);
\begin{scope}[canvas is xy plane at z={0}]
\draw[dashed] (O) circle (r);
\scalprod\myz=(O).(Z); % z component of T
\pgfmathsetmacro{\myel}{atan(-1*\myz/r)}
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\end{scope}
\begin{scope}[canvas is xy plane at z={h}]
\scalprod\myz=(O).(Z); % z component of T
\pgfmathsetmacro{\myel}{atan(-1*\myz/r)}
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\end{scope}
\coordinate (I) at ($ (O) !0.5!(O') $);
\begin{scope}[tdplot_screen_coords, on background layer]
\draw[thick] (I) circle ({sqrt(a*a + b*b + h*h)/2});
\end{scope}
\foreach \v/\position in {A/below,B/below,C/below,D/below,A'/above,B'/above,C'/above,D'/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
}
\foreach \X in {A,B,C,D} \draw[dashed] (\X) -- (\X');
\draw[dashed] (A) --(B) -- (C) -- (D) -- cycle
(A') --(B') -- (C')-- (D') -- cycle
;
\end{tikzpicture}
\end{document}
两个圆的线不正确。我怎样才能使它们正确?
答案1
平面的高度分别为 或h/2
,而不是 的 z 分量O
(为 0)。代入后,可得到
\documentclass[tikz,border=1mm, 12 pt]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds}
\makeatletter
% retrieves the 3D coordinates
\def\RawCoord(#1){\csname tikz@dcl@coord@#1\endcsname}%
\def\scalprod#1=#2.#3;{%
\edef\coordA{\RawCoord#2}%
\edef\coordB{\RawCoord#3}%
\pgfmathsetmacro\pgfutil@tmpa{scalarproduct({\coordA},{\coordB})}
\edef#1{\pgfutil@tmpa}}%
\makeatother
\newcommand{\spaux}[6]{(#1)*(#4)+(#2)*(#5)+(#3)*(#6)}
\pgfmathdeclarefunction{scalarproduct}{2}{% scalar product of two 3-vectors
\begingroup%
\pgfmathparse{\spaux#1#2}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\begin{document}
\tdplotsetmaincoords{65}{70}
\begin{tikzpicture}[scale=1/7,tdplot_main_coords,
declare function={a=16;b=16;h=28;r=sqrt(a*a + b*b)/2;
R=sqrt(a*a + b*b + h*h)/2;
alpha1(\th,\ph,\b)=\ph+asin(cot(\th)*tan(\b));%
alpha2(\th,\ph,\b)=-180+\ph-asin(cot(\th)*tan(\b));%
beta1(\th,\ph,\a)=90+atan(cot(\th)/sin(\a-\ph));%
beta2(\th,\ph,\a)=270+atan(cot(\th)/sin(\a-\ph));%
}]
\path (0,0,0) coordinate (O)
(-a/2,-b/2,0) coordinate (A)
(a/2,-b/2,0) coordinate (C)
at (a/2,b/2,0) coordinate (B)
(-a/2,b/2,0) coordinate (D)
(0,0,h) coordinate (O')
(-a/2,-b/2,h) coordinate (A')
(a/2,-b/2,h) coordinate (C')
at (a/2,b/2,h) coordinate (B')
(-a/2,b/2,h) coordinate (D')
(0,0,16/a) coordinate(Z);
\begin{scope}[canvas is xy plane at z={0}]
\draw[dashed] (O) circle (r);
\pgfmathsetmacro{\myel}{atan(-h/2/r)}
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\end{scope}
\begin{scope}[canvas is xy plane at z={h}]
\pgfmathsetmacro{\myel}{atan(h/2/r)}
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\end{scope}
\coordinate (I) at ($ (O) !0.5!(O') $);
\begin{scope}[tdplot_screen_coords, on background layer]
\draw[thick] (I) circle (R);
\end{scope}
\foreach \v/\position in {A/below,B/below,C/below,D/below,A'/above,B'/above,C'/above,D'/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
}
\foreach \X in {A,B,C,D} \draw[dashed] (\X) -- (\X');
\draw[dashed] (A) --(B) -- (C) -- (D) -- cycle
(A') --(B') -- (C')-- (D') -- cycle
;
\end{tikzpicture}
\end{document}
这当然适用于任何视角。(和的解析表达式alpha1
是alpha2
基于有限拉伸而非完全拉伸可见的假设而得出的。必须检查情况是否如此,因此有这些\ifnum
陈述。)
\documentclass[tikz,border=1mm, 12 pt]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds}
\makeatletter
% retrieves the 3D coordinates
\def\RawCoord(#1){\csname tikz@dcl@coord@#1\endcsname}%
\def\scalprod#1=#2.#3;{%
\edef\coordA{\RawCoord#2}%
\edef\coordB{\RawCoord#3}%
\pgfmathsetmacro\pgfutil@tmpa{scalarproduct({\coordA},{\coordB})}
\edef#1{\pgfutil@tmpa}}%
\makeatother
\newcommand{\spaux}[6]{(#1)*(#4)+(#2)*(#5)+(#3)*(#6)}
\pgfmathdeclarefunction{scalarproduct}{2}{% scalar product of two 3-vectors
\begingroup%
\pgfmathparse{\spaux#1#2}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\begin{document}
\foreach \Angle in {24,26,...,84,82,80,...,26}
{\tdplotsetmaincoords{\Angle}{70}
\begin{tikzpicture}[scale=1/7,tdplot_main_coords,
declare function={a=16;b=16;h=28;r=sqrt(a*a + b*b)/2;
R=sqrt(a*a + b*b + h*h)/2;
alpha1(\th,\ph,\b)=\ph+asin(cot(\th)*tan(\b));%
alpha2(\th,\ph,\b)=-180+\ph-asin(cot(\th)*tan(\b));%
beta1(\th,\ph,\a)=90+atan(cot(\th)/sin(\a-\ph));%
beta2(\th,\ph,\a)=270+atan(cot(\th)/sin(\a-\ph));%
}]
\path[use as bounding box,tdplot_screen_coords]
(-1.2*R,-1.2*R+h/2) rectangle (1.2*R,1.2*R+h/2);
\path (0,0,0) coordinate (O)
(-a/2,-b/2,0) coordinate (A)
(a/2,-b/2,0) coordinate (C)
at (a/2,b/2,0) coordinate (B)
(-a/2,b/2,0) coordinate (D)
(0,0,h) coordinate (O')
(-a/2,-b/2,h) coordinate (A')
(a/2,-b/2,h) coordinate (C')
at (a/2,b/2,h) coordinate (B')
(-a/2,b/2,h) coordinate (D')
(0,0,16/a) coordinate(Z);
\begin{scope}[canvas is xy plane at z={0}]
\draw[dashed] (O) circle (r);
\pgfmathsetmacro{\myel}{atan(-h/2/r)}
\pgfmathparse{int(ifthenelse(abs(cot(\tdplotmaintheta)*tan(\myel))<1,1,0))}
\ifnum\pgfmathresult>0
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\fi
\end{scope}
\begin{scope}[canvas is xy plane at z={h}]
\draw[dashed] (O') circle (r);
\pgfmathsetmacro{\myel}{atan(h/2/r)}
\pgfmathparse{int(ifthenelse(abs(cot(\tdplotmaintheta)*tan(\myel))<1,1,0))}
\ifnum\pgfmathresult>0
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{\myel})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{\myel})}:r) ;
\else
\draw[thick] (O') circle (r);
\fi
\end{scope}
\coordinate (I) at ($ (O) !0.5!(O') $);
\begin{scope}[tdplot_screen_coords, on background layer]
\draw[thick] (I) node{$\theta=\tdplotmaintheta^\circ,\varphi=\tdplotmainphi^\circ$} circle (R);
\end{scope}
\foreach \v/\position in {A/below,B/below,C/below,D/below,A'/above,B'/above,C'/above,D'/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
}
\foreach \X in {A,B,C,D} \draw[dashed] (\X) -- (\X');
\draw[dashed] (A) --(B) -- (C) -- (D) -- cycle
(A') --(B') -- (C')-- (D') -- cycle
;
\end{tikzpicture}}
\end{document}