4

I have four given points, for example

A(3,2)
B(5,4)
C(-3, 4)
D(-3, 0)

Using TikZ/PgfPlots, how can I draw them in the Cartesian coordinate system, show the points, each accompanied by its name (A,B,C,D) and then draw the sides of the quadrilateral ABCD?

I'm new to TikZ :)

8

Below I include two options: the first one uses "pure" TikZ and the second one uses the great tkz-euclide package.

One possibility (the code has explanatory comments):

\documentclass{article} 
\usepackage{tikz}

\tikzset{
smalldot/.style={
  circle,
  fill,
  inner sep=1.5pt
  }
}

\begin{document} 

\begin{tikzpicture}
% The x-axis
\draw[->] 
  (-3.5,0) -- (5.5,0);
% The y-axis
\draw[->] 
  (0,-0.5) -- (0,4.5);
% Ticks for x-axis
\foreach \Valor in {-3,...,5}
  \draw (\Valor,2pt) -- (\Valor,-2pt) node[below] {\Valor};
% Ticks for y-axis
\foreach \Valor in {1,...,4}
  \draw (-2pt,\Valor) node[left] {\Valor} -- (2pt,\Valor);
% define coordinates at the desired points
\path
  coordinate (A) at (3,2)
  coordinate (B) at (5,4)
  coordinate (C) at (-3,4)
  coordinate (D) at (-3,0);
% draw lines between the coordinates  
\draw (A) -- (B) -- (C) -- (D) -- cycle;
% Label the points
\foreach \Nombre/\Pos in {A/right,B/right,C/left,D/left}
{
  \node[\Pos] at (\Nombre) {$\Nombre$};
  \node[smalldot] at (\Nombre) {};
}
\end{tikzpicture}

\end{document}

enter image description here

If you are starting with TikZ, I'd like to suggest you the tkz-euclide package which is built on top of TikZ and has a more intuitive series of commands and some predefined features which could help you to easily draw euclidean figures (compare the code below with the one using "pure" TikZ):

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

\begin{document} 

\begin{tikzpicture}
\tkzInit[xmax=5,xmin=-4,ymax=4,ymin=-1]
\tkzAxeXY
\tkzGrid
\tkzDefPoint[label=right:$A$](3,2){A}
\tkzDefPoint[label=right:$B$](5,4){B}
\tkzDefPoint[label=left:$C$](-3,4){C}
\tkzDefPoint[label=left:$D$](-3,0){D}
\tkzDrawSegments[color=red!70!black,line width=1pt](A,B B,C C,D D,A)
\tkzDrawPoints[color=red!70!black](A,B,C,D)
\end{tikzpicture}

\end{document}

enter image description here

2
  • The solution is great and very intuitive. But in my real example, I need a much bigger range of x, y variables. If I do \tkzInit[xmax=10,xmin=-10,ymax=10,ymin=-10] then the produced diagram is too big, so it goes out of the page range. How can I scale it to the page size?
    – marmistrz
    Mar 7 '15 at 15:56
  • 1
    @marmistrz add scale=<factor> and possibly also transform shape to the tikzpicture options, as in \begin{tikzpicture}[scale=0.5,transform shape]...\end{tikzpicture} (to scale it down 50%). By the way, don't forget that now that you have several answers, you can accept the one you consider best solved your problem by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. Mar 7 '15 at 16:00
8

With pgfplots

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,enlargelimits,
    xlabel=$x$,ylabel=$y$,nodes near coords]
\addplot+[point meta=explicit symbolic] coordinates{
(3,2)  [A]
(5,4)  [B]
(-3, 4)[C]
(-3, 0)[D]
}; % Add "-- cycle" if you want to close the drawing
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

0
4

Now with MetaPost (and LuaLaTeX).

\documentclass[12pt, border = 5bp]{standalone}
\usepackage{luamplib}
    \mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
    u = 1.5cm; len := 3bp;
    xmin := -3.5u; xmax := 5.75u; ymin := -.5u; ymax := 4.75u;
    pair A, B, C, D; A = u*(3,2); B = u*(5,4); C = u*(-3, 4); D = u*(-3,0);
    beginfig(1);
        labeloffset := 5bp;
        for i = -3 upto 5:
            if i <> 0: draw (i*u, -len) -- (i*u, len); 
                label.bot("$" & decimal i & "$", (i*u, 0)); fi
        endfor
        for j = 1 upto 4:
            draw (-len, j*u) -- (len, j*u);
            label.lft("$" & decimal j & "$", (0, j*u));
        endfor
        drawarrow (xmin, 0) -- (xmax, 0); drawarrow (0, ymin) -- (0, ymax);
        draw A -- B -- C -- D -- cycle; 
        labeloffset := 3bp;
        dotlabel.rt("$A$", A); dotlabel.rt("$B$", B);
        dotlabel.lft("$C$", C); dotlabel.ulft("$D$", D);
        label.llft("$O$", origin); label.bot("$x$", (xmax, 0)); label.lft("$y$", (0, ymax));
    endfig;
