4

I know that I can draw a rectangle with tikz using \draw (0.0,0.0) rectangle (1.0,1.0);. Is there a possibility to determine all 4 edge coordinates directly from the shape?

I do can save the lower left coordinate and the upper right by \draw (0.0,0.0) coordinate (lb) rectangle (1.0,1.0) coordinate (ru);. But is there a way to get the upper left and bottom right corner directly from the shape?

I know I can calculate them. But are they directly accessible from the shape?


MWE

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

% the rectangle
\draw (0.0,0.0) coordinate (lb) rectangle (1.0,1.0) coordinate (ru);

% coordinates
\draw (lb) circle [radius=2pt];
\draw (ru) circle [radius=2pt];

% calculated coordinates
\draw[dashed] (lb |- ru) coordinate (lu) circle [radius=2pt];
\draw[dashed] (lb -| ru) coordinate (rb) circle [radius=2pt];

\end{tikzpicture}

\end{document}
0

2 Answers 2

3

You could define your own rectangle path. In the MWE

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[my rectangle/.style={to path={
-| coordinate[pos=0.5] (#1-2) (\tikztotarget) coordinate (#1-3) 
-| coordinate (#1-4) (\tikztostart) coordinate (#1-1) }}]
\draw (0,0) edge[my rectangle=krtek] (1,1);
\foreach \X in {1,...,4}
{\draw (krtek-\X) circle [radius=2pt];}
\end{tikzpicture}
\end{document}

the style my rectangle=<coordinate base name> will give the four corners the names coordinate base name-1, ... , coordinate base name-4.

enter image description here

And there are, of course, predefined shapes that have the corner coordinates stored in anchors.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\usetikzlibrary{shapes.geometric}
\begin{tikzpicture}

% the rectangle
\path (0,0) node[draw,anchor=south west,minimum size=1cm] (R) {};
\foreach \X in {45,135,225,315}
{\draw (R.\X) circle [radius=2pt];}

\path (2,0) node[draw,anchor=south west,minimum size={sqrt(2)*1cm},regular polygon,regular
polygon sides=4] (poly) {};

\foreach \X in {1,...,4}
{\draw (poly.corner \X) circle [radius=2pt];}

\end{tikzpicture}

\end{document}

enter image description here

2

If you don't want to explicitely declare coordinates for the corners, you can automatically convert your rectangle into a node with fit library. This way, the resultant node gives you all rectangular anchors without havint to name them.

Following example shows how to insert rectangular coordinates into fit parameter. The result node is equivalent to previously drawn rectangle (you don't need to draw the previous rectangle, it's just for demonstration purposes).

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

\draw (0,0) rectangle (2,1);
\node[fit={(0,0) (2,1)}, inner sep=0pt, draw=red, opacity=.5] (a) {};

\foreach \i in {north east, north west, south east, south west}
    \draw (a.\i) circle(2pt);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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