jedes zweite oder x-te Element einer Foreach-Schleife wählen

Antwort erstellen


Diese Frage dient dazu, das automatisierte Versenden von Formularen durch Spam-Bots zu verhindern.
Smilies
:D :) :( :o :shock: :? 8) :lol: :-x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:

BBCode ist eingeschaltet
[img] ist eingeschaltet
[flash] ist ausgeschaltet
[url] ist eingeschaltet
Smilies sind eingeschaltet

Die letzten Beiträge des Themas

Ich habe die Datenschutzerklärung gelesen und bin damit einverstanden.

   

Wenn du eine Datei oder mehrere Dateien anhängen möchtest, gib die Details unten ein.

Ansicht erweitern Die letzten Beiträge des Themas: jedes zweite oder x-te Element einer Foreach-Schleife wählen

von Beinschuss » Fr 29. Sep 2017, 21:33

Das nenne ich mal eine sparsame, elegante und durchdachte Lösung des Problems. Alle Achtung!

von cgnieder » Fr 29. Sep 2017, 14:59

Wenn es nur um zwei unterschiedliche Farben geht: das hier macht jede dritte Kugel rot, die restlichen weiß:
\foreach \x in {1,...,6} 
\foreach \y in {1,...,6}
\foreach \z in {1,...,6}
\pgfmathparse{int(mod(\x+\y+\z,3))}% mod 3 => every third
\edef\mycolor{\ifnum\pgfmathresult=0 red\else white\fi}
\draw [ball color = \mycolor] (\x,\y,\z) circle (0.5cm);
Abwechselnd geht sogar noch einfacher:
\foreach \x in {1,...,6} 
\foreach \y in {1,...,6}
\foreach \z in {1,...,6}
\draw [ball color = \ifodd\numexpr\x+\y+\z\relax red\else white\fi] (\x,\y,\z) circle (0.5cm);
Bild
Dateianhänge
test.png
test.png (135.86 KiB) 3258 mal betrachtet

von Elbflorentiner » Fr 29. Sep 2017, 12:08

Wow, vielen Dank für eure Mühe.

Im Wesentlichen ist es der Workaround von Ulrike, der sämtliche meiner Fragen löst.

Toll. :D

von Beinschuss » Do 28. Sep 2017, 22:57

Für einen Würfel mit ungerader Kantenlänge kann man meinen Vorschlag deutlich abspecken:
\documentclass[]{standalone} 
\usepackage[utf8]{inputenc} 
\usepackage[ngerman]{babel} 
\usepackage{tikz} 
\usetikzlibrary{3d} 
\usepackage{xparse} 

\ExplSyntaxOn 
\seq_new:N\g_colorlist_seq 
\seq_set_from_clist:Nn\g_colorlist_seq{blue,red} 

\newcommand\currentballbr{} 

\NewDocumentCommand\getnextballbr {} { 
\seq_gpop_left:NN\g_colorlist_seq\currentballbr 
\seq_gput_right:NV\g_colorlist_seq\currentballbr 
}
\ExplSyntaxOff 

\begin{document} 
\begin{tikzpicture}[x={(-0.5cm,-0.5cm)},y={(1cm,0cm)}, z={(0cm,1cm)}] 
\draw[->, >=latex] (0,0,0) -- (7,0,0) node[left]{x}; 
\draw[->, >=latex] (0,0,0) -- (0,7,0) node[below]{y}; 
\draw[->, >=latex] (0,0,0) -- (0,0,7) node[left]{z}; 
%%Kugeln 
\foreach \x in {1,...,5} 
\foreach \y in {1,...,5} 
\foreach \z in {1,...,5} 
{\getnextballbr \draw [ball color = \currentballbr] (\x,\y,\z) circle (0.5cm);} 
\end{tikzpicture} 
\end{document}
Bei gerader Kantenlänge jedoch ergibt sich hierbei nicht das gewünschte Muster.

von Beinschuss » Do 28. Sep 2017, 21:34

Ein interessantes Problem und ein gutes Beispiel, an dem ich etwas weiter gebastelt habe. Wie wäre es hiermit?
\documentclass[]{standalone} 
\usepackage[utf8]{inputenc} 
\usepackage[ngerman]{babel} 
\usepackage{tikz} 
\usetikzlibrary{3d} 
\usepackage{xparse} 

\ExplSyntaxOn 
\seq_new:N\g_colorlist_seq 
\seq_set_from_clist:Nn\g_colorlist_seq{blue,red} 
\seq_new:N\g_colorrb_seq 
\seq_set_from_clist:Nn\g_colorrb_seq{red,blue}

\newcommand\currentballbr{} 
\newcommand\currentballrb{}

\NewDocumentCommand\getnextballbr {} { 
\seq_gpop_left:NN\g_colorlist_seq\currentballbr 
\seq_gput_right:NV\g_colorlist_seq\currentballbr 
}
\NewDocumentCommand\getnextballrb {} { 
	\seq_gpop_left:NN\g_colorrb_seq\currentballrb 
	\seq_gput_right:NV\g_colorrb_seq\currentballrb 
}
 
