如何在 tikzpicture 中添加具有旋转坐标的球体?

如何在 tikzpicture 中添加具有旋转坐标的球体?

我想将球体放在旋转坐标的原点,但最后球体会变形。有什么办法可以解决这个问题?这是背页代码

在此处输入图片描述

主文件(您还需要 3dplot.sty)在这里:

\documentclass{article}
%%%<
\usepackage{verbatim,amsmath}
%%%>

\begin{comment}
:Title:  The 3dplot package
:Zip: 3dplot.sty

The 3dplot package provides straightforward ways to define three-dimensional coordinate frames through which to plot in TikZ.  The user can specify the orientation of the main coordinate frame, and use standard TikZ commands and coordinates to render their tikzfigure.  A secondary coordinate frame is provided to allow rotations and translations with respect to the main coordinate frame.

The 3dplot.sty package can be found at `<http://www.heinjd.com/dev/latex/3dplot/3dplot.sty>`_.

Documentation for the 3dplot.sty package can be found at `<http://www.heinjd.com/dev/latex/3dplot/3dplot_documentation.pdf>`_.
\end{comment}

\usepackage{tikz}   %TikZ is required for this to work.  Make sure this exists before the next line

\usepackage{3dplot} %requires 3dplot.sty to be in same directory, or in your LaTeX installation

\usepackage[active,tightpage]{preview}  %generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%synatax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{110}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{.8}
\pgfmathsetmacro{\thetavec}{30}
\pgfmathsetmacro{\phivec}{60}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
%coordinate transformation provided by 3dplot
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);

%determine a coordinate (P) using (r,\theta,\phi) coordinates.  This command
%also determines (Pxy), (Pxz), and (Pyz): the xy-, xz-, and yz-projections
%of the point (P).
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}

%draw figure contents
%--------------------

%draw the main coordinate system axes
\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P) 
\draw[-stealth,color=red] (O) -- (P);

%draw projection on xy plane, and a connecting line
\draw[dashed, color=red] (O) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\theta$}


%set the rotated coordinate system so the x'-y' plane lies within the
%"theta plane" of the main coordinate system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw Center of mass
\draw  (0,-0.2,-0.1) node[anchor=south]{$\boldsymbol{R}_{CM}$};

%draw normal vector
\draw  (0,0.35,0.6) node[anchor=south]{$\boldsymbol{n}$};

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.5}{0}{\thetavec}{anchor=south west}{$\psi$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);
\shadedraw [ball color=red] (0,0,0) circle(0.1);
\end{tikzpicture}
\end{document}

答案1

为此,tikz-3dplot附带tdplot_screen_coords,即

\shadedraw [ball color=red,tdplot_screen_coords] (0,0,0) circle(0.1);

成功了。由于您的 MWE 中有一个拼写错误(包是tikz-3dplot,而不是3dplot),为了可重复性,我添加了一个完整的 MWE:

\documentclass{article}
%%%<
\usepackage{verbatim,amsmath}
%%%>

\begin{comment}
:Title:  The 3dplot package
:Zip: 3dplot.sty

The 3dplot package provides straightforward ways to define three-dimensional coordinate frames through which to plot in TikZ.  The user can specify the orientation of the main coordinate frame, and use standard TikZ commands and coordinates to render their tikzfigure.  A secondary coordinate frame is provided to allow rotations and translations with respect to the main coordinate frame.

The 3dplot.sty package can be found at `<http://www.heinjd.com/dev/latex/3dplot/3dplot.sty>`_.

Documentation for the 3dplot.sty package can be found at `<http://www.heinjd.com/dev/latex/3dplot/3dplot_documentation.pdf>`_.
\end{comment}

\usepackage{tikz}   %TikZ is required for this to work.  Make sure this exists before the next line

\usepackage{tikz-3dplot} %requires 3dplot.sty to be in same directory, or in your LaTeX installation

\usepackage[active,tightpage]{preview}  %generates a tightly fitting border around the work
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%synatax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{110}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{.8}
\pgfmathsetmacro{\thetavec}{30}
\pgfmathsetmacro{\phivec}{60}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
%coordinate transformation provided by 3dplot
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);

%determine a coordinate (P) using (r,\theta,\phi) coordinates.  This command
%also determines (Pxy), (Pxz), and (Pyz): the xy-, xz-, and yz-projections
%of the point (P).
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}

%draw figure contents
%--------------------

%draw the main coordinate system axes
\draw[thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P) 
\draw[-stealth,color=red] (O) -- (P);

%draw projection on xy plane, and a connecting line
\draw[dashed, color=red] (O) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\theta$}


%set the rotated coordinate system so the x'-y' plane lies within the
%"theta plane" of the main coordinate system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw Center of mass
\draw  (0,-0.2,-0.1) node[anchor=south]{$\boldsymbol{R}_{CM}$};

%draw normal vector
\draw  (0,0.35,0.6) node[anchor=south]{$\boldsymbol{n}$};

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.5}{0}{\thetavec}{anchor=south west}{$\psi$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);
\shadedraw [ball color=red,tdplot_screen_coords] (0,0,0) circle(0.1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,如果按照正确的顺序绘制这些物体并且不让线条进入球体内部,事情就会变得更引人注目。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz-3dplot} %requires 3dplot.sty to be in same directory, or in your LaTeX installation

\begin{document}

%Angle Definitions
%-----------------

%set the plot display orientation
%synatax: \tdplotsetdisplay{\theta_d}{\phi_d}
\tdplotsetmaincoords{60}{110}

%define polar coordinates for some vector
%TODO: look into using 3d spherical coordinate system
\pgfmathsetmacro{\rvec}{.8}
\pgfmathsetmacro{\thetavec}{30}
\pgfmathsetmacro{\phivec}{60}

%start tikz picture, and use the tdplot_main_coords style to implement the display 
%coordinate transformation provided by 3dplot
\begin{tikzpicture}[scale=5,tdplot_main_coords]

%set up some coordinates 
%-----------------------
\coordinate (O) at (0,0,0);

%determine a coordinate (P) using (r,\theta,\phi) coordinates.  This command
%also determines (Pxy), (Pxz), and (Pyz): the xy-, xz-, and yz-projections
%of the point (P).
%syntax: \tdplotsetcoord{Coordinate name without parentheses}{r}{\theta}{\phi}
\tdplotsetcoord{P}{\rvec}{\thetavec}{\phivec}
\tdplotsetcoord{P'}{0.1}{\thetavec}{\phivec}

%draw figure contents
%--------------------

\shadedraw [ball color=red,tdplot_screen_coords] (0,0,0) circle(0.1);

%draw the main coordinate system axes
\draw[thick,->] (0.1,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[thick,->] (0,0.1,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[thick,->] (0,0,0.1) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from origin to point (P) 
\draw[-stealth,color=red] (P') -- (P);

%draw projection on xy plane, and a connecting line
\draw[dashed, color=red] (P'xy) -- (Pxy);
\draw[dashed, color=red] (P) -- (Pxy);

%draw the angle \phi, and label it
%syntax: \tdplotdrawarc[coordinate frame, draw options]{center point}{r}{angle}{label options}{label}
\tdplotdrawarc{(O)}{0.2}{0}{\phivec}{anchor=north}{$\theta$}


%set the rotated coordinate system so the x'-y' plane lies within the
%"theta plane" of the main coordinate system
%syntax: \tdplotsetthetaplanecoords{\phi}
\tdplotsetthetaplanecoords{\phivec}

%draw Center of mass
\draw  (0,-0.2,-0.1) node[anchor=south]{$\boldsymbol{R}_{CM}$};

%draw normal vector
\draw  (0,0.35,0.6) node[anchor=south]{$\boldsymbol{n}$};

%draw theta arc and label, using rotated coordinate system
\tdplotdrawarc[tdplot_rotated_coords]{(0,0,0)}{0.5}{0}{\thetavec}{anchor=south west}{$\psi$}

%draw some dashed arcs, demonstrating direct arc drawing
\draw[dashed,tdplot_rotated_coords] (\rvec,0,0) arc (0:90:\rvec);
\draw[dashed] (\rvec,0,0) arc (0:90:\rvec);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容