TransWikia.com

Automatic dotted lines

TeX - LaTeX Asked by Junpyo Choi on May 13, 2021

documentclass[12pt]{article}



usepackage{pgfplots}
pgfplotsset{compat=1.15}
usepackage{tikz, tkz-euclide,tikz-3dplot,ifthen}
usetikzlibrary{arrows,decorations.markings,calc,fadings,decorations.pathreplacing, patterns, decorations.pathmorphing, positioning}
usepgfplotslibrary{fillbetween}

begin{document}



centering
begin{tikzpicture}
begin{axis}[
axis lines=middle,
 view={135}{30},
 ticks=none,
  zmin=0,zmax=5,
  xmin=-3,xmax=3,
  ymin=-3,ymax=5,
  axis line style={draw=none},
  style={draw=none}
]





draw[fill=gray!50,opacity=0.5](2,0,4)--(0,0,4)--(0,3,4)--(2,3,4)--cycle;

draw[fill=gray!50,opacity=0.5](2,3,4)--(0,3,4)--(0,3,0)--(2,3,0)--cycle;

draw[fill=gray!50,opacity=0.5](2,0,0)--(2,0,4)--(2,3,4)--(2,3,0)--cycle;

draw[dashed](0,0,0)--(0,0,4);
draw[-latex](0,0,4)--(0,0,5)node[right]{$z$};

draw(-2.3,0,0)--(-3,0,0);
draw[dashed](2,0,0)--(-2.3,0,0);
draw[-latex](2,0,0)--(3,0,0)node[above]{$x$};

draw(0,-2.65,0)--(0,-4,0);
draw[dashed](0,-2.65,0)--(0,3,0);
draw[-latex](0,3,0)--(0,5,0)node[above]{$y$};
  
end{axis}
end{tikzpicture}



end{document}

enter image description here

I was wondering if there is a way to let dashed lines be drawn automatically when the lines are behind surfaces. Shoud I just find those lines and manually draw them as above?

One Answer

