Last active
February 2, 2018 17:02
-
-
Save seungwonpark/c0884e3ac74b35f319e1404d7826e393 to your computer and use it in GitHub Desktop.
Drawing cube with TikZ using perspective projection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% convert -delay 2 -loop 0 -density 1000 -alpha remove cube.pdf cube.gif | |
\documentclass[tikz]{standalone} | |
\usepackage{tikz} | |
\usepackage{pgffor} | |
\usetikzlibrary{calc} | |
\def\sintheta{0.8660254037844386} | |
\def\costheta{0.5} | |
\def\sinphi{0.9396926207859083} | |
\def\cosphi{0.3420201433256688} | |
\def\cube{1} | |
\def\dist{3} | |
\begin{document} | |
\foreach \sinphi/\cosphi in {0.00000/1.00000, 0.01745/0.99985, 0.03490/0.99939, 0.05234/0.99863, 0.06976/0.99756, 0.08716/0.99619, 0.10453/0.99452, 0.12187/0.99255, 0.13917/0.99027, 0.15643/0.98769, 0.17365/0.98481, 0.19081/0.98163, 0.20791/0.97815, 0.22495/0.97437, 0.24192/0.97030, 0.25882/0.96593, 0.27564/0.96126, 0.29237/0.95630, 0.30902/0.95106, 0.32557/0.94552, 0.34202/0.93969, 0.35837/0.93358, 0.37461/0.92718, 0.39073/0.92050, 0.40674/0.91355, 0.42262/0.90631, 0.43837/0.89879, 0.45399/0.89101, 0.46947/0.88295, 0.48481/0.87462, 0.50000/0.86603, 0.51504/0.85717, 0.52992/0.84805, 0.54464/0.83867, 0.55919/0.82904, 0.57358/0.81915, 0.58779/0.80902, 0.60182/0.79864, 0.61566/0.78801, 0.62932/0.77715, 0.64279/0.76604, 0.65606/0.75471, 0.66913/0.74314, 0.68200/0.73135, 0.69466/0.71934, 0.70711/0.70711, 0.71934/0.69466, 0.73135/0.68200, 0.74314/0.66913, 0.75471/0.65606, 0.76604/0.64279, 0.77715/0.62932, 0.78801/0.61566, 0.79864/0.60182, 0.80902/0.58779, 0.81915/0.57358, 0.82904/0.55919, 0.83867/0.54464, 0.84805/0.52992, 0.85717/0.51504, 0.86603/0.50000, 0.87462/0.48481, 0.88295/0.46947, 0.89101/0.45399, 0.89879/0.43837, 0.90631/0.42262, 0.91355/0.40674, 0.92050/0.39073, 0.92718/0.37461, 0.93358/0.35837, 0.93969/0.34202, 0.94552/0.32557, 0.95106/0.30902, 0.95630/0.29237, 0.96126/0.27564, 0.96593/0.25882, 0.97030/0.24192, 0.97437/0.22495, 0.97815/0.20791, 0.98163/0.19081, 0.98481/0.17365, 0.98769/0.15643, 0.99027/0.13917, 0.99255/0.12187, 0.99452/0.10453, 0.99619/0.08716, 0.99756/0.06976, 0.99863/0.05234, 0.99939/0.03490, 0.99985/0.01745}{ | |
\begin{tikzpicture} | |
\draw[white] (-1,-1) rectangle (1,1); | |
% \node at (0,-0.5) {\sinphi}; | |
\coordinate (A) at ($ ( | |
{(\cube*\sinphi - \cube*\cosphi + 0) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi + \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % +++ | |
\coordinate (B) at ($ ( | |
{(\cube*\sinphi - \cube*\cosphi - 0) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi + \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % ++- | |
\coordinate (C) at ($ ( | |
{(\cube*\sinphi + \cube*\cosphi + 0) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi - \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % +-+ | |
\coordinate (D) at ($ ( | |
{(\cube*\sinphi + \cube*\cosphi - 0) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi - \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % +-- | |
\coordinate (E) at ($ ( | |
{(-\cube*\sinphi - \cube*\cosphi + 0) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi + \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % -++ | |
\coordinate (F) at ($ ( | |
{(-\cube*\sinphi - \cube*\cosphi - 0) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi + \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % -+- | |
\coordinate (G) at ($ ( | |
{(-\cube*\sinphi + \cube*\cosphi + 0) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi - \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % --+ | |
\coordinate (H) at ($ ( | |
{(-\cube*\sinphi + \cube*\cosphi - 0) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi - \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % --- | |
\draw (A) -- (B) -- (D) -- (C) -- (A) -- (E) -- (F) -- (B); | |
\draw (E) -- (G) -- (C); | |
\draw (G) -- (H); | |
\draw (F) -- (H) -- (D); | |
\end{tikzpicture} | |
} | |
\end{document} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{standalone} | |
\usepackage{tikz} | |
\usetikzlibrary{calc} | |
%\def\camtheta{60} | |
%\def\camphi{70} | |
\def\sintheta{0.8660254037844386} | |
\def\costheta{0.5} | |
\def\sinphi{0.9396926207859083} | |
\def\cosphi{0.3420201433256688} | |
\def\cube{1} | |
\def\dist{3} | |
\begin{document} | |
\begin{tikzpicture} | |
% Be careful with calculating: see https://tex.stackexchange.com/a/31833 | |
\coordinate (A) at ($ ( | |
{(\cube*\sinphi - \cube*\cosphi + 0) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi + \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % +++ | |
\coordinate (B) at ($ ( | |
{(\cube*\sinphi - \cube*\cosphi - 0) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi + \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % ++- | |
\coordinate (C) at ($ ( | |
{(\cube*\sinphi + \cube*\cosphi + 0) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi - \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % +-+ | |
\coordinate (D) at ($ ( | |
{(\cube*\sinphi + \cube*\cosphi - 0) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(\cube*\costheta*\cosphi - \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % +-- | |
\coordinate (E) at ($ ( | |
{(-\cube*\sinphi - \cube*\cosphi + 0) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi + \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % -++ | |
\coordinate (F) at ($ ( | |
{(-\cube*\sinphi - \cube*\cosphi - 0) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi + \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi + \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % -+- | |
\coordinate (G) at ($ ( | |
{(-\cube*\sinphi + \cube*\cosphi + 0) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi - \cube*\costheta*\sinphi - \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi + \cube*\costheta - \dist)} | |
) $); % --+ | |
\coordinate (H) at ($ ( | |
{(-\cube*\sinphi + \cube*\cosphi - 0) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)}, | |
{(-\cube*\costheta*\cosphi - \cube*\costheta*\sinphi + \cube*\sintheta) | |
/ (-\cube*\sintheta*\cosphi - \cube*\sintheta*\sinphi - \cube*\costheta - \dist)} | |
) $); % --- | |
\draw (A) -- (B) -- (D) -- (C) -- (A) -- (E) -- (F) -- (B); | |
\draw (E) -- (G) -- (C); | |
\draw (G) -- (H); | |
\draw (F) -- (H) -- (D); | |
\end{tikzpicture} | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment