什麼是edge?

什麼是edge?

我正在嘗試使用 TikZ 為兒童創建分數圖。

有沒有一種優雅的方法來固定這個 1/2 圖表的頂部,當我想要顯示時,例如 29/6 帶有三角形......以及最終其他形狀,它可以縮放?看來以非 90 度角相交的路徑的交叉點並不完全按照我希望的方式相交...

亂七八糟的圖片

\documentclass{article}

\usepackage{tikz}
\usepackage{xcolor}

\begin{document}

\(\frac{1}{2}\)

\begin{tikzpicture}
\filldraw[fill=gray, draw=black, thick] (0,0)--(1,0)--(1,2)--cycle;
\draw[thick] (1,0)--(2,0)--(1,2)--cycle;
\end{tikzpicture}

\end{document}

答案1

我不確定這是否是您想要的,但請查看連結的圖像,似乎是這樣:我借用了一些程式碼Ignasi's answer如何在TikZ中繪製三角網格?。由於三角形網格是用\nodes 建構的,因此您可以輕鬆使用它們的錨點進行填充,我將其放置在圖層中background

在此輸入影像描述

代碼:

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

\newcommand\grid[1]{
\foreach \i [count=\row from 0, remember=\row as \lastrow (initially 0)] in {0,...,#1}
  {
  \foreach \j [count=\column from 0, remember=\column as \lastcolumn (initially 0)] in {0,...,\i}
    {
    \ifnum\row=0
      \node[tri](0-0) {};
    \else
      \ifnum\column=0
        \node[tri, anchor=north](\row-0) at (\lastrow-0.corner 2) {};
      \else
         \node[tri, anchor=north](\row-\column) at (\lastrow-\lastcolumn.corner 3) {};
      \fi
    \fi
    }
  }
}

\begin{document}

\begin{tikzpicture}[
tri/.style={
  draw,
  regular polygon,
  regular polygon sides=3, 
  minimum size=2cm, 
  inner sep=0pt,
  outer sep=0pt,
  line width=1pt
  }
]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (0-0.corner 1) -- 
    (3-3.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}

\begin{scope}[xshift=8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (3-0.corner 1) -- 
    (3-3.corner 1) -- 
    (3-3.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}
\end{scope}

\begin{scope}[yshift=-8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (2-0.corner 1) -- 
    (2-2.corner 1) -- 
    (3-1.corner 3) -- 
    cycle;
\end{pgfonlayer}
\end{scope}

\begin{scope}[xshift=8cm,yshift=-8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (0-0.corner 1) -- 
    (3-1.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}
\draw[line width=1pt]
  (0-0.corner 1) -- (3-1.corner 3);
\end{scope}
\end{tikzpicture}

\end{document}

答案2

一種方法是先填充,然後繪製一條路徑:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \fill [fill=gray] (0,0)--(1,0)--(1,2)--cycle;
  \draw[thick] (0,0) -- (1,2) edge (1,0) -- (2,0) -- cycle;
\end{tikzpicture}
\end{document}

填充然後繪製

什麼是edge?

Anedge是一條獨立的路徑,可以朝不同的方向延伸,並且可以使用與主路徑完全不同的選項來繪製(或不繪製)。它不會擾亂主要路徑。在這種情況下,向下edge繪製垂直線,但主路徑從頂點開始,就好像沒有edge指定一樣。edges 對於複雜路徑尤其有用。它們是在主路徑之後建造的。

要查看發生了什麼,請更改路徑規範,如下所示:

\draw[thick] (0,0) -- (1,2) edge [red] (1,0) -- (2,0) -- cycle;

預設情況下,edge從主路徑繼承選項,因此這個選項是thick,但與主路徑不同的是,它是red.當然,在上圖中,沒有指定明顯的選項,因此edge也是預設顏色並與主路徑混合。

紅邊

相關內容