5

I am drawing a convex polygon and a line connecting two points on edges. However, as shown in the figure below, the endpoints of the line are not on the edges. How to fix that?

convex-polygon-line

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \path[draw] (0,0) node[left] (6) {6}
    -- (1.5,-1) node[below] (5) {5}
    -- (3.5, -0.8) node[below] (4) {4}
    -- (5,0) node[right] (3) {3}
    -- (4, 1.5) node[above] (2) {2}
    -- (1,2) node[above] (1) {1}
    -- cycle;

    \draw ($(3)!0.4!(4)$) -- ($(1)!0.4!(2)$);
\end{tikzpicture}
\end{document}
7

That happens because your (1), (2), (3), etc. nodes are the labels, not the points.

Here a possible solution using \coordinate:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}
    \coordinate (1) at (1,2); 
    \coordinate (2) at (4, 1.5); 
    \coordinate (3) at (5,0); 
    \coordinate (4) at (3.5, -0.8); 
    \path[draw] (0,0) node[left] {6}
    -- (1.5,-1) node[below] {5}
    -- (4) node[below] {4}
    -- (3) node[right] {3}
    -- (2) node[above] {2}
    -- (1) node[above] {1}
    -- cycle;

    \draw ($(3)!0.4!(4)$) -- ($(1)!0.4!(2)$);
    \end{tikzpicture}
\end{document}

enter image description here

7

You can see the explanation why it is not working in the Heiko Oberdiek's answer. To overcome this you can replace node[position] (n) {n} by coordinate[label=position:n] (n). Here is the full code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
    \path[draw] (0,0) coordinate[label=left:6] (6)
      -- (1.5,-1) coordinate[label=below:5] (5)
      -- (3.5, -0.8) coordinate[label=below:4] (4)
      -- (5,0) coordinate[label=right:3] (3)
      -- (4, 1.5) coordinate[label=above:2] (2)
      -- (1,2) coordinate[label=above:1] (1)
      -- cycle;

    \draw[red] ($(3)!0.4!(4)$) -- ($(1)!0.4!(2)$);
  \end{tikzpicture}
\end{document}

enter image description here

6

The lines (3) -- (4) and (1) -- (2) connect the nodes, the center point of the nodes:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \path[draw] (0,0) node[left] (6) {6}
    -- (1.5,-1) node[below] (5) {5}
    -- (3.5, -0.8) node[below] (4) {4}
    -- (5,0) node[right] (3) {3}
    -- (4, 1.5) node[above] (2) {2}
    -- (1,2) node[above] (1) {1}
    -- cycle;

    \draw[red] (3) -- (4) (1) -- (2);
    \draw[blue] ($(3)!0.4!(4)$) -- ($(1)!0.4!(2)$);
\end{tikzpicture}
\end{document}

Visualization of the connection lines to illustrate the problem

Instead of the center points of the nodes, you need their anchor points:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \path[draw] (0,0) node[left] (6) {6}
    -- (1.5,-1) node[below] (5) {5}
    -- (3.5, -0.8) node[below] (4) {4}
    -- (5,0) node[right] (3) {3}
    -- (4, 1.5) node[above] (2) {2}
    -- (1,2) node[above] (1) {1}
    -- cycle;

    \draw[red] ($(3.west)!0.4!(4.north)$) -- ($(1.south)!0.4!(2.south)$);
\end{tikzpicture}
\end{document}

Result using anchor points

However, this is a maintenance nightmare. If a label is moved, it requires a fix of the anchor point as well. Therefore, it is cleaner to define the polygon vertices as coordinates as shown in the answer of CarLaTeX.

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.