Lesson 2: Find Your Way Through a Maze

Learn how to draw straight lines between points defined by cartesian coordinates, change line width and line color and use relative coordinates.  

Read this post for more details!

Your first illustration

Assume that you would like to draw a straight line from the point with coordinates (0,0) to the point with coordinates (2,1). This can be achieved in TikZ as follows:



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

% Draw a straight line
\begin{tikzpicture}
	\draw (0,0) -- (2,1);
\end{tikzpicture}

\end{document}

We loaded TikZ package and used \draw command and the operation -- to get a straight line between the points in question.

Important: each line code has to end with a semi-colon ;

Task 1:

Modify the following code in order to draw a rectangle with corners (0,0), (2,0), (2,1) and (0,1).



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

% Draw a rectangle
\begin{tikzpicture}
	\draw (0,0) -- (2,1);
	\draw (0,0) -- (2,1);
	\draw (0,0) -- (2,1);
	\draw (0,0) -- (2,1);
\end{tikzpicture}

\end{document}

Multiple pieces of straight lines

In Task 1, we have repeated the draw command four times to draw the rectangle's four sides. We can get the same result with only one draw command (path interrupted). Check the following code:



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

% Draw a rectangle
\begin{tikzpicture}
	\draw (0,0) -- (2,0) -- (2,1) -- (0,1) -- (0,0);
\end{tikzpicture}

\end{document}

If the path is not continuous, we skip the operation --  in place of the gaps and go on to next coordinates. Here is an example:



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

% Draw two parallel lines
\begin{tikzpicture}
	\draw (0,0) -- (2,0) (0,1) -- (2,1);
\end{tikzpicture}

\end{document}

It should be noted that a path is a series of straight lines and curves that are connected.

  • By default, the measurement unit is centimeter. You can also use:
      - pt: for Point unit
      - in: for inches (72.27 pt)
      - cm: for centimeter unit
      - mm: for millimeter unit
      - ex: corresponds to x-height
      - em: corresponds to m-width

Horizontal and Vertical Lines (-| and |-)

Suppose we want to connect two points by a line that contains a right angle and is partially horizontal and partially vertical. In that case, we can use either the operation -| or the operation -|, instead of successive applications of --. Check the following example:



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
% First horizontal, then vertical
	\draw (0,0) -| (2,2);

% First vertical, then horizontal
	\draw (2.5,0) |- (4.5,2);
\end{tikzpicture}

\end{document}

Task 2:

The code below draws a square. Optimize the code by using horizontal and vertical lines.



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

% Draw a right triangle
\begin{tikzpicture}
	\draw (0,0) -- (2,0) -- (2,2) -- (0,0);
\end{tikzpicture}

\end{document}

Modify Line Color

We can change the color by adding the name of the color to the draw command options. The color will be applied to the whole path of the drawing command in question.

Base Colors Latex

Check the following example : 



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}


\begin{tikzpicture}
% Line 1
	\draw [draw=red] (0,0) -- (3,0);

% Line 2
	\draw [draw=violet] (0,-1) -- (3,-1);

% Line 3
	\draw [teal] (0,-2) -- (3,-2);

\end{tikzpicture}

\end{document}

Remark: In the last line of the code, we only added teal color instead of draw=teal, which also works. The details will be discussed in the next lesson!

Change Line Width

The line width can be changed by adding one of these options to the draw command.

Line width in TikZ

Here is an example: 



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}


\begin{tikzpicture}
% Line 1
	\draw [red,thin] (0,0) -- (3,0);

% Line 2
	\draw [red,very thick] (0,-1) -- (3,-1);

% Line 3
	\draw [red,line width=3pt] (0,-2) -- (3,-2);

\end{tikzpicture}

\end{document}

Challenge of the day!

Solve the maze with straight lines and as little code as possible: 

- Use the operations -| and |- in order to minimize the TikZ code. 

- The origin (0,0) corresponds to the end of the arrowhead.

- Step between help lines is 1cm!

- Add your code below and compile to get results (or open it in Overleaf).



\documentclass[border=0.2cm]{standalone}

% Loading TikZ
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
% Grid
\draw[lightgray!20] (0,0) grid (8,8);

% Maze code
\draw[line width=3pt,
cap=round,
rounded corners=1pt,
draw=black!75] (-0.5,-0.5) -| (8.5,8.5)
(7.5,8.5)-|(-0.5,0.5) -- (0.5,0.5) (-0.5,2.5)-|(0.5,4.5)
(0.5,5.5)|-(4.5,7.5)|-(3.5,3.5)|-(2.5,2.5)|-(3.5,1.5)
(8.5,0.5)--(6.5,0.5)
(5.5,0.5)-|(3.5,-0.5)
(3.5,0.5)-|(1.5,3.5)-|(2.5,5.5)
(2.5,4.5)-|(3.5,6.5)-|(1.5,4.5)
(7.5,8.5)|-(5.5,6.5)--(5.5,7.5)
(6.5,7.5)--(6.5,8.5)
(8.5,3.5)-|(6.5,4.5)-|(7.5,5.5)
(6.5,5.5)-|(5.5,2.5)--(4.5,2.5)
(5.5,2.5)-|(7.5,1.5)--(4.5,1.5)
(5.5,4.5)--(4.5,4.5)
(1.5,1.5)--(0.5,1.5)
(1.5,7.5)--(1.5,8.5);

% Start and End Points
\draw[-latex,line width=3pt,red] (-1,0)--(0,0);
\draw[-latex,line width=3pt,red] (8,8) -- (8,9);

%%%%% PUT YOUR CODE HERE %%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{tikzpicture}

\end{document}

Summary

- We draw a path in TikZ using the \draw command.

- We add options to the draw command in squared brackets following the command.

- Line width can be changed by adding one of these as options: ultra thinvery thinthinsemithickthickvery thickultra thick or by providing the precise width value: line width=<value>.

- Colors are changed by adding the color names as options to the draw command.

-  The operation -- is used for the straight segment between two points.

- The operation -| creates the horizontal-vertical path from the starting point to the end point ( |- draws the vertical-horizontal path).