4

I am trying to create a wedge from a cylinder like

wedge of a cylinder

I have been able to get the basics:

\documentclass[11pt,tikz,border=3.14pt]{standalone}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}

\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{tikzpicture}[tdplot_main_coords,scale=3]

\def\pos{0.81} %value 0 < \pos < 1

\coordinate (O) at (0,0,0);

\coordinate (A) at (0,0,0);
\coordinate (B) at (1,0,0);
\coordinate (C) at (1,2,0);
\coordinate (D) at (0,2,0);

\coordinate (AT) at (0,0,1);
\coordinate (BT) at (1,0,1);
\coordinate (CT) at (1,2,1);
\coordinate (DT) at (0,2,1);

        \draw[thick,black] (A) -- (B) -- (C) -- (D) -- cycle;
        \draw[thick,black] (AT) -- (BT) -- (CT) -- (DT) -- cycle;

        \draw[thick,black] (A) -- (AT);
        \draw[thick,black] (B) -- (BT);
        \draw[thick,black] (C) -- (CT);
        \draw[thick,black] (D) -- (DT);

        \draw[thin,black] (1,1,0) -- (0,1,0) -- (0,1,1) -- cycle;

% The circle/ellipse:
\begin{scope}[canvas is xy plane at z=0]
        \draw[thick] (1,0) arc (270:90:1);
\end{scope}

\begin{scope}[
                plane origin={(1,0,0)},
                plane x={(1,1,0)},
                plane y={(0,0,1)},
                canvas is plane
             ]
        \draw[thick] (0,0) arc (180:0:1);
\end{scope}

% The manually computed right-edge:
        \draw[thin] ({1-sqrt(1-\pos^2)},1+\pos,0) -- ({1-sqrt(1-\pos^2)},1+\pos,{sqrt(1-\pos^2)}) ;

% The axis:
    \draw[->] (O) --++ (1.5,0,0) node[below] {$x$};
    \draw[->] (O) --++ (0,2.5,0) node[below] {$y$};
    \draw[->] (O) --++ (0,0,1.25) node[right] {$z$};
\end{tikzpicture}

\end{document}

that renders as:

image with manual right-edge

But I have a couple of roadblocks:

I would like to compute the right-edge of the cylinder automatically, much like this answer here, so I can experiment with the best angle view of the image, but I guess, I did not manage to understand how that solution fully works.

I also would like to shade the whole solid surface, but I am having problems with the edge computed by hand.

4
  • Hi! To obtain the right edge for example, you need to know the observer's direction of view. It is defined by two angles, a latitude and a longitude. Having them, in fact you need only the longitude for this computation, you have to determine the point of tangency between the base circle of your cylinder and the line having the direction determined by the longitude. Let say that in your figure the direction of view is 30 degrees longitude and ..., Then the tangency point you are looking for is characterized by the angle of 120 degrees (0 degrees being the point with x max and y=R.
    – Daniel N
    Commented Sep 20 at 12:03
  • I do understand what you say above, that is to say, the mathematical part. What I do NOT understand is the intricate play of the \pgf commands of @MaciejSkorski, that I cited, to achieve it. From the fact that an important issue that is overlooked by many people is sitting there without any comments or votes , it is an indication that not very many people understood it either. It would be nice if you or him, could digress on the construction and what each piece does. –
    – Knudsen
    Commented Sep 20 at 14:20
  • I wouldn't conclude anything from the numbers of votes or comments, especially for an answer posted so long after the question was first answered. Even answers posted minutes or hours later typically get fewer votes and comments than those posted earlier (unless they get accepted and then sometimes the counts change over time). Earlier answers have a massive advantage: that's just how the site works. In theory, the best answers float to the top. In practice, the earliest answers tend to triumph. (There are exceptions, especially if earlier answers don't do the job, but generally.)
    – cfr
    Commented Sep 21 at 18:02
  • @cfr I agree that no generalization could be good enough, but it is noticeable that this important problem (and solution by @MaciejSkorski) while people keep populating examples and galleries with hand calculated cylinder boundaries.
    – Knudsen
    Commented Sep 22 at 0:27

1 Answer 1

5

longitude 63 theta 60

I was thinking about something like this; in this image the longitude (defining the observer equals 63 degrees and the angle theta equals 60 degrees.

In the code, the point of interest (the "tangency" point) is denoted P. The code uses tikz-3dplot and the library math to have all the parameters: latitude, longitude, angle theta, radius.

Below is the drawing with longitude=45 and theta=39.

longitude 45 and theta 39

The code

\documentclass[11pt, border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{tikz-3dplot}
\begin{document}

\tikzset{%
  view/.style 2 args={%  observer longitude and latitude (y upwards)
                      %  Remark. lomg=0 means x=0
    z={({-sin(#1)}, {-cos(#1)*sin(#2)})},
    x={({cos(#1)}, {-sin(#1)*sin(#2)})},
    y={(0, {cos(#2)})},
    evaluate={%
      \toxx={sin(#1)*cos(#2)};
      \toyy={sin(#2)};
      \tozz={cos(#1)*cos(#2)};
    },
    longitude = #1,
    latitude = #2
  },
  c arc/.style args={#1:#2:#3}{insert path={++(#1:#3) arc (#1:#2:#3)}},
}
\pgfkeys{/tikz/.cd,
  latitude/.store in=\toLatit,  % observer's latitude
  latitude=0
}
\pgfkeys{/tikz/.cd,
  longitude/.store in=\toLongit,  % observer's longitude
  longitude=0  % corresponds to x=0
}

\begin{tikzpicture}[view={45}{20}, every node/.style={scale=.8}]
  \tikzmath{%
    \r = 2;
    \ang = 39;
    \h = \r*tan(\ang);
    \tanAng = \toLongit +90;
    \rPrime = \r/cos(\ang);
  }

  % the prism
  \draw[gray, dashed, very thin, canvas is yz plane at x=-\r]
  (0, 0) -- ++(\h, 0) -- ++(0, -\r) -- ++(-\h, 0) -- cycle;
  \draw[gray, dashed, very thin, canvas is xy plane at z=-\r]
  (-\r, 0) -- ++(2*\r, 0) -- ++(0, \h) -- ++(-2*\r, 0) -- cycle;
  \draw[gray, dashed, very thin, canvas is zx plane at y=0]
  (0, -\r) -- ++(0, 2*\r) -- ++(-\r, 0) -- ++(0, -2*\r) -- cycle;
  \draw[canvas is yz plane at x=\r]
  (0, 0) -- ++(\h, 0) -- ++(0, -\r) -- ++(-\h, 0) -- cycle;
  \draw[canvas is xy plane at z=0]
  (-\r, 0) -- ++(2*\r, 0) -- ++(0, \h) -- ++(-2*\r, 0) -- cycle;
  \draw[canvas is zx plane at y=\h]
  (0, -\r) -- ++(0, 2*\r) -- ++(-\r, 0) -- ++(0, -2*\r) -- cycle;

  % the angle seen in x=0
  \draw[blue, very thin, canvas is yz plane at x=0]
  (\h, -\r) -- (0, 0) -- (0, -\r);
  \draw[blue, very thin, canvas is yz plane at x=0]
  (-90: 1/3*\r)  arc (-90:{-90+\ang}:1/3*\r)
  node[pos=.6, right] {$\theta$};
  
  % the arc in the horizontal plane y = 0
  \draw[red, dashed, canvas is zx plane at y=0]
  (90:\r) arc (90:270:\r);

  % the arc in the slanted plane
  \draw[red, thick, fill=red!60, fill opacity=.5,
  rotate around x=\ang, canvas is zx plane at y=0]
  (90:\r) arc (90:270:\rPrime cm and \r cm) -- cycle;

  % the tangency point
  \path[canvas is zx plane at y=0] (\tanAng: \r) coordinate (P);

  % the cylinder (visible part)
  \fill[red, opacity=.5] (0, 0, 0)
  {[rotate around x=\ang, canvas is zx plane at y=0]
    -- (90:\r) arc (90:\tanAng:\rPrime cm and \r cm)}
  {[canvas is zx plane at y=0] -- (P) arc (\tanAng:90:\r)}
  -- cycle;

  % the visible arc in the plane y = 0
  \draw[red, thick, canvas is zx plane at y=0]
  (P) arc (\tanAng:90:\r);

  % z axis
  \draw[very thick, ->] (-\r, 2/3*\h, 0) -- ++(0, 0, 2/3*\r)
  node[pos=.6, above] {$y$};
\end{tikzpicture}
\end{document}
8
  • Very nice and also easy to understand.
    – Knudsen
    Commented Sep 22 at 0:27
  • Can you show how one could add a line at the edge of the cylinder?
    – Knudsen
    Commented Sep 22 at 4:44
  • If you need a vertical border to the visible part of the cylinder, just change \fill to \draw[red, fill=...]. If you need a ruler in general, then take a point on the basis and the corresponding one on the slanted ellipse (the angles must be equal.).
    – Daniel N
    Commented Sep 22 at 6:40
  • I think I have now understood all your code, and I have one question: Wouldn't it be easier to draw the solids in 3-dim coordinates? And avoid the entire setup for the viewing angle... Would finding the edge be the reason?
    – Knudsen
    Commented Sep 23 at 18:29
  • The edge that defines the portion of the cylinder that can be seen depends on the point of view, i.e. the observer's position. That was your initial question; How to find that segment? Anyway, if for example you want to draw a sphere, or a torus, you will always need the observer's position. if you want a 3d object with its local color in the light, then you will need the observer's position and the light source's position.
    – Daniel N
    Commented Sep 23 at 19:09

You must log in to answer this question.

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