在下面的三角形中,如何才能平等划分三角形的边缘,而不是指定绝对坐标?目标是在两侧的两个平行边缘之间放置一条水平线,并在底边之间放置等距的垂直线。在中间放置一个文本,上下放置一个文本,或者根据情况在左右放置一个文本。
例如,我想用四个等距的水平条来划分边缘,并在其上方和两侧放置一个文本 - 即一个在内三角形内,一个在外三角形外。
查看代码
\begin{tikzpicture}
[scale=1.5]
\draw (8.,0.) -- (14.,0.) -- (11.,5.196152422706633) -- cycle;
\draw (9.,0.5) -- (13.,0.5) -- (11.,4.196152422706633) -- cycle;
\draw (8.,0.) -- (9., 0.5);
\draw (14.,0.) -- (13., 0.5);
\draw (11.,5.196152422706633) -- (11.,4.196152422706633);
\end{tikzpicture}
答案1
我将使用极坐标表示三角形,并使用一些辅助坐标和calc
库来放置文本。我定义了一个命令\DivideSide
,允许您指定每条边的细分数;此命令还分配了一些坐标,以便在每个细分中放置文本:
\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\DivideSide[4]{
\foreach \Value [evaluate=\Value as \Parts using \Value/#1] in {1,...,\numexpr#1-1\relax}
\draw
( $ (#22)!\Parts!(#32) $ ) -- coordinate (#4\Value)
( $ (#21)!($(#22)!\Parts!(#32)$)!(#31) $ );
}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\draw
(8,0) coordinate (left1) -- ++(0:6) coordinate (right1) -- ++(120:6) coordinate (top1) -- cycle
(9.,0.5) coordinate (left2) -- ++(0:4) coordinate (right2) -- ++(120:4) coordinate (top2) -- cycle
(8,0) -- coordinate (MiddleLeft) (9,0.5)
(14,0) -- coordinate (MiddleRight) (13,0.5)
(top1) -- coordinate (MiddleTop) (top2);
% Subdivide the sides
\DivideSide{3}{left}{top}{LeftSide}
\DivideSide{4}{right}{top}{RightSide}
\DivideSide{5}{right}{left}{BottomSide}
% Labels for the left side
\node[rotate=60] at
( $ (MiddleLeft)!0.5!(LeftSide1) $ )
{a};
\node[rotate=60] at
( $ (LeftSide1)!0.5!(LeftSide2) $ )
{b};
\node[rotate=60] at
( $ (LeftSide2)!0.5!(MiddleTop) $ )
{c};
% Labels for the right side
\node[rotate=-60] at
( $ (MiddleTop)!0.5!(RightSide3) $ )
{d};
\node[rotate=-60] at
( $ (RightSide3)!0.5!(RightSide2) $ )
{e};
\node[rotate=-60] at
( $ (RightSide2)!0.5!(RightSide1) $ )
{f};
\node[rotate=-60] at
( $ (RightSide1)!0.5!(MiddleRight) $ )
{g};
% Labels for the bottom side
\node at
( $ (MiddleRight)!0.5!(BottomSide1) $ )
{h};
\node at
( $ (BottomSide1)!0.5!(BottomSide2) $ )
{i};
\node at
( $ (BottomSide2)!0.5!(BottomSide3) $ )
{j};
\node at
( $ (BottomSide3)!0.5!(BottomSide4) $ )
{k};
\node at
( $ (BottomSide4)!0.5!(MiddleLeft) $ )
{l};
\end{tikzpicture}
\end{document}
答案2
受到 Gonzalo Medina 回答的启发,我尝试编写更短的代码。与 Gonzalo 的解决方案一样,它使用calc
TikZ 库,主要区别在于,如何在三角形部分中定位带有文本的节点。对于文本,我也采用了 Gonzales 的命名法。
由于我不明白为什么问题的 MWE 中的起始坐标是(8,0)
。我将其更改为(0,0)
。
\documentclass[border=3mm,
tikz,
preview]{standalone}
\usetikzlibrary{calc}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}
[scale=1.5]
\draw (0,0.0) coordinate (a) -- ++
(6,0.0) coordinate (b) -- ++
(120:6) coordinate (c) -- cycle;
\draw (1,0.5) coordinate (a') -- ++
(4,0.0) coordinate (b') -- ++
(120:4) coordinate (c') -- cycle;;
\draw (a) -- (a') (b) -- (b') (c) -- (c');
%
\draw ($(a)!0.5!(b)$) coordinate (mb) -- ++ (0,0.5);
\draw ($(b')!0.5!(c')$) coordinate (mr) -- ($(b)!0.5!(c)$);
\draw ($(a')!0.5!(c')$) coordinate (ml) -- ($(a)!0.5!(c)$);
%
\begin{scope}[every node/.style={inner ysep=3mm,text depth=0.3 ex}]
\path (a') -- node {bottom left} (mb) -- node {bottom right} (b');
\path (a') -- node[above,sloped] {left lower} (ml)
-- node[above,sloped] {left upper} (c');
\path (b') -- node[above,sloped] {right lower} (mr)
-- node[above,sloped] {right upper} (c');
\end{scope}
\end{tikzpicture}
\end{document}
使用上述代码生成的图像是:
编辑: 要将边界划分为两个以上的部分,您需要考虑外三角形的边界部分与内三角形的相同部分不垂直。因此,您需要考虑使用不同的方法:在边界之间绘制垂直线。在此应考虑到内三角形不仅是外三角形的缩放版本,而且还向下移动。因此,它们之间的距离在底部(5 毫米)和左上角和右上角(~6,1604 毫米)不同。因此,您需要使用一些几何图形来解决这个问题。
首先计算三角形左上角和右上角之间的距离。从几何学上可知:
(10 - 5/tan(60))cos(30) [mm]
知道这个距离,边界各部分之间的线可以用
\draw ($(b')!0.33!(c')$) coordinate (mr1) -- ($(mr1)!6.16mm!90:(b')$);
(右上边框)。其位置由第一个括号中的项决定。此方法可用于三个边框,或者对于底部边框使用一些简化,因为有正交线。
考虑到上述情况,代码如下:
\documentclass[border=3mm,
tikz,
many,
preview]{standalone}
\usetikzlibrary{calc}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}
[scale=1.5]
\draw (0,0.0) coordinate (a) -- ++
(6,0.0) coordinate (b) -- ++
(120:6) coordinate (c) -- cycle;
\draw (1,0.5) coordinate (a') -- ++
(4,0.0) coordinate (b') -- ++
(120:4) coordinate (c') -- cycle;;
\draw (a) -- (a') (b) -- (b') (c) -- (c');
% in case of more than 2 parts in lower border
\draw ($(a)!0.33!(b)$) coordinate (mb1) -- ++ (0,0.5);
\draw ($(a)!0.66!(b)$) coordinate (mb2) -- ++ (0,0.5);
% in case of more than 2 parts in upper right border
% distance 6.16 = (10 - 5/tan(60))cos(30) in [mm]
\draw ($(b')!0.33!(c')$) coordinate (mr1) -- ($(mr1)!6.16mm!90:(b')$);
\draw ($(b')!0.66!(c')$) coordinate (mr2) -- ($(mr2)!6.16mm!90:(b')$);
%
\draw ($(a')!0.5!(c')$) coordinate (ml) -- ($(a)!0.5!(c)$);
%
\begin{scope}[every node/.style={inner ysep=3mm,text depth=0.3 ex}]
\path (a') -- node {A} (mb1)
++ (0,0.5) % for going on opposite side of line,
% necessary fo
-- node {B} (mb2)
-- node {C} (b');
\path (a') -- node[above,sloped] {left lower} (ml)
-- node[above,sloped] {left upper} (c');
\path (b') -- node[above,sloped] {D} (mr1)
-- node[above,sloped] {E} (mr2)
-- node[above,sloped] {F} (c');
\end{scope}
\end{tikzpicture}
\end{document}
它给:
所以,边界部分的分界线是平行的!