Using TikZ package, is there an easy way to generate the following diagram given in the link below?

I tried using \draw command to draw individual lines, but it becomes really painful to make things align. Could someone show me an elegant way?

No Geogebra is needed for this case. Here is a pure tikz solution which makes use of interpolated coordinates of the form ($(a)!factor!angle:(b)$) to find the corners of the squares.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\def\buildsquare#1#2{
\draw[red]
(#2) -- (#1) --
($(#1)!1!-90:(#2)$) coordinate (x) --
($(#2)!1!90:(#1)$) coordinate (y) -- cycle;
\draw[red,dashed] (#1) -- (y) (#2) -- (x);
\node[fill=blue,circle,minimum size=5pt, inner sep=0pt]
(#1#2) at ($(#1)!.5!(y)$) {};
}

\begin{document}
\begin{tikzpicture}
% You can place A, B, C and D at arbitrary places
\coordinate (A) at (0,0);
\coordinate (B) at (2,2);
\coordinate (C) at (.2,3.5);
\coordinate (D) at (-2,2.3);

\foreach \a/\b in {A/B, B/C, C/D, D/A}
\buildsquare{\a}{\b};
\draw[blue] (AB) -- (CD)
(BC) -- (DA);
\draw[ultra thick] (A)--(B)--(C)--(D)--cycle;
\end{tikzpicture}
\end{document}


Output:

# Update:

In the spirit of "Don't give a man a fish. Teach him how to fish instead", there is a little explanation of macro \buildsquare, which is obviously the core of this solution

The macro receives two arguments, which are the names of two nodes (in particular those are two corners of the initial quadrilaterlal. Its mission is to draw a square with one side being the line (#1) -- (#2).

The two remaining corners of this square can be computed from these two. In particular, the syntax: ($(#1)!1!-90:(#2)$) means "this is the point wich lies in a line which passes through (#1) (because #1 is the first coordinate in the expression), it is perpendicular to the line (#1)--(#2) (the angle -90 expresses this condition), and whose lenght is equal to the length of the line (#1)--(#2) (the !1! expresses this condition, since the number between ! is a multiplier for the length (#1)--(#2).

After computing this coordinate, I give it the name (x). In a similar way, the other corner is found with the expression ($(#2)!1!90:(#1)$) and it is given the name (y).

At same time than those coordinates are found, the square is drawn in red, all as part of the first \draw sentence.

The second \draw draws the diagonals of the square, in dashed style, using the previously computed coordinates (x) and (y).

Finally the \node sentence draws a blue dot in the center of the square (which is found as the middle point of a diagonal), and gives to this node the name (#1#2), so for example when we are drawing the square on the side (A)--(B), the corresponding blue dot for that side is named (AB).

The main picture simply contains a loop to build in sequence the squares on A-B, B-C, C-D and D-A. When the loop ends, we have new nodes named AB, BC, CD and DA in the center of each square, so the final step is only to connect two of those nodes.

# Update 2

I noticed that my drawing lacked the right angle mark at the intersection of the blue lines, so I added it. The angle is drawn by the following macro, which I find a bit too convoluted, but I was unable to find a simpler solution:

\def\markangle#1#2#3#4{ % (#1)--(#2) intersect (#3) -- (#4)
\path[name path=horizontal] (#1) -- (#2);
\path[name path=vertical] (#3) -- (#4);
\coordinate[name intersections={
of=horizontal and vertical,
by=center}] {};
\draw[blue] ($(center)!1ex!(#2)$) --
($(center)!1ex!(#2)!1ex!90:(#2)$) --
($(center)!1ex!(#4)$);
}


The macro receives four points, finds the intersection of the lines which connect those points ands draw two lines close of the intersection. The syntax ($(center)!1ex!(#2)!1ex!90:(#2)$) deserves an explanation since it chains two expressions. The first part ($(center)!1ex!(#2) finds a point located 1ex from (center) in the line (center)--(#2). The second part starts from this point and does !1ex!90:(#2)$), which is a point located at 1ex and 90 degrees from the previous computed point. This is the corner of the "right angle mark".

The complete code (I used different values for the corner A this time, just for fun):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,intersections}
\def\buildsquare#1#2{
\draw[red]
(#2) -- (#1) --
($(#1)!1!-90:(#2)$) coordinate (x) --
($(#2)!1!90:(#1)$) coordinate (y) -- cycle;
\draw[red,dashed] (#1) -- (y) (#2) -- (x);
\node[fill=blue,circle,minimum size=5pt, inner sep=0pt]
(#1#2) at ($(#1)!.5!(y)$) {};
}

\def\markangle#1#2#3#4{ % (#1)--(#2) intersect (#3) -- (#4)
\path[name path=horizontal] (#1) -- (#2);
\path[name path=vertical] (#3) -- (#4);
\coordinate[name intersections={
of=horizontal and vertical,
by=center}] {};
\draw[blue] ($(center)!1ex!(#2)$) --
($(center)!1ex!(#2)!1ex!90:(#2)$) --
($(center)!1ex!(#4)$);
}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-.3, 1);
\coordinate (B) at (2,2);
\coordinate (C) at (.2,3.5);
\coordinate (D) at (-2,2.3);

\foreach \a/\b in {A/B, B/C, C/D, D/A}
\buildsquare{\a}{\b};

\draw[blue] (AB) -- (CD)
(BC) -- (DA);
\draw[ultra thick] (A)--(B)--(C)--(D)--cycle;
\coordinate (center) at ($(AB)!.5!(CD)$);
\markangle{DA}{BC}{AB}{CD};
\end{tikzpicture}
\end{document}


The result :

• Simply wow! This is so incredibly good!!! – Prism Feb 24 '13 at 13:17
• @Prism: You should probably accept this answer instead of mine, it's a much more elegant solution. – Jake Feb 24 '13 at 19:55
• @Jake Yeah, I just have. :) – Prism Feb 25 '13 at 5:52
• @JLDiaz: Thank you so much for the code and very thorough explanation. Not only you are a tikz master, but also a great teacher. Cheers! – Prism Feb 25 '13 at 5:53

Geogebra is a really nice tool for graphically developing geometry diagrams, which you can export as TikZ code. The generated code is actually pretty good, and being able to define the geometric relationships using a GUI is almost certainly faster than hand-coding:

The TikZ code generated by Geogebra:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\definecolor{qqwuqq}{rgb}{0,0.39,0}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\definecolor{zzttqq}{rgb}{0.6,0.2,0}
\definecolor{qqqqff}{rgb}{0,0,1}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=0.7cm,y=0.7cm]
\clip(-4.86,-5.02) rectangle (13.06,10.3);
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.1] (1.75,3.14) -- (6.4,3.59) -- (5.96,8.24) -- (1.3,7.79) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.1] (6.4,3.59) -- (7.69,0.2) -- (11.08,1.49) -- (9.79,4.87) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.1] (7.69,0.2) -- (3.45,0.61) -- (3.04,-3.64) -- (7.28,-4.05) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.1] (3.45,0.61) -- (1.75,3.14) -- (-0.78,1.44) -- (0.91,-1.09) -- cycle;
\draw[color=qqwuqq,fill=qqwuqq,fill opacity=0.1] (5.09,1.79) -- (5,2.22) -- (4.58,2.13) -- (4.67,1.71) -- cycle;
\draw (1.75,3.14)-- (3.45,0.61);
\draw (3.45,0.61)-- (7.69,0.2);
\draw (7.69,0.2)-- (6.4,3.59);
\draw (6.4,3.59)-- (1.75,3.14);
\draw [color=zzttqq] (1.75,3.14)-- (6.4,3.59);
\draw [color=zzttqq] (6.4,3.59)-- (5.96,8.24);
\draw [color=zzttqq] (5.96,8.24)-- (1.3,7.79);
\draw [color=zzttqq] (1.3,7.79)-- (1.75,3.14);
\draw [color=zzttqq] (6.4,3.59)-- (7.69,0.2);
\draw [color=zzttqq] (7.69,0.2)-- (11.08,1.49);
\draw [color=zzttqq] (11.08,1.49)-- (9.79,4.87);
\draw [color=zzttqq] (9.79,4.87)-- (6.4,3.59);
\draw [color=zzttqq] (7.69,0.2)-- (3.45,0.61);
\draw [color=zzttqq] (3.45,0.61)-- (3.04,-3.64);
\draw [color=zzttqq] (3.04,-3.64)-- (7.28,-4.05);
\draw [color=zzttqq] (7.28,-4.05)-- (7.69,0.2);
\draw [color=zzttqq] (3.45,0.61)-- (1.75,3.14);
\draw [color=zzttqq] (1.75,3.14)-- (-0.78,1.44);
\draw [color=zzttqq] (-0.78,1.44)-- (0.91,-1.09);
\draw [color=zzttqq] (0.91,-1.09)-- (3.45,0.61);
\draw (3.85,5.69)-- (5.36,-1.72);
\draw (1.33,1.03)-- (8.74,2.54);
\begin{scriptsize}
\fill [color=qqqqff] (1.75,3.14) circle (1.5pt);
\fill [color=qqqqff] (6.4,3.59) circle (1.5pt);
\fill [color=qqqqff] (7.69,0.2) circle (1.5pt);
\fill [color=qqqqff] (3.45,0.61) circle (1.5pt);
\fill [color=uququq] (5.96,8.24) circle (1.5pt);
\fill [color=uququq] (1.3,7.79) circle (1.5pt);
\fill [color=uququq] (11.08,1.49) circle (1.5pt);
\fill [color=uququq] (9.79,4.87) circle (1.5pt);
\fill [color=uququq] (3.04,-3.64) circle (1.5pt);
\fill [color=uququq] (7.28,-4.05) circle (1.5pt);
\fill [color=uququq] (-0.78,1.44) circle (1.5pt);
\fill [color=uququq] (0.91,-1.09) circle (1.5pt);
\fill [color=uququq] (3.85,5.69) circle (1.5pt);
\fill [color=uququq] (8.74,2.54) circle (1.5pt);
\fill [color=uququq] (5.36,-1.72) circle (1.5pt);
\fill [color=uququq] (1.33,1.03) circle (1.5pt);
\fill [color=uququq] (4.67,1.71) circle (1.5pt);
\end{scriptsize}
\end{tikzpicture}
\end{document}

• Notice that (at least in my version of GeoGebra, which came with Ubuntu 12.04), the resulting tikz code sometimes is broken (notably when using circle arcs - a unicode "pi" symbol appears in polar coordinates, which is obviously garbage for tikz). Apart from that, GG is a neat idea. – mbork Feb 24 '13 at 12:44
• Thanks Jake! This looks really great. I will definitely look up Geogebra from now on. I would like to ask just one more thing: Could you tell me how to add labels to the nodes? It seems like one needs to add extra parameter to the line \fill [color=qqqqff] (1.75,3.14) circle (1.5pt); but I am unaware of how to do that. Thanks again. – Prism Feb 24 '13 at 12:45
• @Prism: You can add the labels within Geogebra itself and then export it, that's easier than adjusting the resulting code. – Jake Feb 24 '13 at 12:55
• @Jake Sweet! Will do that! :) – Prism Feb 24 '13 at 12:56
• +1 for teaching me something; I did not know GeoGebra had TikZ export functionality! – Jonathan Landrum Jun 24 '15 at 14:06

It's relatively easy to draw this picture with tkz-euclide

\documentclass{article}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-.3, 1);
\coordinate (B) at (2,2);
\coordinate (C) at (.2,3.5);
\coordinate (D) at (-2,2.3);

\foreach \a/\b/\c in {A/B/E, B/C/F, C/D/G, D/A/H}{
\tkzDefSquare(\b,\a)
\tkzDrawPolygon[color=red](\b,\a,tkzFirstPointResult,tkzSecondPointResult)
\tkzDrawSegments[dashed,red](\b,tkzFirstPointResult \a,tkzSecondPointResult)
\tkzDefBarycentricPoint(\b=1,\a=1,tkzFirstPointResult=1,tkzSecondPointResult=1)
% \tkzInterLL is possible
\tkzGetPoint{\c}
\tkzDrawPoint(\c)
}%
\tkzDrawSegments[blue](E,G F,H);
\tkzDrawPolygon[ultra thick](A,B,C,D)
\tkzInterLL(E,G)(F,H)        \tkzGetPoint{I}
\tkzMarkRightAngle[color=blue,fill=blue!20,thick](E,I,F)
\end{tikzpicture}
\end{document}