What I am trying to do is to create an image such as that below, that is, triangles inside triangles. I can get a single triangle created and I commented out the parts that create circles (for a Venn diagram, the original code was found here), but I am not sure how to create midpoints on each of the sides and connect those midpoints.
While I am only looking for depth two (the image is depth 3), I figure it would be useful to figure out how to do it programmatically so as to create something such as a fractal. I am also wanting to label the sides – at least for the first and second depth guessing that there would have to be some kind of if structure. Any ideas?
\documentclass[tikz,border=10pt]{standalone}
\begin{tikzpicture}
\node (tri) [regular polygon, regular polygon sides=3, draw, densely dashed, minimum width=50mm] {};
% \foreach \i/\j in {1/2,2/3,3/1}
% {
% \node [draw] at (tri.corner \i) [circle through={($(tri.corner \i)!1/2!(tri.corner \j)$)}, draw] {};
% \path [fill] ($(tri.corner \i)!1/2!(tri.corner \j)$) circle (2.5pt);
% }
\end{document}
EDIT!
Using a small piece (not employing the arbitrary depth) of Tom Bombadil's extra-ordinary answer I added the labels for the sides in which I sought. The reason I added it is note that each of the three 'sides' of the largest (outer) triangle is really 6 sides – that is, instead of for example (A) — (B) there is a coordinate (D) half way in between (A) and (B), so each of the three sides is halved to make 6 sides. While my problem is solved in this manner, I thought that someone might be able to modify Tom's code for labeling to support this ability. My labeling of a simple triangle inside triangle from Tom's code is:
\documentclass[tikz,border=10pt]{standalone}
\begin{figure}
\begin{tikzpicture}
\coordinate [label=right:$Y \wedge \neg (X \vee Z)$] (A) at (-30:5);
\coordinate [label=above:$X \wedge \neg (Y \vee Z)$] (B) at (90:5);
\coordinate [label=left:$Z \wedge \neg (X \vee Y)$] (C) at (210:5);
% $\draw (A) -- (B) -- (C) -- cycle;
\coordinate [label=right:$X \wedge Y \wedge \neg Z$] (D) at ($(A)!0.5!(B)$);
\coordinate [label=left:$X \wedge Z \wedge \neg Y$] (E) at ($(B)!0.5!(C)$) ;
\coordinate [label=below:$Y \wedge Z \wedge \neg X$] (F) at ($(C)!0.5!(A)$);
\draw (B) -- (D) node[right] at ( $ (B)!0.5!(D) $) (a) {$X \wedge \neg Z$};
\draw (B) -- (E) node[left] at ( $ (B)!0.5!(E) $) (a) {$X \wedge \neg Y$};
\draw (E) -- (C) node[left] at ( $ (E)!0.5!(C) $) (a) {$Z \wedge \neg Y$};
\draw (C) -- (F) node[below] at ( $ (C)!0.3!(F) $) (a) {$Z \wedge \neg X$};
\draw (F) -- (A) node[below] at ( $ (F)!0.7!(A) $) (a) {$Y \wedge \neg X$};
\draw (A) -- (D) node[right] at ( $ (A)!0.5!(D) $) (a) {$Y \wedge \neg Z$};
% \draw (D) -- (E) -- (F) -- cycle;
\draw (D) -- (E) node[above] at ( $ (E)!0.5!(D) $) (a) {$X \wedge (Y \Updownarrow Z)$};
\draw (E) -- (F) node[left] at ( $ (E)!0.65!(F) $) (b) {$Z \wedge (X \Updownarrow Y)$};
\draw (F) -- (D) node[right] at ( $ (F)!0.35!(D) $) (c) {$Y \wedge (X \Updownarrow Z)$};
\end{tikzpicture}
\end{figure}
Best Answer
My solution does the same as percusses, but probably is a bit easier to read. It uses the
calc
library to compute the midpoints of the edges, and then renames the coordinates appropriately.Code
Output
Edit 1: Generalized for regular n-Gons, where the meeting point is variable and color fill is added:
Code
Output
Edit 2: And of cause, one can produce silly animations! The following
converted with ImageMagick's
convert -loop 0 -delay 5 -dispose previous -density 50 swirl.pdf swirl.gif
produces this:Edit 3: For labeling the sides, here's how on could go about this. As this command now has the maximum of 9 parameters, switching to some key-value mechanism like
pgfkeys
is advised. If the "upside down" text is considered bad, remove theallow upside down
option, but then the labels are not always placed on the "right side" (outside) of the triangle.Code
Output
Edit 4: Here's a variant for giving two labels to all but the inner iteration.
Code
Output
Edit 5: If you don't like the
sloped
style, here's how you can do it via computing appropriate angles. It seems unneccessarily complicated to first define atemp
coordinate and then placing a node at the fitting angle, but using thepos
andlabel
options of a node together does not seem to work, as the node is then always placed at the end.Code
Output
Edit 6: Now it should work to place labels of arbitrary width (if the triangle is big enough).
Code
Output
Edit 7: I switched to pgfkeys so that one has named parameters and can also have more than nine.
Code
Output