GeneralHow can I reduce my code?

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

How can I reduce my code?

Post by Tran Van Toan »

I draw a cone with base is a regular polygon with ten edgs. This is my code:

Code: Select all

\documentclass[border=2mm,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\usepackage{tkz-euclide,amsmath}
\usetkzobj{all}
\usepackage{pgfplots}

\begin{document}

%polar coordinates of visibility
\pgfmathsetmacro\th{60}
\pgfmathsetmacro\az{130}

\tdplotsetmaincoords{\th}{\az}

%parameters of the cone
\pgfmathsetmacro\R{3}
\pgfmathsetmacro\v{5}


\begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]
\path
coordinate (O) at (0,0,0)
coordinate (S) at (0,0,\v);
\coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);
\coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);
\coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);
\coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);
\coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);
\coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);
\coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);
\coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);
\coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);
\coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);
\tkzDrawPoints[fill = black,size = 5 pt](S,A,B,C,D,E,F,O,G,H,I,J)
% % computation of tangential points
\pgfmathsetmacro\cott{{cot(\th)}}
\pgfmathsetmacro\fraction{\R*\cott/\v}
\pgfmathsetmacro\angle{{acos(\fraction)}}

% % angles for transformed lines
\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}

% % coordinates for transformed surface lines
\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}

% % angles for original surface lines
\pgfmathsetmacro\sinazp{{sin(\az-90)}}
\pgfmathsetmacro\cosazp{{cos(\az-90)}}
\pgfmathsetmacro\sinazm{{sin(90-\az)}}
\pgfmathsetmacro\cosazm{{cos(90-\az)}}

% % draw basis circle
\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}

% % displaying tranformed surface of the cone (rotated)
\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);

% % displaying original surface of the cone (rotated)
\draw[thick] (S) -- (A) (S) -- (B)  (S) -- (C) (S)--(D) (S) -- (I) (S) -- (J)  (S) -- (H);
\tkzDrawPolygon[dashed](A,B,C,D,E,F,G,H,I,J)
%\draw[dashed] (A) -- (B) --(C)--(D)--(E)--(F) --(G)--(H)-- (I)-- (J) --(A) --cycle;
\draw[dashed](S) --(E) (S) -- (F) (S) --(O) (S)--(G) (A)--(F) (B)--(G) (C)--(H) (D)--(I) (E)--(J);
%\tkzLabelPoints[right](S,A,B,C,D,E,F,G,H,I,J)
\tkzLabelPoints[above](S)
\tkzLabelPoints[below](O)
%\tkzFillPolygon[pattern=north west lines](A,B,C,D,E,F)
\end{tikzpicture}
\end{document}
How can I reduce the lines?

Code: Select all

[code]\coordinate (A) at ($(O) + (360/10:{\R} and {\R})$);
\coordinate (B) at ($(O) + (2*360/10:{\R} and {\R})$);
\coordinate (C) at ($(O) + (3*360/10:{\R} and {\R})$);
\coordinate (D) at ($(O) + (4*360/10:{\R} and {\R})$);
\coordinate (E) at ($(O) + (5*360/10:{\R} and {\R})$);
\coordinate (F) at ($(O) + (6*360/10:{\R} and {\R})$);
\coordinate (G) at ($(O) + (7*360/10:{\R} and {\R})$);
\coordinate (H) at ($(O) + (8*360/10:{\R} and {\R})$);
\coordinate (I) at ($(O) + (9*360/10:{\R} and {\R})$);
\coordinate (J) at ($(O) + (10*360/10:{\R} and {\R})$);
[/code]
Attachments
ScreenHunter 53.png
ScreenHunter 53.png (64.68 KiB) Viewed 6774 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

How can I reduce my code?

Post by rais »

You could use a \foreach statement instead, say

Code: Select all

\foreach \a/\b in {A/1,B/2,C/3,D/4,E/5,F/6,G/7,H/8,I/9,J/10}
    \coordinate (\a) at ($(O) + (\b*36:{\R})$);
KR
Rainer
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I reduce my code?

Post by Tran Van Toan »

Thank you very much.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I reduce my code?

Post by Stefan Kottwitz »

Hi,

even simpler:

Code: Select all

\foreach \i in {1,...,10}
    \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:

Code: Select all

\makeatletter
\let\Alph\@Alph
\makeatother
Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I reduce my code?

Post by Tran Van Toan »

Thank you very much. I still a small problem:
How can I reduce this command

Code: Select all

\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: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I reduce my code?

Post by Stefan Kottwitz »

For example:

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

Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I reduce my code?

Post by Tran Van Toan »

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.

Code: Select all

\foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});
\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.

Code: Select all

\documentclass[border=2mm,tikz]{standalone}
\usepackage{fouriernc}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\usepackage{tkz-euclide,amsmath}
\usetkzobj{all}
\usepackage{pgfplots}
\makeatletter
\let\Alph\@Alph
\makeatother
\begin{document}

%polar coordinates of visibility
\pgfmathsetmacro\th{60}
\pgfmathsetmacro\az{130}
\tdplotsetmaincoords{\th}{\az}
%parameters of the cone
\pgfmathsetmacro\R{3}
\pgfmathsetmacro\v{5}
\begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]
\path
coordinate (O) at (0,0,0)
coordinate (S) at (0,0,\v);
\foreach \i in {1,...,10} \coordinate (\Alph{\i}) at ($(O) + (\i*36:{\R})$);
%\foreach \i in {1,...,10} \tkzDrawPoints[fill = black,size = 5 pt] (\Alph{\i})
\tkzDrawPoints[fill = black,size = 5 pt](S);
% % computation of tangential points
\pgfmathsetmacro\cott{{cot(\th)}}
\pgfmathsetmacro\fraction{\R*\cott/\v}
\pgfmathsetmacro\angle{{acos(\fraction)}}

% % angles for transformed lines
\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}

% % coordinates for transformed surface lines
\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}

% % angles for original surface lines
\pgfmathsetmacro\sinazp{{sin(\az-90)}}
\pgfmathsetmacro\cosazp{{cos(\az-90)}}
\pgfmathsetmacro\sinazm{{sin(90-\az)}}
\pgfmathsetmacro\cosazm{{cos(90-\az)}}

% % draw basis circle
\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}

% % displaying tranformed surface of the cone (rotated)
\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);

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

%\foreach \i in {1,...,10} \tkzLabelPoints[left](\Alph{\i});
\foreach \i in {1,...,4,8,9,10} \draw[thick] (S)--(\Alph{\i});
\foreach \i in {5,6,7} \draw[dashed] (S)--(\Alph{\i});
\tkzLabelPoints[above](S)
\tkzLabelPoints[below](O)
\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]
(S)--(O);
\end{tikzpicture}
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How can I reduce my code?

Post by Stefan Kottwitz »

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

Code: Select all

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

Stefan
LaTeX.org admin
Tran Van Toan
Posts: 23
Joined: Sat Oct 28, 2017 1:18 am

How can I reduce my code?

Post by Tran Van Toan »

Thank you very much.
Post Reply