You can use 3dtools here. Many thanks to marmot for support this answer.

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
begin{document}
    pgfdeclarelayer{background}
    pgfdeclarelayer{foreground} 
    pgfsetlayers{background,main,foreground}
    foreach Angle in {60}
    {begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt}, 
            3d/install view={phi=Angle,psi=0,theta=70},
            same bounding box=A]
            path
            (0,0,0) coordinate (O)
            (a,0,0) coordinate (A)
            (a,a,0) coordinate (B)
            (0,a,0) coordinate (C)
            (0,0,h) coordinate (O')
            (a,0,h) coordinate (A')
            (a,a,h) coordinate (B')
            (0,a,h) coordinate (C')
            (a/2,a/2,h/2) coordinate (T);   
            tikzset{3d/polyhedron/.cd,O={(T)},
                fore layer=foreground,back layer=background,
                fore/.append style={fill=gray!50,opacity=0.5,thick},
                back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
                %edges have complete dashes,
                draw face with corners={{(O)},{(A)},{(B)},{(C)}},
                draw face with corners={{(O')},{(A')},{(B')},{(C')}},
                draw face with corners={{(A)},{(B)},{(B')},{(A')}},
                draw face with corners={{(O)},{(C)},{(C')},{(O')}},
                draw face with corners={{(O)},{(A)},{(A')},{(O')}},
                draw face with corners={{(B)},{(C)},{(C')},{(B')}}
            }
            %path foreach p/g in {A/-90,B/-90,C/-90,O/-90}{(p)node[c]{}+(g:2.5mm) node{$p$}}; 
            path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}]; 
            path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
            path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
            path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
            tikzset{3d/ordered paths/.cd,
                hull/.style={draw=none},
                lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
            foreach Dir/Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}  
            {
                pgfmathtruncatemacro{itest}{screendepth(Dir)<0?0:1} 
                ifnumitest=0
                tikzset{3d/draw ordered paths={lLine,hull}}
                else
                tikzset{3d/draw ordered paths={hull,lLine}}
                fi}
    end{tikzpicture}}
end{document}  

enter image description here

Or you can use

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
begin{document}
    pgfdeclarelayer{background}
    pgfdeclarelayer{foreground} 
    pgfsetlayers{background,main,foreground}
    foreach Angle in {10,20,...,350}
    {begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt}, 
            3d/install view={phi=Angle,psi=0,theta=70},
            same bounding box=A]
            path
            (0,0,0) coordinate (O)
            (a,0,0) coordinate (A)
            (a,a,0) coordinate (B)
            (0,a,0) coordinate (C)
            (0,0,h) coordinate (O')
            (a,0,h) coordinate (A')
            (a,a,h) coordinate (B')
            (0,a,h) coordinate (C')
            (a/2,a/2,h/2) coordinate (T);   
            tikzset{3d/polyhedron/.cd,O={(T)},
                fore layer=foreground,back layer=background,
                fore/.append style={fill=gray!50,opacity=0.5,thick},
                back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
                %edges have complete dashes,
                draw face with corners={{(O)},{(A)},{(B)},{(C)}},
                draw face with corners={{(O')},{(A')},{(B')},{(C')}},
                draw face with corners={{(A)},{(B)},{(B')},{(A')}},
                draw face with corners={{(O)},{(C)},{(C')},{(O')}},
                draw face with corners={{(O)},{(A)},{(A')},{(O')}},
                draw face with corners={{(B)},{(C)},{(C')},{(B')}}
            }
            %path foreach p/g in {A/-90,B/-90,C/-90,O/-90}{(p)node[c]{}+(g:2.5mm) node{$p$}}; 
            path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}]; 
            path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
            path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
            path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
            tikzset{3d/ordered paths/.cd,
                hull/.style={draw=none},
                lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
            foreach Dir/Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}  
            {
                pgfmathtruncatemacro{itest}{screendepth(Dir)<0?0:1} 
                ifnumitest=0
                tikzset{3d/draw ordered paths={lLine,hull}}
                else
                tikzset{3d/draw ordered paths={hull,lLine}}
                fi}
    end{tikzpicture}}
end{document}    

enter image description here

Or for longer axes

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{calc,3dtools}% https://github.com/marmotghost/tikz-3dtools
begin{document}
    pgfdeclarelayer{background}
    pgfdeclarelayer{foreground} 
    pgfsetlayers{background,main,foreground}
    foreach Angle in {10,20,...,350}
    {begin{tikzpicture}[line cap=round,line join=round,declare function={a=3;h=5;}, c/.style={circle,fill,inner sep=1pt}, 
            3d/install view={phi=Angle,psi=0,theta=70},
            same bounding box=A]
            path
            (0,0,0) coordinate (O)
            (a,0,0) coordinate (A)
            (a,a,0) coordinate (B)
            (0,a,0) coordinate (C)
            (0,0,h) coordinate (O')
            (a,0,h) coordinate (A')
            (a,a,h) coordinate (B')
            (0,a,h) coordinate (C')
            (a/2,a/2,h/2) coordinate (T);   
            tikzset{3d/polyhedron/.cd,O={(T)},
                fore layer=foreground,back layer=background,
                fore/.append style={fill=gray!50,opacity=0.5,thick},
                back/.append style={3d/polyhedron/complete dashes,fill=gray!50,opacity=0.5},
                %edges have complete dashes,
                draw face with corners={{(O)},{(A)},{(B)},{(C)}},
                draw face with corners={{(O')},{(A')},{(B')},{(C')}},
                draw face with corners={{(A)},{(B)},{(B')},{(A')}},
                draw face with corners={{(O)},{(C)},{(C')},{(O')}},
                draw face with corners={{(O)},{(A)},{(A')},{(O')}},
                draw face with corners={{(B)},{(C)},{(C')},{(B')}}
            }
            %path foreach p/g in {A/-90,B/-90,C/-90,O/-90}{(p)node[c]{}+(g:2.5mm) node{$p$}}; 
            path[save named path=hull,convex hull of={O,A,B,C,O',A',B',C'}]; 
            path[save named path=lx] (A) -- (a+2,0,0) node [pos=1.15] {$x$};
            path[save named path=lx'] (O) -- (-a-2,0,0);
            path[save named path=ly] (C) -- (0,a+1,0) node [pos=1.15] {$y$};
            path[save named path=ly'] (O) -- (0,-a-2,0);
            path[save named path=lz] (O') -- (0,0,h+1) node [pos=1.15] {$z$};
            path[save named path=lz'] (O) -- (O);
            tikzset{3d/ordered paths/.cd,
                hull/.style={draw=none},
                lx/.style={-latex},ly/.style={-latex},lz/.style={-latex}}
            foreach Dir/Line in {{1,0,0}/x,{0,1,0}/y,{0,0,1}/z}  
            {
                pgfmathtruncatemacro{itest}{screendepth(Dir)<0?0:1} 
                ifnumitest=0
                tikzset{3d/draw ordered paths={lLine,hull,lLine'}}
                else
                tikzset{3d/draw ordered paths={lLine',hull,lLine}}
                fi}
    end{tikzpicture}}
end{document}    

enter image description here

Correct answer by minhthien_2016 on May 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP