I want to create a TikZ diagram showing a unit sphere intersected by a plane. I'd like to highlight the spherical cap (the portion of the sphere on one side of the plane).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a TikZ diagram of a spherical cap requires careful handling of 3D projections and filling. The tikz-3dplot package is highly recommended for this as it greatly simplifies the calculation of 3D coordinates and projections.
Here's a detailed solution using tikz-3dplot:
latex
\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot} % Required for 3D plotting
\usepackage{amsmath} % For math environments if needed, but not strictly for this diagram
\begin{document}
% Define the viewing angles for the 3D plot
% \tdplotsetmaincoords{elevation}{azimuth}
% elevation: angle from the positive Z-axis down to the viewing vector (0-180)
% azimuth: angle from the positive X-axis around the Z-axis (0-360)
\tdplotsetmaincoords{60}{110} % A good isometric-like view
\begin{tikzpicture}[
tdplot_main_coords, % Activate the 3D coordinate system
% --- Custom Styles ---
sphere/.style={ball color=blue!10, opacity=0.6}, % Style for the main sphere
cap/.style={fill=blue!50, opacity=0.7}, % Style for the highlighted cap
plane/.style={thick, gray, opacity=0.7}, % Style for the intersecting plane
axis/.style={->, thick, gray!70}, % Style for the axes
label/.style={font=\small}, % Style for text labels
dimline/.style={dashed, thin, gray!60} % Style for dimension lines
]
% --- Parameters ---
\pgfmathsetmacro{\R}{3} % Sphere radius
\pgfmathsetmacro{\h}{1.5} % Height of the plane from the origin (z-coordinate)
% Make sure h < R for a valid intersection
\pgfmathsetmacro{\rc}{sqrt(\R*\R - \h*\h)} % Radius of the intersection circle
\pgfmathsetmacro{\phiCap}{acos(\h/\R)} % Angle from the Z-axis (North Pole) to the intersection circle
% --- Drawing the Sphere and Cap ---
% 1. Draw the full sphere (lightly shaded background)
% tdplotdrawsphere takes: {center x}{center y}{center z}{radius}
\tdplotdrawsphere[sphere]{0}{0}{0}{\R}
% 2. Draw the spherical cap (highlighted part)
% tdplotfillrmax fills a spherical region:
% {center x}{center y}{center z}{radius}{start_theta}{end_theta}{start_phi}{end_phi}
% - theta: polar angle from the +Z axis (0=North Pole, 180=South Pole)
% - phi: azimuthal angle around the Z-axis (0=along +X axis, 90=along +Y axis)
% We want the cap from the North Pole (theta=0) down to the plane (theta=\phiCap),
% and for all azimuthal angles (phi=0 to 360).
\tdplotfillrmax[cap]{0}{0}{0}{\R}{0}{\phiCap}{0}{360}
% --- Drawing the Intersection Circle (Plane) ---
% The intersection circle is at z=h and has radius rc.
% We need to draw it split into a hidden (dashed) and visible (solid) part based on the view.
% Calculate the start and end angles for the visible and hidden arcs of the circle.
% These angles are in the object's XY plane, where 0 is along +X, 90 along +Y.
% The visibility depends on the `tdplotmainphi` (azimuth of the viewer).
% The part of the circle facing the viewer will be visible.
% The arc from (\tdplotmainphi - 90) to (\tdplotmainphi + 90) is generally visible.
% The arc from (\tdplotmainphi + 90) to (\tdplotmainphi + 270) is generally hidden.
% Adjust angles to be within 0-360 range using fmod.
\pgfmathsetmacro{\startVis}{fmod(\tdplotmainphi - 90 + 360, 360)}
\pgfmathsetmacro{\endVis}{fmod(\tdplotmainphi + 90 + 360, 360)}
\pgfmathsetmacro{\startHid}{fmod(\tdplotmainphi + 90 + 360, 360)}
\pgfmathsetmacro{\endHid}{fmod(\tdplotmainphi + 270 + 360, 360)}
% Set the drawing plane for the circle: it's parallel to the XY-plane (theta=90)
\tdplotsetthetaplanecoords{90}
% Draw the hidden part of the intersection circle
% tdplotdrawarc: {center x}{center y}{center z}{radius}{start_angle}{end_angle}
\tdplotdrawarc[plane, dashed]{0}{0}{\h}{\rc}{\startHid}{\endHid}
% Draw the visible part of the intersection circle
\tdplotdrawarc[plane]{0}{0}{\h}{\rc}{\startVis}{\endVis}
% --- Adding Labels and Axes ---
% Origin
\node[label, below left] at (0,0,0) {O};
% Z-axis
\draw[axis] (0,0,0) -- (0,0,\R+0.7) node[label, right] {$Z$};
% Height 'h' dimension line
\draw[dimline] (0,0,0) -- (0,0,\h);
\node[label, below right] at (0,0,\h/2) {$h$};
% Sphere radius 'R' from origin to the pole
\draw[dimline] (0,0,0) -- (0,0,\R);
\node[label, above] at (0,0,\R) {$(0,0,R)$};
% Radius 'rc' of the intersection circle
\draw[dimline] (0,0,\h) -- (\rc,0,\h);
\node[label, below] at (\rc/2,0,\h) {$r_c$};
% Sphere radius 'R' from origin to a point on the intersection circle
\draw[dimline] (0,0,0) -- (\rc,0,\h);
\node[label, below, xshift=-5pt, yshift=-5pt] at (\rc/2,0,\h/2) {$R$};
% A label for the spherical cap
\node[label, above, xshift=-15pt] at (0,0,\R*0.75) {Spherical Cap};
\end{tikzpicture}
\end{document}
Explanation:
\usepackage{tikz-3dplot}: This package is essential for drawing in 3D.
\tdplotsetmaincoords{60}{110}: This command sets the viewpoint for the 3D scene.
60: The elevation angle (from the positive Z-axis down to the viewer).
110: The azimuth angle (from the positive X-axis, rotated counter-clockwise around the Z-axis, to the viewer). These values create a common isometric-like perspective.
tdplot_main_coords option: This option, when used with \begin{tikzpicture}, tells TikZ to use the 3D coordinate system defined by \tdplotsetmaincoords.
Parameters: \R (sphere radius), \h (plane height), \rc (intersection circle radius calculated by Pythagorean theorem), and \phiCap (the polar angle from the Z-axis to the intersection plane, calculated using acos).
Drawing the Sphere (\tdplotdrawsphere):
\tdplotdrawsphere[sphere]{0}{0}{0}{\R} draws a sphere centered at (0,0,0) with radius \R. The sphere style (defined earlier) provides the ball color shading.
Drawing the Spherical Cap (\tdplotfillrmax):
This is the core command for highlighting the cap.
\tdplotfillrmax[cap]{0}{0}{0}{\R}{0}{\phiCap}{0}{360} fills a region on the sphere.
{0}{0}{0}{\R}: Center (0,0,0), radius \R.
{0}{\phiCap}: This defines the polar angle range. 0 is the North Pole (along +Z). \phiCap is the angle calculated to the cutting plane. So, it fills the surface from the pole down to the plane.
{0}{360}: This defines the azimuthal angle range, covering the full circle around the Z-axis.
Drawing the Intersection Circle (Plane):
This circle is on the plane z=h. Its radius is \rc.
Visibility Calculation: To make the diagram look realistic, the circle is split into a hidden (dashed) and a visible (solid) part. The start and end angles for these arcs (\startVis, \endVis, etc.) are calculated based on the (the viewer's azimuth angle).
This setup provides a clear and accurate representation of a unit sphere intersected by a plane, with the spherical cap visually highlighted.
User
The sphere looks like a circle. How can i fix it?
Assistant
You're absolutely right! A common pitfall with 3D diagrams is that if the lighting/shading or the viewing angle isn't right, a sphere can indeed look like a flat circle in 2D projection.
The tikz-3dplot package's ball color style property is key here, as it automatically applies shading based on a simulated light source. Let's refine the appearance:
Stronger ball color: blue!10 is very light. A darker base color or a more saturated color will make the shading more apparent.
Increased Opacity: opacity=0.6 makes the sphere quite transparent, which can reduce the perceived depth. Making it more opaque will make the shading more pronounced.
Adjusting light source (Optional but good): You can explicitly set the light source for ball color to control where the "sun" is, which affects the highlights and shadows.
Viewing Angle: While the current \tdplotsetmaincoords{60}{110} is generally good, sometimes trying slightly different angles (e.g., 50 or 70 for elevation, or varying azimuth) can also enhance the 3D perception.
Here's the modified code focusing on these improvements:
latex
\tdplotmainphi
\tdplotsetthetaplanecoords{90}: This command is crucial. It temporarily redefines the drawing plane so that \tdplotdrawarc can draw circles in a plane parallel to the main XY-plane (theta=90 corresponds to the XY-plane in spherical coordinates). The arcs are then drawn at the specified z coordinate (\h).
\tdplotdrawarc is then used twice: once for the dashed (hidden) part and once for the solid (visible) part.
Labels and Axes: Standard TikZ \draw and \node commands are used to add axes, origin, and dimension labels for clarity. dimline style is used for dashed lines indicating dimensions like h and r_c.
\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{amsmath}
\begin{document}
% Define the viewing angles for the 3D plot
% Increased elevation slightly to see more of the "top" and less flat side
% Adjusted azimuth slightly for a different perspective
\tdplotsetmaincoords{65}{120} % Good for showing curvature
\begin{tikzpicture}[
tdplot_main_coords,
% --- Custom Styles ---
% Increased ball color intensity and opacity for the sphere
% Added a light source for more defined shading
sphere/.style={ball color={blue!30!white, light source={(45,45)}}, opacity=0.9},
% Adjusted cap style for better visibility against the more opaque sphere
cap/.style={fill=blue!60!white, opacity=0.9},
plane/.style={thick, gray, opacity=0.7},
axis/.style={->, thick, gray!70},
label/.style={font=\small},
dimline/.style={dashed, thin, gray!60}
]
% --- Parameters ---
\pgfmathsetmacro{\R}{3}
\pgfmathsetmacro{\h}{1.5}
\pgfmathsetmacro{\rc}{sqrt(\R*\R - \h*\h)}
\pgfmathsetmacro{\phiCap}{acos(\h/\R)}
% --- Drawing the Sphere and Cap ---
% 1. Draw the full sphere
\tdplotdrawsphere[sphere]{0}{0}{0}{\R}
% 2. Draw the spherical cap (highlighted part)
% Ensure the cap's opacity allows the sphere to be visible if it's "inside"
\tdplotfillrmax[cap]{0}{0}{0}{\R}{0}{\phiCap}{0}{360}
% --- Drawing the Intersection Circle (Plane) ---
% Calculate the start and end angles for the visible and hidden arcs of the circle.
\pgfmathsetmacro{\startVis}{fmod(\tdplotmainphi - 90 + 360, 360)}
\pgfmathsetmacro{\endVis}{fmod(\tdplotmainphi + 90 + 360, 360)}
\pgfmathsetmacro{\startHid}{fmod(\tdplotmainphi + 90 + 360, 360)}
\pgfmathsetmacro{\endHid}{fmod(\tdplotmainphi + 270 + 360, 360)}
% Set the drawing plane for the circle: it's parallel to the XY-plane (theta=90)
\tdplotsetthetaplanecoords{90}
% Draw the hidden part of the intersection circle
\tdplotdrawarc[plane, dashed]{0}{0}{\h}{\rc}{\startHid}{\endHid}
% Draw the visible part of the intersection circle
\tdplotdrawarc[plane]{0}{0}{\h}{\rc}{\startVis}{\endVis}
% --- Adding Labels and Axes ---
% Origin
\node[label, below left] at (0,0,0) {O};
% Z-axis
\draw[axis] (0,0,0) -- (0,0,\R+0.7) node[label, right] {$Z$};
% Height 'h' dimension line
\draw[dimline] (0,0,0) -- (0,0,\h);
\node[label, below right] at (0,0,\h/2) {$h$};
% Sphere radius 'R' from origin to the pole
\draw[dimline] (0,0,0) -- (0,0,\R);
\node[label, above] at (0,0,\R) {$(0,0,R)$}; % or just 'R'
% Radius 'rc' of the intersection circle
\draw[dimline] (0,0,\h) -- (\rc,0,\h);
\node[label, below] at (\rc/2,0,\h) {$r_c$};
% Sphere radius 'R' from origin to a point on the intersection circle
\draw[dimline] (0,0,0) -- (\rc,0,\h);
\node[label, below, xshift=-5pt, yshift=-5pt] at (\rc/2,0,\h/2) {$R$};
% A label for the spherical cap
\node[label, above, xshift=-15pt] at (0,0,\R*0.75) {Spherical Cap};
\end{tikzpicture}
\end{document}
blue!30!white: Instead of blue!10, we use blue!30!white. This mixes 30% blue with 70% white, creating a slightly darker and more saturated base color for the sphere. The shading will have more contrast.
light source={(45,45)}: This tells ball color to simulate a light source coming from an azimuth of 45 degrees and an elevation of 45 degrees. This creates more distinct highlights and shadows, enhancing the 3D appearance.
opacity=0.9: Increased from 0.6 to make the sphere more opaque, which further solidifies the perceived shape and makes the shading more impactful.
cap/.style={fill=blue!60!white, opacity=0.9}:
Adjusted the fill color and opacity for the cap to complement the changes to the main sphere. It's slightly darker than the sphere's ball color base, making it stand out as the highlighted part.
With these changes, the sphere will appear much more three-dimensional and less like a flat circle.