6

I would like to draw the following picture where I can color text, shapes and the lines. The shapes are triangles and stars.

enter image description here

So far, what I have done is

\documentclass[12pt]{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
\draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
\draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};

\coordinate (O) at (2,1);
\coordinate (A) at (2,2);
\coordinate (B) at (3,1.5);
\draw[line width=.2mm, fill=red] (O)--(A)--(B)--cycle;

\coordinate (O) at (1,1.8);
\coordinate (A) at (1,2.8);
\coordinate (B) at (2,2.3);
\draw[line width=.2mm, fill=black] (O)--(A)--(B)--cycle;

\end{tikzpicture}

\end{document}

So, the output is enter image description here

1
  • 5
    A down vote with no comment less than 5 minutes after the question was asked? I initially assumed the question was just a 'draw this for me' question as I couldn't think why else it would be immediately down voted. IMHO, this is completely uncalled for and in no way justified by the quality of the question.
    – cfr
    Sep 18, 2014 at 1:57

1 Answer 1

9

Adjust the stars as you wish:

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
  \begin{tikzpicture}
    [
      my star/.append style={star, draw, star points=4, minimum height=10mm, star point ratio=2.5}
    ]

    \draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
    \draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
    \draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};

    \coordinate (O) at (2,1);
    \coordinate (A) at (2,2);
    \coordinate (B) at (3,1.5);
    \draw[line width=.2mm, fill=red] (O)--(A)--(B)--cycle;

    \coordinate (O) at (1,1.8);
    \coordinate (A) at (1,2.8);
    \coordinate (B) at (2,2.3);
    \draw[line width=.2mm, fill=black] (O)--(A)--(B)--cycle;

    \foreach \i/\j/\k [count=\ijno] in {{6,-1}/blue/a,{1.5,5}/yellow/b,{2.5,4.5}/green/c,{4.5,4}/cyan/d,{5.5,3.5}/orange/e}
    \node (star \ijno) [my star, fill=\j, label=\k] at (\i) {};

    \path [draw] (1.5,2.3)
      edge node [left] {text} (star 2.center)
      edge node [below right] {text} (star 4.center);
    \path [draw] (2.5,1.5)
      edge node [right, pos=.75] {text} (star 3.center)
      edge node [below right] {text} (star 5.center)
      edge node [below left, pos=.6] {text} (star 1.center);

  \end{tikzpicture}

\end{document}

stars and triangles

Update

As Pier Paolo says, if you would like the lines to be drawn 'behind' the shapes, the easiest way is to draw the triangles as nodes and to drop the .center anchor whose sole purpose was to draw the lines in front of the star nodes.

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
  \begin{tikzpicture}
    [
      my star/.append style={star, draw, star points=4, minimum height=10mm, star point ratio=2.5},
      my triangle/.append style={isosceles triangle, draw, minimum height=10mm},
    ]

    \draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
    \draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
    \draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};

    \foreach \i/\j [count=\ijno] in {{1.5,2.3}/magenta,{2.5,1.5}/purple}
      \node (triangle \ijno) [my triangle, fill=\j] at (\i) {};

    \foreach \i/\j/\k [count=\ijno] in {{6,-1}/blue/a,{1.5,5}/yellow/b,{2.5,4.5}/green/c,{4.5,4}/cyan/d,{5.5,3.5}/orange/e}
      \node (star \ijno) [my star, fill=\j, label=\k] at (\i) {};

    \path [draw] (triangle 1)
      edge node [left] {text} (star 2)
      edge node [below right] {text} (star 4);
    \path [draw] (triangle 2)
      edge node [right, pos=.75] {text} (star 3)
      edge node [below right] {text} (star 5)
      edge node [below left, pos=.6] {text} (star 1);

  \end{tikzpicture}

\end{document}

lines behind shapes...

4
  • Sorry but how to draw the lines behind the shapes? the only thing I know is to place them in the code above the shapes but this works only with something of them. any suggestions?
    – CroCo
    Sep 18, 2014 at 4:27
  • 2
    @CroCo: you could redraw the triangles as nodes: \node (nameoftriangle) [draw,isosceles triangle] {}; (add the color and the dimensions at will between square brackets) and then simply draw: \draw (nameoftriangle) -- (nameofstar);
    – Pier Paolo
    Sep 18, 2014 at 7:54
  • 1
    @CroCo I'm not sure quite what you mean. In the image you posted, the lines appear in front of the shapes. In fact, I deliberately used <name of node>.center rather than <name of node> to achieve this for the stars. If you don't want that, get rid of the .center bits and make the triangles nodes as Pier Paolo suggests. Is that what you meant?
    – cfr
    Sep 18, 2014 at 14:22
  • @cfr, thank you so much. This is exactly what I'm seeking. and sorry for not being more clear in my picture.
    – CroCo
    Sep 19, 2014 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .