23

I'm trying to create a signed distance function S(x), where I want to intersect S(x) = 0 with a plane. The problem is not to get the two functions into a plot, but to get the two functions to visual intersect with each other, i.e the nearest surface is in the foreground. Currently, I have something like:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[grid=major]
      \addplot3[surf,domain=-10:10,samples=40]
      {-sqrt((x-0)^2/1 + (y-0)^2/1) + 5};
      \addplot3[surf,domain=-10:10,samples=2,opacity=0.5]
      {0*x+0*y};
    \end{axis}
  \end{tikzpicture}  
\end{document}

Which gives: enter image description here

As you can see, the plane is only overlapping the function and not intersecting. How can it be done? Bonus question: Is it possible to create a contour on the 3d surf plot at the intersection?

4
  • 2
    Unfortunately, this can't be done with pgfplots. From the manual: "pgfplots supports z buffering techniques up to a certain extent. (...) However, it can't combine different \addplot commands, those will be drawn in the order of appearance."
    – Jake
    Apr 23, 2012 at 6:26
  • 3
    You can use Asymptote instead, it has better 3D support.
    – Leo Liu
    Apr 23, 2012 at 6:45
  • Thanks for the quick response! Leo, I haven't tryout Asymptote before but I think I will stick with pgfplots for now.
    – aagaard
    Apr 23, 2012 at 7:20
  • I have found a hack that allows to plot an arbitrary number of surfaces correctly, without doing anything manually: tex.stackexchange.com/a/394066/38641.
    – iavr
    Oct 1, 2017 at 10:55

1 Answer 1

27

This can't be done automatically, unfortunately, since pgfplots can't do z buffering between different \addplot commands.

For this concrete application, you could construct the plot "by hand", however:

First, you draw the part of the cone below 0, then you draw the plane and the circle, then you draw the part of the cone above 0.

I've used a polar coordinate system for this, since it makes the input of polar functions easier:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[grid=major,view={20}{40},z buffer=sort, data cs=polar]
      \addplot3 [surf, domain=0:360, domain y=5:10,samples=30, samples y=10]
      {-y+5};
      \addplot3 [data cs=cart,surf,domain=-10:10,samples=2, opacity=0.5]
      {0};
      \addplot3 [domain=0:360, samples y=0, samples=30, thick, z buffer=auto]
      (x,5.1,0);
      \addplot3 [surf,domain=0:360, domain y=0:5,samples=30, samples y=10]
      {-y+5};
    \end{axis}
  \end{tikzpicture}  
\end{document}
2
  • 1
    Nice alternative. In my particular case the intersection surf-plots will not be more complicated than that, so this will work for me. Thanks (also for the comment about z-buffering)
    – aagaard
    Apr 23, 2012 at 7:23
  • Nice plot! I added it to the PGFPlots example gallery.
    – Stefan Kottwitz
    Mar 17, 2014 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .