# Draw a square with given points

Given points A and B, I want to draw a square with vertices A and B.

Of course, there are two possible squares along with a segment AB.

First, I thought that it could be drawn by using "rotate around={90:(A)}". This works well for moving a point, but it is not good for drawing a tilted square.

Using a perpendicular line through B is another way. But, it requires the length of AB. So, it makes too complicated codes.

I think there is a simple method to draw a square with given vertices. Could you let me know?

You cal use the calc library.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,1);
\fill (A) circle (0.1);
\fill (B) circle (0.1);
\draw (A) node[below] {A} -- (B) node[below] {B} -- ($(B)!1!-90:(A)$) -- ($(A)!1!90:(B)$) -- cycle;
\end{tikzpicture}
\end{document}


The tkz-euclide package has a lot of features for making geometric constructions, including making a square from two previously defined coordinates:

\tkzDefSquare(A,B)
\tkzGetPoints{C}{D}


The first line defines the square based on A and B. The seconds gets the coordinates of the last two corners, and creates named coordinates C and D.

\documentclass[border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3,1);

\tkzDefSquare(A,B)
\tkzGetPoints{C}{D}

\tkzDrawPoints[size=8](A,B,C,D)
\tkzDrawPolygon(A,B,C,D)
\end{tikzpicture}
\end{document}


Something like this? tikz/turn is a good choice.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand { \mysquare } { s O{} D(){a} D(){b} }
{
\tikz@scan@one@point\pgf@process(#3)
\pgf@xa = \pgf@x
\pgf@ya = \pgf@y
\tikz@scan@one@point\pgf@process(#4)
\pgfmathsetmacro{\square@l}{
sqrt( (\pgf@x - \pgf@xa)^2 + (\pgf@y - \pgf@ya)^2 )
}
\def\square@sign{}
\IfBooleanT { #1 } { \def\square@sign{-} }
\draw[#2] (#3) -- (#4)
-- ([turn]\square@sign 90:\square@l pt)
-- ([turn]\square@sign 90:\square@l pt)
-- cycle;
}
\makeatother

\begin{document}
\begin{tikzpicture}
\coordinate [label=below left:$A$] (a) at (0, 0);
\coordinate [label=right:$B$] (b) at (2, 3);
\coordinate [label=above:$C$] (c) at (2, 1);
\mysquare[thick](a)(b)
\mysquare*[thick, red](a)(c)
\end{tikzpicture}
\end{document}