최근에 나는 pgfplots를 발견했고 이 패키지의 기능에 깊은 인상을 받았습니다. 그러나 pgfplots를 사용하여 편리하게 정의하는 방법을 알 수 없는 한 가지 유형의 플롯이 있습니다.
기본적으로 나는 사각형의 하위 집합이 표시된 사각형 격자를 플롯하고 싶습니다(죄송합니다. 새로운 사용자로서 이미지를 게시할 수 없습니다). 닫힌 모양이 그려지는 설명서의 예를 따르거나 수정할 수 있다는 것을 알고 있습니다.
% Preamble: \pgfplotsset{width=7cm,compat=1.6}
\begin{tikzpicture}
\begin{axis}
\addplot+[fill] coordinates
{(0,1) (1,2) (0,3) (-1,2)} --cycle;
\end{axis}
\end{tikzpicture}
하지만 궁금합니다.제곱의 정수 좌표각 사각형의 정확한 네 모서리 목록이 아닌 표시를 하시겠습니까?
답변1
스택을 원하는지 슬래브를 원하는지 확실하지 않으므로 다음은 둘 다입니다.
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{arrayjobx}
\usepackage{trimspaces}
\usepackage{xifthen}
\makeatletter
\def\trimspace#1{\trim@spaces@in{#1}}
\makeatother
\newcommand{\getslab}[2]{\checkdrawsquares(#1,#2)\trimspace\cachedata}
\newcommand{\drawstacks}[3]% fillheights, baroptions, gridoptions
{ \draw[#3] (0,0) grid (\gridwidth,\gridheight);
\foreach \x [count=\c] in {#1}
{ \fill[#2] (\c-1,0) rectangle (\c,\x);
}
}
\newcommand{\drawslabs}[3]% dataarray, slaboptions, gridoptions
{ \draw[#3] (0,0) grid (\gridwidth,\gridheight);
\foreach \x in {1,...,\gridwidth}
{ \foreach \y in {1,...,\gridheight}
{ \pgfmathtruncatemacro{\colnum}{\x}
\pgfmathtruncatemacro{\rownum}{\gridheight+1-\y}
\getslab{\rownum}{\colnum}
\ifthenelse{\cachedata>0}
{\fill[#2] (\x-1,\y-1) rectangle (\x,\y);}
{}
}
}
}
\begin{document}
\pgfmathtruncatemacro{\gridwidth}{13}
\pgfmathtruncatemacro{\gridheight}{7}
\newarray\drawsquares
\readarray{drawsquares}{%
1 & 1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 1 &%
0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 &%
1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 &%
1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 &%
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1}
\dataheight=\gridwidth%
\begin{tikzpicture}
\drawstacks{1,7,5,0,3,0,0,2,3,4,6,4,7}{red!50!gray,fill opacity=0.5,draw=red!50!gray,thick}{gray,densely dashed}
\end{tikzpicture}
\begin{tikzpicture}
\drawslabs{drawsquares}{red!50!gray,fill opacity=0.5,draw=red!50!gray,thick}{gray,densely dashed}{red}{gray}
\end{tikzpicture}
\end{document}
편집 1:나는 그것을 관리했지만 어제 \expandafter
전날 만 잊어 버렸습니다 \csname
.
업데이트됨\getslab
\newcommand{\getslab}[3]{\expandafter\csname check#1\endcsname(#2,#3)\trimspace\cachedata}
업데이트됨\drawslabs
\newcommand{\drawslabs}[3]% arraname, slaboptions, gridoptions
{ \draw[#3] (0,0) grid (\gridwidth,\gridheight);
\foreach \x in {1,...,\gridwidth}
{ \foreach \y in {1,...,\gridheight}
{ \pgfmathtruncatemacro{\colnum}{\x}
\pgfmathtruncatemacro{\rownum}{\gridheight+1-\y}
\getslab{#1}{\rownum}{\colnum}
\ifthenelse{\cachedata>0}
{\fill[#2] (\x-1,\y-1) rectangle (\x,\y);}
{}
}
}
}
새로운 샘플 입력
\pgfmathtruncatemacro{\gridwidth}{13}
\pgfmathtruncatemacro{\gridheight}{7}
\newarray\drawsquares
\readarray{drawsquares}{%
1 & 1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 1 &%
0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 &%
1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 &%
1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 &%
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1}
\dataheight=\gridwidth%
\begin{tikzpicture}
\drawstacks{1,7,5,0,3,0,0,2,3,4,6,4,7}{red!50!gray,fill opacity=0.5,draw=red!50!gray,thick}{gray,densely dashed}
\end{tikzpicture}
\begin{tikzpicture}
\drawslabs{drawsquares}{red!50!gray,fill opacity=0.5,draw=red!50!gray,thick}{gray,densely dashed}{red}{gray}
\end{tikzpicture}
\delarray\drawsquares% Deleting the old array; don't reuse names of deleted arrays!
% define a "new array" by just interchanging width and height
\pgfmathtruncatemacro{\gridwidth}{7}% changed from 13 to 7
\pgfmathtruncatemacro{\gridheight}{13}% changed from 7 to 13
\newarray\verticalarray% new name
\readarray{verticalarray}{%
1 & 1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 1 & 1 &%
0 & 0 & 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 &%
1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 &%
1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 &%
1 & 1 & 1 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 &%
0 & 1 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 1 & 1 & 1}
\dataheight=\gridwidth% similar array as before, but this time interpreted as 7x13 instead of 13x7
\begin{tikzpicture}
\drawslabs{verticalarray}{red!50!gray,fill opacity=0.5,draw=red!50!gray,thick}{gray,densely dashed}{red}{gray}
\end{tikzpicture}