答案1
您可以使用calc
图书馆。
\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}
答案2
该tkz-euclide
包具有许多用于制作几何构造的功能,包括根据两个先前定义的坐标制作一个正方形:
\tkzDefSquare(A,B)
\tkzGetPoints{C}{D}
第一行根据A
和定义正方形B
。第二行获取最后两个角的坐标,并创建命名坐标C
和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}
答案3
\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}