LaTeX forum ⇒ GeneralHow can I reduce my code? Topic is solved

LaTeX specific issues not fitting into one of the other forums of this category.
Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

How can I reduce my code?

Postby Tran Van Toan » Sat Nov 11, 2017 9:36 am

I draw a cone with base is a regular polygon with ten edgs. This is my code:
  1. \documentclass[border=2mm,tikz]{standalone}
  2. \usepackage{fouriernc}
  3. \usepackage{tikz-3dplot}
  4. \usetikzlibrary{calc,backgrounds}
  5. \usepackage{tkz-euclide,amsmath}
  6. \usetkzobj{all}
  7. \usepackage{pgfplots}
  8.  
  9. \begin{document}
  10.  
  11. %polar coordinates of visibility
  12. \pgfmathsetmacro\th{60}
  13. \pgfmathsetmacro\az{130}
  14.  
  15. \tdplotsetmaincoords{\th}{\az}
  16.  
  17. %parameters of the cone
  18. \pgfmathsetmacro\R{3}
  19. \pgfmathsetmacro\v{5}
  20.  
  21.  
  22. \begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]
  23. \path
  24. coordinate (O) at (0,0,0)
  25. coordinate (S) at (0,0,\v);
  26. \coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);
  27. \coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);
  28. \coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);
  29. \coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);
  30. \coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);
  31. \coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);
  32. \coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);
  33. \coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);
  34. \coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);
  35. \coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);
  36. \tkzDrawPoints[fill = black,size = 5 pt](S,A,B,C,D,E,F,O,G,H,I,J)
  37. % % computation of tangential points
  38. \pgfmathsetmacro\cott{{cot(\th)}}
  39. \pgfmathsetmacro\fraction{\R*\cott/\v}
  40. \pgfmathsetmacro\angle{{acos(\fraction)}}
  41.  
  42. % % angles for transformed lines
  43. \pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
  44. \pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}
  45.  
  46. % % coordinates for transformed surface lines
  47. \pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
  48. \pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
  49. \pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
  50. \pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}
  51.  
  52. % % angles for original surface lines
  53. \pgfmathsetmacro\sinazp{{sin(\az-90)}}
  54. \pgfmathsetmacro\cosazp{{cos(\az-90)}}
  55. \pgfmathsetmacro\sinazm{{sin(90-\az)}}
  56. \pgfmathsetmacro\cosazm{{cos(90-\az)}}
  57.  
  58. % % draw basis circle
  59. \tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
  60. \tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}
  61.  
  62. % % displaying tranformed surface of the cone (rotated)
  63. \draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
  64. \draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);
  65.  
  66. % % displaying original surface of the cone (rotated)
  67. \draw[thick] (S) -- (A) (S) -- (B) (S) -- (C) (S)--(D) (S) -- (I) (S) -- (J) (S) -- (H);
  68. \tkzDrawPolygon[dashed](A,B,C,D,E,F,G,H,I,J)
  69. %\draw[dashed] (A) -- (B) --(C)--(D)--(E)--(F) --(G)--(H)-- (I)-- (J) --(A) --cycle;
  70. \draw[dashed](S) --(E) (S) -- (F) (S) --(O) (S)--(G) (A)--(F) (B)--(G) (C)--(H) (D)--(I) (E)--(J);
  71. %\tkzLabelPoints[right](S,A,B,C,D,E,F,G,H,I,J)
  72. \tkzLabelPoints[above](S)
  73. \tkzLabelPoints[below](O)
  74. %\tkzFillPolygon[pattern=north west lines](A,B,C,D,E,F)
  75. \end{tikzpicture}
  76. \end{document}


How can I reduce the lines?
  1. [code]\coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);
  2. \coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);
  3. \coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);
  4. \coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);
  5. \coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);
  6. \coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);
  7. \coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);
  8. \coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);
  9. \coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);
  10. \coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);

[/code]
Attachments
ScreenHunter 53.png
ScreenHunter 53.png (64.68 KiB) Viewed 283 times

Tags:

rais
Posts: 149
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Sat Nov 11, 2017 2:07 pm

You could use a \foreach statement instead, say
  1. \foreach \a/\b in {A/1,B/2,C/3,D/4,E/5,F/6,G/7,H/8,I/9,J/10}
  2. \coordinate (\a) at ($(O) + (\b*36:{\R})$);

KR
Rainer

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Tue Nov 14, 2017 11:33 am

Thank you very much.

User avatar
Stefan Kottwitz
Site Admin
Posts: 8522
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Wed Nov 15, 2017 5:17 pm

Hi,

even simpler:

  1. \foreach \i in {1,...,10}
  2. \coordinate (\Alph{\i}) at ($(O) + (\i*36:{\R})$);


That of course requires a macro \Alph to turn numbers into letters. There is one internal macro, that could be used that way if you would add to the document preamble:

  1. \makeatletter
  2. \let\Alph\@Alph
  3. \makeatother


Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Fri Nov 17, 2017 6:11 pm

