下图是我想要的球体的样子:
代码:
\documentclass{article}
\usepackage[a4paper,top=3cm,bottom=3cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{patchplots}
\usepgfplotslibrary{colormaps}
\usepackage{graphicx}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shadings}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\begin{document}
\begin{tikzpicture}[scale=2]
\pgfmathsetmacro{\myangle}{90}
\begin{axis}[axis equal image, axis lines=center, ticks=none, view/h=120, view/v=20, enlarge x limits={abs=2pt}, enlarge y limits={abs=2pt}, enlarge z limits={abs=2pt}]
% axis line style={latex-latex}
\addplot3+[domain=2:3.65, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({-x},{0},{0});
\addplot3+[domain=2:2.4, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({0},{-x},{0});
\addplot3+[domain=2:2.25, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({0},{0},{-x});
%sphere
\addplot3[surf, shader=interp, point meta={x + 2*y + 3*z}, colormap={custom}{rgb255=(0,0,0)rgb255=(250,250,250)}, z buffer = sort, samples = 50,
variable = \u, variable y = \v, domain = 0:180, y domain = 0:360] ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {2*cos(v)});
%viviani window underlay edge
\addplot3+[domain=0:4*pi, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
%equator cylinder
\addplot3+[domain=0:2*pi, samples=30, samples y=0, no marks, smooth, solid, black, thin]({1-cos(deg(x))},{sin(deg(x))},{0});
%hidden axes
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({x},{0},{0});
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({0},{x},{0});
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({0},{0},{x});
%viviani cylinder parts
% \addplot3[surf, shader=interp, point meta={4*x + 8*y - 2}, colormap={custom}{rgb255=(50,50,50)rgb255=(255,255,255)}, z buffer = sort, samples = 35,
% variable = \u, variable y = \v, domain = -180:180, y domain = -2:0, ] ({1-cos(u)}, {-sin(u)}, {min(-sqrt(4 - 2*x),v)});
% \addplot3[surf, shader=interp, point meta={4*x + 8*y - 2}, colormap={custom}{rgb255=(50,50,50)rgb255=(255,255,255)}, z buffer = sort, samples = 35,
% variable = \u, variable y = \v, domain = -180:180, y domain = 0:2, ] ({1-cos(u)}, {-sin(u)}, {max(sqrt(4 - 2*x),v)});
%viviani window overlay edge
\addplot3+[domain=-pi/3:2*pi/3, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
\addplot3+[domain=5*pi/3:8*pi/3, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
\addplot3+[domain=2.34375:4.5325, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
%axis extensions
\addplot3+[domain=2:3.625, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({x},{0},{0});
\addplot3+[domain=2:2.4, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({0},{x},{0});
\addplot3+[domain=2:2.25, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({0},{0},{x});
\end{axis}\end{tikzpicture}
\end{document}
但是当我取消注释圆柱体部分时,却发生了以下情况:
我不知道这里发生了什么,我希望这一切都停止。
我唯一能观察到的是,我已经多次使用了点元,并且当我更改任一图的点元时,它会改变两个图上的阴影。
答案1
问题是球体的变化小于圆柱体的变化。这为什么重要?在调查阶段pgfplots
检查所有图的最小值和最大值,然后得出一个公式,使得最大点元对应于色谱的上端,最小点元对应于色谱的下端。因此,您需要做的就是通过将球体的点元乘以某个数字(和一个常数)来修改球体的点元。
\documentclass{article}
\usepackage[a4paper,top=3cm,bottom=3cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}[scale=2]
\pgfmathsetmacro{\myangle}{90}
\begin{axis}[axis equal image, axis lines=center, ticks=none, view/h=120, view/v=20, enlarge x limits={abs=2pt}, enlarge y limits={abs=2pt}, enlarge z limits={abs=2pt}]
% axis line style={latex-latex}
\addplot3+[domain=2:3.65, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({-x},{0},{0});
\addplot3+[domain=2:2.4, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({0},{-x},{0});
\addplot3+[domain=2:2.25, samples=5, samples y=0, line width= 0.31pt, no marks, smooth, solid, black]({0},{0},{-x});
%sphere
\addplot3[surf, shader=interp, point meta={(x + 2*y + 3*z)*4-2}, colormap={custom}{rgb255=(0,0,0)rgb255=(250,250,250)}, z buffer = sort, samples = 50,
variable = \u, variable y = \v, domain = 0:180, y domain = 0:360] ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {2*cos(v)});
%viviani window underlay edge
\addplot3+[domain=0:4*pi, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
%equator cylinder
\addplot3+[domain=0:2*pi, samples=30, samples y=0, no marks, smooth, solid, black, thin]({1-cos(deg(x))},{sin(deg(x))},{0});
%hidden axes
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({x},{0},{0});
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({0},{x},{0});
\addplot3+[domain=-2:2, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, densely dotted, black]({0},{0},{x});
%viviani cylinder parts
\addplot3[surf, shader=interp, point meta={4*x + 8*y - 2}, colormap={custom}{rgb255=(50,50,50)rgb255=(255,255,255)}, z buffer = sort, samples = 35,
variable = \u, variable y = \v, domain = -180:180, y domain = -2:0, ] ({1-cos(u)}, {-sin(u)}, {min(-sqrt(4 - 2*x),v)});
\addplot3[surf, shader=interp, point meta={4*x + 8*y - 2}, colormap={custom}{rgb255=(50,50,50)rgb255=(255,255,255)}, z buffer = sort, samples = 35,
variable = \u, variable y = \v, domain = -180:180, y domain = 0:2, ] ({1-cos(u)}, {-sin(u)}, {max(sqrt(4 - 2*x),v)});
%viviani window overlay edge
\addplot3+[domain=-pi/3:2*pi/3, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
\addplot3+[domain=5*pi/3:8*pi/3, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
\addplot3+[domain=2.34375:4.5325, samples=50, samples y=0, no marks, smooth, solid, black, thin]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)});
%axis extensions
\addplot3+[domain=2:3.625, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({x},{0},{0});
\addplot3+[domain=2:2.4, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({0},{x},{0});
\addplot3+[domain=2:2.25, samples=5, samples y=0, line width= 0.5pt, no marks, smooth, solid, black]({0},{0},{x});
\end{axis}\end{tikzpicture}
\end{document}