1

I am trying to draw this picture enter image description here

I tried with pgfplots.

\documentclass[border=1.5mm,12pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis line style = very thick,
axis lines = center,
xlabel=$x$, ylabel=$y$,
xtick           = {-1,0,...,4},
ytick           = {-1,0,...,4},
xmin       = -0.5,   xmax = 5.5,
ymin       =-0.5,     ymax = 4.5,
]      
\node at (axis cs:-0.25, -0.25) {$A$};
\node at (axis cs: 4.25, 0.25) {$B$};
\node at (axis cs: 4, 3.25) {$C$};
\node at (axis cs: 0.15, 3.25) {$D$};
\node at (axis cs: 0.2, 1.2) {$M$};
\node at (axis cs: 3, 3.25) {$K$};
\addplot[very thick,mark=*] coordinates {
    (4 ,0) (4,3) (0,3) (0,0)  (0,1)
};
\addplot[mark=*,very thick] coordinates {
    (0,0)  (3,3)};
\addplot[very thick,mark=*] coordinates {
    (4,0)  (0,1)};     
\end{axis}
\end{tikzpicture}
\end{document}

How to draw it with TikZ?

2 Answers 2

8

If you place first all the coordinates, the rest is pretty straightforward.

Something like this:

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

\begin{document}
\begin{tikzpicture}
  % coordinates
  \coordinate (A) at (0,0);
  \coordinate (B) at (4,0);
  \coordinate (C) at (4,3);
  \coordinate (D) at (0,3);
  \coordinate (K) at (3,3);
  \coordinate (M) at (0,1);
  % axes
  \draw[-latex] (-0.5,0) --++ (5.4,0) node [above] {$x$};
  \draw[-latex] (0,-0.5) --++ (0,5.4) node [right] {$y$};
  \foreach\i in {1,...,4}
  {
    \draw (\i,-0.1) --++ (0,0.2) node [yshift=-4mm] {$\i$};
    \draw (-0.1,\i) --++ (0.2,0) node [xshift=-4mm] {$\i$};
  }
  % lines
  \draw[thick] (A) rectangle (C);
  \draw[thick] (A) -- (K);
  \draw[thick] (B) -- (M);
  % points
  \fill (A) circle (1.5pt) node [below left] {$A$};
  \foreach\i in {B,C,D,K,M}
    \fill (\i) circle (1.5pt) node [above right] {$\i$};
\end{tikzpicture}
\end{document}

enter image description here

4
  • What is the meaning of ++ in \draw[-latex] (-0.5,0) --++ (5.4,0) node [above] {$x$};? Commented Oct 9, 2021 at 10:42
  • 1
    @JoséCarlosSantos, it's a relative coordinate. We draw a line from (-0.5,0) to the point (4.9,0) which is 5.4cm to the right. Commented Oct 9, 2021 at 10:46
  • One + is enough ^^
    – Black Mild
    Commented Oct 9, 2021 at 10:57
  • @BlackMild, yep, you're right. In this case it's the same one or too. Commented Oct 9, 2021 at 11:08
2

An approximate Asymptote version of Juan Castaño's answer.

Compile on http://asymptote.ualberta.ca/

unitsize(cm);

// coordinates
pair A=(0,0),B=(4,0),C=(4,3),D=(A.x,C.y);
pair M=(0,1),K=(3,3);
// axes
draw(Label("$x$",Relative(0.98),LeftSide),(-0.5,0)--(5.4,0),Arrow(TeXHead));
draw(Label("$y$",Relative(0.98)),(0,-0.5)--(0,5.4),Arrow(TeXHead));
for (int i : new int[]{1,2,3,4})
{
  draw(Label((string) i,BeginPoint),(i,-0.1)--(i,0.1));
  draw(Label((string) i,BeginPoint),(-0.1,i)--(0.1,i));
}
// lines
draw(box(A,C));
draw(A--K);
draw(B--M);

dot("$A$",A,dir(-135));
dot("$B$",B,dir(45));
dot("$C$",C,dir(45));
dot("$D$",D,dir(45));
dot("$K$",K,dir(45));
dot("$M$",M,dir(45));

You must log in to answer this question.

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