\ExplSyntaxOff 
\begin{document} 
\begin{tikzpicture}[x={(-0.5cm,-0.5cm)},y={(1cm,0cm)}, z={(0cm,1cm)}] 
\draw[->, >=latex] (0,0,0) -- (7,0,0) node[left]{x}; 
\draw[->, >=latex] (0,0,0) -- (0,7,0) node[below]{y}; 
\draw[->, >=latex] (0,0,0) -- (0,0,7) node[left]{z}; 
%%Kugeln 
\foreach \x in {1,...,6} 
\foreach \y in {1,...,6} 
\foreach \z in {1,...,3} 
{\getnextballbr \draw [ball color = \currentballbr] (\x,\y,\z) circle (0.5cm);} 

\foreach \x in {1,...,6} 
\foreach \y in {1,...,6} 
\foreach \z in {4,...,6} 
{\getnextballrb \draw [ball color = \currentballrb] (\x,\y,\z) circle (0.5cm);} 

%%obere Fläche 
\begin{scope}[canvas is xy plane at z=6] 
% x-axis 
\draw[fill = gray!60, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle; 
\end{scope} 
%%vordere Fläche 
\begin{scope}[canvas is yz plane at x=6] 
% x-axis 
\draw[fill = gray!40, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle; 
\end{scope} 
\end{tikzpicture} 
\end{document} 

von u_fischer » Do 28. Sep 2017, 20:37

Ich weiß, nach welchem System du die Farben wechseln willst, aber du kannst natürlich innerhalb der Schleife z.B. durch eine Liste wandern:
\documentclass[]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{3d}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N\g_colorlist_seq
\seq_set_from_clist:Nn\g_colorlist_seq{blue,red,green,yellow,brown}

\newcommand\currentballcolor{}

\NewDocumentCommand\getnextballcolor {} {
 \seq_gpop_left:NN\g_colorlist_seq\currentballcolor
 \seq_gput_right:NV\g_colorlist_seq\currentballcolor
}
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}[x={(-0.5cm,-0.5cm)},y={(1cm,0cm)}, z={(0cm,1cm)}]
\draw[->, >=latex] (0,0,0) -- (7,0,0) node[left]{x};
\draw[->, >=latex] (0,0,0) -- (0,7,0) node[below]{y};
\draw[->, >=latex] (0,0,0) -- (0,0,7) node[left]{z};
%%Kugeln
\foreach \x in {1,...,6}
\foreach \y in {1,...,6}
\foreach \z in {1,...,6}
{\getnextballcolor \draw [ball color = \currentballcolor] (\x,\y,\z) circle (0.5cm);}

%%obere Fläche
\begin{scope}[canvas is xy plane at z=6]
  % x-axis
  \draw[fill = gray!60, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle;
\end{scope}
%%vordere Fläche
\begin{scope}[canvas is yz plane at x=6]
  % x-axis
  \draw[fill = gray!40, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document} 

von Gast » Do 28. Sep 2017, 18:40

Verstehe ich nicht ganz. Meinst du so:
\foreach \x in {1,3,5} 
\foreach \y in {2,4,6}
\foreach \z in {1,3,5}
\draw [ball color = gray!40] (\x,\y,\z) circle (0.5cm);

jedes zweite oder x-te Element einer Foreach-Schleife wählen

von Elbflorentiner » Do 28. Sep 2017, 16:42

Hallo,

ich möchte mit TIKZ eine 3D-Zeichnung einer Kugelanordnung erstellen. Dies ist mir auch gelungen. Nun möchte ich jedoch nur jedes zweite Element der Foreach-Schleife wählen, um die Kugeln abwechseln verschiedenfarbig darstellen zu können. Bisher brachte mir die Recherche keine Lösung.

Ich möchte, das TIKZ nur jede zweite Kugel zeichnet, d. h. dass in der ersten Reihe folgendes Muster (1=Kugel) liegt: 1,0,1,0,1,0. Für Reihe zwei dann 0,1,0,1,0,1 usw.

Mein Minimalbespiel:
\documentclass[]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{3d}

\begin{document}
\begin{tikzpicture}[x={(-0.5cm,-0.5cm)},y={(1cm,0cm)}, z={(0cm,1cm)}]
\draw[->, >=latex] (0,0,0) -- (7,0,0) node[left]{x};
\draw[->, >=latex] (0,0,0) -- (0,7,0) node[below]{y};
\draw[->, >=latex] (0,0,0) -- (0,0,7) node[left]{z};
%%Kugeln 
\foreach \x in {1,...,6} 
\foreach \y in {1,...,6}
\foreach \z in {1,...,6}
\draw [ball color = gray!40] (\x,\y,\z) circle (0.5cm);

%%obere Fläche
\begin{scope}[canvas is xy plane at z=6]
  % x-axis
  \draw[fill = gray!60, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle;  
\end{scope}
%%vordere Fläche
\begin{scope}[canvas is yz plane at x=6]
  % x-axis
  \draw[fill = gray!40, opacity = 0.4] (0,0) -- (6,0) -- (6,6) -- (0,6) -- cycle;  
\end{scope}
\end{tikzpicture}
\end{document}
Über Tips und Hinweise freue ich mich sehr.

Elmar

Nach oben