拉伸图像

拉伸图像

我正在尝试在 y 方向上创建一个变形六边形网格。@Ignasi 给出的以下解决方案似乎适用于未变形的情况

绘制六边形

在尝试修改代码后,我无法得到我想要的结果,所以我在考虑变形生成的 tikz 图像的可能性。

有什么命令可以用来实现我想要的吗?


\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}

\def\hexagonsize{0.5cm}
\pgfdeclarepatternformonly
{hexagons}% name
{\pgfpointorigin}% lower left
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}%  upper right
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize}}%  tile size
{% shape description
\pgfsetlinewidth{0.4pt}
\pgftransformshift{\pgfpoint{0mm}{0.866025*\hexagonsize}}
\pgfpathmoveto{\pgfpoint{0mm}{0mm}}
\pgfpathlineto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{3*\hexagonsize+0.2mm}{0mm}}
\pgfpathmoveto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfusepath{stroke}
}
\begin{document}

\begin{tikzpicture}
\fill[pattern=hexagons] (0,0) rectangle (5,5);
\end{tikzpicture}

\end{document}

我想要的是将图像在垂直方向上拉伸。例如,就图像而言,如果这是一张 100x100 的图像,我想将其转换为 100x200 的图像。

关于代码,我的想法是将六边形的边长沿 y 方向延伸,但我做不到。我曾尝试将数字乘以 2 或类似的数字,但没有成功

答案1

请注意,这\pgftransformyscale{\hexscale}并不影响其他参数。

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns}

\def\hexagonsize{0.5cm}
\def\hexscale{2}
\pgfdeclarepatternformonly
{hexagons}% name
{\pgfpointorigin}% lower left
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize*\hexscale}}%  upper right
{\pgfpoint{3*\hexagonsize}{0.866025*2*\hexagonsize*\hexscale}}%  tile size
{% shape description
\pgfsetlinewidth{0.4pt}
\pgftransformyscale{\hexscale}
\pgftransformshift{\pgfpoint{0mm}{0.866025*\hexagonsize}}
\pgfpathmoveto{\pgfpoint{0mm}{0mm}}
\pgfpathlineto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{-0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{3*\hexagonsize+0.2mm}{0mm}}
\pgfpathmoveto{\pgfpoint{0.5*\hexagonsize}{0mm}}
\pgfpathlineto{\pgfpoint{\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2*\hexagonsize}{0.866025*\hexagonsize}}
\pgfpathlineto{\pgfpoint{2.5*\hexagonsize}{0mm}}
\pgfusepath{stroke}
}
\begin{document}

\begin{tikzpicture}[yscale=2]
\fill[pattern=hexagons] (0,0) rectangle (5,5);
\end{tikzpicture}

\end{document}

演示

答案2

这是压缩或拉伸六角形晶格的另一种方法:

\documentclass{standalone}        
\begin{tikzpicture}
   \def\t{1/2};
   \def\s{1};
  \foreach \i in {0,...,8}
  \foreach \j in {0,...,3} {
  \coordinate (P) at (3*\i*\t,2*sin{60}*\j*\s);
  \coordinate (Q) at ([shift={(3*\t/2,sin{60}*\s)}]P);
  \draw[blue,thick]
        (P) -- ([shift={(\t,0)}]P)
        (P) -- ([shift={(-\t/2,-sin{60}*\s)}]P)
        (P) -- ([shift={(-\t/2,+sin{60}*\s)}]P);
  \draw[blue,thick]
        (Q) -- ([shift={(\t,0)}]Q)
        (Q) -- ([shift={(-\t/2,-sin{60}*\s)}]Q)
        (Q) -- ([shift={(-\t/2,+sin{60}*\s)}]Q);
}
\end{tikzpicture}
\end{document}

或者很简单

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[xscale=1/2, yscale=1]
  \foreach \i in {0,...,8}
  \foreach \j in {0,...,3} {
  \coordinate (P) at (3*\i,2*sin{60}*\j);
  \coordinate (Q) at ([shift={(3/2,sin{60})}]P);
  \draw[blue,thick]
        (P) -- ([shift={(1,0)}]P)
        (P) -- ([shift={(-1/2,-sin{60})}]P)
        (P) -- ([shift={(-1/2,+sin{60})}]P);
  \draw[blue,thick]
        (Q) -- ([shift={(1,0)}]Q)
        (Q) -- ([shift={(-1/2,-sin{60})}]Q)
        (Q) -- ([shift={(-1/2,+sin{60})}]Q);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

有些人可能不喜欢“卷须”。可以按如下方法去除它们:

\documentclass{standalone}    
\usepackage{tikz}
\begin{document}    
\begin{tikzpicture}
  \def\t{1/3};
  \def\s{1};
  \def\nrow{3}
  \def\ncol{8}
  \foreach \i in {0,...,\ncol}
  \foreach \j in {0,...,\nrow} {
  \coordinate (P) at (3*\i*\t,2*sin{60}*\j*\s);
  \coordinate (Q) at ([shift={(3*\t/2,sin{60}*\s)}]P);
  \ifnum\j>0 
    \draw[blue,thick] (P) -- ([shift={(-\t/2,-sin{60}*\s)}]P);
  \fi  
  \draw[blue,thick]        
        (P) -- ([shift={(\t,0)}]P)        
        (P) -- ([shift={(-\t/2,+sin{60}*\s)}]P);        
  \ifnum\j<\nrow 
    \draw[blue,thick](Q) -- ([shift={(-\t/2,+sin{60}*\s)}]Q);
  \fi    
  \ifnum\i<\ncol
    \draw[blue,thick](Q) -- ([shift={(\t,0)}]Q);
  \fi 
  \draw[blue,thick](Q) -- ([shift={(-\t/2,-sin{60}*\s)}]Q);                
}
\end{tikzpicture}    
\end{document}

在此处输入图片描述

相关内容