\end{mplibcode}
\end{document}

enter image description here

While I'm at it: another, with the mfpic package:

\documentclass[12pt]{scrartcl}
\usepackage[metapost]{mfpic}
    \setlength{\mfpicunit}{1cm}
\opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[1.5]{-3.5}{5.75}{-0.5}{4.75}
    \pointdef{A}(3, 2)
    \pointdef{B}(5, 4)
    \pointdef{C}(-3, 4)
    \pointdef{D}(-3, 0)
    \polygon{\A, \B, \C, \D}
    \point{\A, \B, \C, \D}
    \doaxes{xy}
    \xmarks{-3 upto -1}\xmarks{1 upto 5}
    \ymarks{1 upto 4}
    \tlpointsep{3pt}
    \tlabels{[tr](0, 0){$O$} [tc](\xmax, 0){$x$} [cr](0, \ymax){$y$}
        [cl](\Ax, \Ay){$A$} [cl](\Bx, \By){$B$} [cr](\Cx, \Cy){$C$} [br](\Dx, \Dy){$D$}}
    \tlpointsep{6pt}
    \axislabels{x}{{$-3$}-3, {$-2$}-2, {$-1$}-1, {$1$}1, {$2$}2, {$3$}3, {$4$}4, {$5$}5}
    \axislabels{y}{{$1$}1, {$2$}2, {$3$}3, {$4$}4}
\end{mfpic}
\closegraphsfile
\end{document}

Run it with LaTeX first (with whatever engine), then with MetaPost, then again with LaTeX.

enter image description here

Edit Once again, this time with Asymptote, short and efficient as ever. To be run with the asy command.

import graph;
unitsize(1.5cm);
real Xmin=-4, Xmax=5, Ymin=-.5, Ymax=5;
pair A = (3, 2), B = (5,4), C = (-3, 4), D = (-3, 0);
draw (A--B--C--D--cycle);
xaxis(xmin=Xmin, xmax=Xmax, Ticks(Step=1, OmitTick(0, Xmax)), arrow=Arrow);
yaxis(ymin=Ymin, ymax=Ymax, Ticks(Step=1, OmitTick(0, Ymax)), arrow=Arrow);
label("$x$", (Xmax, 0), S); label("$y$", (0, Ymax), W);
dot(Label("$A$", A, E)); label("$O$", (0, 0), SE); dot(Label("$B$", B, E));
dot(Label("$C$", C, W)); dot(Label("$D$", D, NW));

enter image description here

3
  • +1 Really nice. What is the difference between a = 1cm and a := 1cm? Mar 7 '15 at 9:52
  • 1
    @HenriMenke The latter is an assignment: the numeric variable a takes the value 1cm and if it had another value, it is discarded. The former is an equation: it checks that a is equal to 1cm or not. If a already had a value happening to be different from 1cm, MetaPost stops with an error message: inconsistent equation. If a was not given any value previously, the equation a = 1cm then is solved (MetaPost has a built-in solver of linear equations systems written implicitly), and here it means that a will take 1cm as value. So both formulations are (in that case) equivalent. Mar 7 '15 at 10:27
  • 1
    @HenriMenke By the way, I normally use assignments to give a value to a numeric variable (this way preventing some unpleasant surprises) but in this precise case I forgot the : of := :-). Another way of doing it would have been to write numeric a; a = 1cm;. numeric a; clears any former value a may have had, and then gives the value 1cm to a by the way of equation solving, but it's longer to write. :-) Mar 7 '15 at 10:35
3

A PSTricks solution using the pstricks-add package:

\documentclass{article}

\usepackage{pstricks-add}

\begin{document}

\begin{pspicture}(-3.5,-0.6)(6,5) % boundry found manually
  \pnodes{P}(3,2)(5,4)(-3,4)(-3,0)
  \psaxes{->}(0,0)(-3.5,-0.6)(5.65,4.6)[$x$,0][$y$,90]
  \psdots(P0)(P1)(P2)(P3)
  \pspolygon(P0)(P1)(P2)(P3)
  \uput[330](P0){$A$}
  \uput[30](P1){$B$}
  \uput[135](P2){$C$}
  \uput[135](P3){$D$}
\end{pspicture}

\end{document}

output

2

Try this one. You'll just have to fix the axis if needed.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \draw[->] (-10,0) -- (10,0) node[below] {$x$};
    \draw[->] (0,-5) -- (0,10) node[left] {$y$};

    \draw (3,2) -- (5,4) -- (-3,4) -- (-3,0);

\end{tikzpicture}
\end{document}
1
  • That's true. I just copied this from a project I'm working on and forgot to remove it.
    – Bjartmar
    Mar 6 '15 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.