Thank you very much. I still a small problem:
How can I reduce this command
  1. \draw[dashed] (\Alph{1}) -- (\Alph{2})-- (\Alph{3}) -- (\Alph{4})--(\Alph{5}) -- (\Alph{6})-- (\Alph{7}) -- (\Alph{8})--(\Alph{9}) -- (\Alph{10})--cycle;

User avatar
Stefan Kottwitz
Site Admin
Posts: 8522
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Fri Nov 17, 2017 6:38 pm

For example:

\draw[red,foreach/.style={insert path=--(\Alph{#1})}] (A) [foreach/.list={2,...,10,1}] --cycle;

Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Sat Nov 18, 2017 6:18 am

Thank you for all your helps. I repaired my code and got the same results.
Please comment my code. I need all comments.
A big problem is how to write a marco to draw style of line (dashed, not dashed) automatically. I can not solve this question by myselt.
This is solved by hand.

  1. \foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});
  2. \foreach \i in {5,6,7} \draw[dashed] (S)--(\Alph{\i});


If regullar polygon with 20 edgs, it's difficult for me to choice style of lines.

And this is my full code.
  1. \documentclass[border=2mm,tikz]{standalone}
  2. \usepackage{fouriernc}
  3. \usepackage{tikz-3dplot}
  4. \usetikzlibrary{calc,backgrounds}
  5. \usepackage{tkz-euclide,amsmath}
  6. \usetkzobj{all}
  7. \usepackage{pgfplots}
  8. \makeatletter
  9. \let\Alph\@Alph
  10. \makeatother
  11. \begin{document}
  12.  
  13. %polar coordinates of visibility
  14. \pgfmathsetmacro\th{60}
  15. \pgfmathsetmacro\az{130}
  16. \tdplotsetmaincoords{\th}{\az}
  17. %parameters of the cone
  18. \pgfmathsetmacro\R{3}
  19. \pgfmathsetmacro\v{5}
  20. \begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]
  21. \path
  22. coordinate (O) at (0,0,0)
  23. coordinate (S) at (0,0,\v);
  24. \foreach \i in {1,...,10} \coordinate (\Alph{\i}) at ($(O) + (\i*36:{\R})$);
  25. %\foreach \i in {1,...,10} \tkzDrawPoints[fill = black,size = 5 pt] (\Alph{\i})
  26. \tkzDrawPoints[fill = black,size = 5 pt](S);
  27. % % computation of tangential points
  28. \pgfmathsetmacro\cott{{cot(\th)}}
  29. \pgfmathsetmacro\fraction{\R*\cott/\v}
  30. \pgfmathsetmacro\angle{{acos(\fraction)}}
  31.  
  32. % % angles for transformed lines
  33. \pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
  34. \pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}
  35.  
  36. % % coordinates for transformed surface lines
  37. \pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
  38. \pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
  39. \pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
  40. \pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}
  41.  
  42. % % angles for original surface lines
  43. \pgfmathsetmacro\sinazp{{sin(\az-90)}}
  44. \pgfmathsetmacro\cosazp{{cos(\az-90)}}
  45. \pgfmathsetmacro\sinazm{{sin(90-\az)}}
  46. \pgfmathsetmacro\cosazm{{cos(90-\az)}}
  47.  
  48. % % draw basis circle
  49. \tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
  50. \tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}
  51.  
  52. % % displaying tranformed surface of the cone (rotated)
  53. \draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
  54. \draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);
  55.  
  56. \draw[dashed,red,foreach/.style={insert path=--(\Alph{#1})}] (A) [foreach/.list={2,...,10,1}] --cycle;
  57.  
  58. %\foreach \i in {1,...,10} \tkzLabelPoints[left](\Alph{\i});
  59. \foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});
  60. \foreach \i in {5,6,7} \draw[dashed] (S)--(\Alph{\i});
  61. \tkzLabelPoints[above](S)
  62. \tkzLabelPoints[below](O)
  63. \draw[dashed,blue] (\Alph{1})--(\Alph{6}) (\Alph{2})--(\Alph{7}) (\Alph{3})--(\Alph{8}) (\Alph{4})--(\Alph{9}) (\Alph{5})--(\Alph{10}); \draw[dashed,orange]
  64. (S)--(O);
  65. \end{tikzpicture}
  66. \end{document}

User avatar
Stefan Kottwitz
Site Admin
Posts: 8522
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sat Nov 18, 2017 2:43 pm

TikZ cannot automatically detect when a line is behind some area. The area is not even drawn. You have to decide what you make dashed for a simulated background appearance.

Anyway, just for better reading and for doing it in a simple loop, you could also write

  1. \foreach \point/\style in {A/thick, B/thick, C/thick, D/thick,
  2. E/dashed, F/dashed, G/dashed,
  3. H/thick, I/thick, J/thick}
  4. \draw[\style] (S)--(\point);



Stefan
Site admin

Tran Van Toan
Posts: 17
Joined: Sat Oct 28, 2017 1:18 am

Postby Tran Van Toan » Sat Nov 18, 2017 4:18 pm

Thank you very much.


Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests