11

I am working on drawing a beam in perspective. I have my point I want to use as the perspective point (10, 3). What would be the way to draw along this line? For instance, consider

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0, 0) rectangle (1cm, 1cm);
% I want to draw a 2cm line from (1cm, 0) along the ray that goes to my vanishing point
\end{tikzpicture}
\end{document}
  1. I could set \pgfmathsetmacro\myangle{atan(3/10)} and use the \usetikzlibrary{calc} to issue \draw (1cm, 0) -- ++(\myangle:2cm);
  2. I have tried issuing \def\myangle{tan(3/10)} but this doesn't work at all.
  3. Or I could use scope and rotate a the line \draw (1cm, 0) -- (2cm, 0); by the intended angle.

After fixing tan to atan @PeterGrill, I realized there is a problem with drawing to the vanishing point. Only (0, 0) would be on the line with angle atan(3/10). How can make adjustments for the other points? I know I could figure out the math but can LaTeX handle this instead?

dustin
  • 18,617
  • 23
  • 99
  • 204

3 Answers3

14

Another alternative from tikz-3dplot that provides more fun stuffs.

  1. Set the xyz coordinate system to xy coordinate system by \tdplotsetmaincoords{90}{90}
  2. Draw a large square first.
  3. Determine the vanishing point (X) at the center of the square.
  4. Use calc to determine the coordinates of a smaller box via ($(d\i)!\s!(X)$) that requires calc.
  5. Switch back to xyz coordinate via \tdplotsetmaincoords{70}{120} and many other different view angles available.
  6. Code given below.

enter image description here

Code

\documentclass[border=1cm,varwidth]{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{shapes,calc,positioning}
\tdplotsetmaincoords{90}{90}

% decide the focus at distance f from the square, like (10,3) of the OP
\def\f{-8}    % try,10 etc

% determine the location of the smaller box
\def\s{0.7}   % 0<s<1

\begin{document}

\begin{tikzpicture}[scale=2, tdplot_main_coords,axis/.style={->,dashed},thick]
\draw[axis] (3, 0, 0) -- (-3, 0, 0) node [right] {$X$};
\draw[axis] (0, 0, 0) -- (0, 3, 0) node [above] {$Y$};
\draw[axis] (0, 0, 0) -- (0, 0, 3) node [above] {$Z$};

\node[coordinate]  (d1) at (2,0,0){};
\node[coordinate]  (d2) at (2,2,0){};
\node[coordinate]  (d3) at (2,2,2){};
\node[coordinate]  (d4) at (2,0,2){};

\coordinate (X) at (\f,1,1);  % change -5 to sue one's needs via \f.

\foreach \i in {1,2,3,4} {
\node[coordinate]  (t\i) at ($(d\i)!\s!(X)$){};
}

% draw lines

\foreach \i in {1,2,3,4}{
\draw[color=blue] (d\i) --(X);
}
\draw [fill=yellow,opacity=1]   (t1)--(t2)--(t3)--(t4)--cycle;
\draw [fill=yellow,opacity=0.5] (d1)--(d2)--(d3)--(d4)--cycle;

\end{tikzpicture}

% Try different view angle/perspective
% view from x {90}{90}
% view from y {90}{0}
% view from z {0}{90}
% view from the first   quadrant {70}{120}
% view from the second  quadrant {120}{70}

\tdplotsetmaincoords{70}{120}

\begin{tikzpicture}[scale=2, tdplot_main_coords,axis/.style={->,dashed},thick]
\draw[axis] (3, 0, 0) -- (-3, 0, 0) node [right] {$X$};
\draw[axis] (0, 0, 0) -- (0, 3, 0) node [above] {$Y$};
\draw[axis] (0, 0, 0) -- (0, 0, 3) node [above] {$Z$};

\node[coordinate]  (d1) at (2,0,0){};
\node[coordinate]  (d2) at (2,2,0){};
\node[coordinate]  (d3) at (2,2,2){};
\node[coordinate]  (d4) at (2,0,2){};
\coordinate  (X)  at (\f,1,1);

\foreach \i in {1,2,3,4} {
\node[coordinate]  (t\i) at ($(d\i)!\s!(X)$){};
}

% draw lines    
\foreach \i in {1,2,3,4}{
\draw[color=blue] (d\i) node[above right] {\color{red} \tiny \i} --(X);
}
\draw [fill=yellow, opacity=0.5] (d1)--(d2)--(d3)--(d4)--cycle;
\draw [fill=yellow, opacity=0.5] (t1)--(t2)--(t3)--(t4)--cycle;
\end{tikzpicture}

\end{document}
Jesse
  • 29,686
9

May be I misunderstood the question (happens to me lately), but may be you are looking for

\usetikzlibrary{calc}
..
\coordinate (vanishingpoint) at (10,3);
\draw (1,0) -- ($(1,0)!2cm!(vanishingpoint)$);
Manuel
  • 27,118
5

The red and blue lines are just to show the vanishing point -- they start from each coordinate of the rectangle, and converge at the vanishing point (X). A scope is used to translate the object to 70% of the way to the vanishing point, and scale the object to 30% of its original size.

enter image description here

Notes:

  • Package the application of the scope to apply a shift and the scale.

Code:

\documentclass[tikz, border=2pt]{standalone}
\begin{document}
\usetikzlibrary{calc}

\newcommand*{\DrawAlongVanashingPoint}[4][]{% % #1 = draw options % #2 = start point % #3 = distance from point % #4 = vanishing point \draw [#1] (#2) -- ($(#2)!#3!(#4)$); } \begin{tikzpicture} \draw [fill=yellow] (0, 0) rectangle (1cm, 1cm); % I want to draw a 2cm line from (1cm, 0) along the ray that goes to my vanishing point \pgfmathsetmacro\myangle{atan(3/10)} \draw [thin, red] (1cm, 0) -- ++(\myangle:2cm) coordinate (X); \draw [thin, red] (1,1) -- (X); \draw [thin, red] (0,1) -- (X); \draw [thin, red] (0,0) -- (X);

\DrawAlongVanashingPoint[thin, blue]{1,1}{0.7}{X}; \DrawAlongVanashingPoint[thin, blue]{0,1}{0.7}{X}; \DrawAlongVanashingPoint[thin, blue]{0,0}{0.7}{X}; \DrawAlongVanashingPoint[thin, blue]{1,0}{0.7}{X};

%\draw [fill=white](0, 0) rectangle (1cm, 1cm);% to hide the vanishing lines

\coordinate (ShiftPoint) at ($(0,0)!0.7!(X)$); \begin{scope}[shift={(ShiftPoint)}, scale=0.3] \draw [fill=yellow] (0, 0) rectangle (1cm, 1cm); \end{scope}

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288