如何用点填充区域?如何控制点之间的距离?如何防止线和点之间的干扰?如何改变点的颜色?
\documentclass[tikz,margin=5pt]{standalone}
\usepackage{xparse}
\usetikzlibrary{calc}
\tikzset{%
Cote node/.style={%
midway,
sloped,
fill=white,
inner sep=1.5pt,
outer sep=2pt
},
Cote arrow/.style={%
<->,
>=latex,
very thin
}
}
\makeatletter
\NewDocumentCommand{\Cote}{%
s % cotation avec les flèches à l'extérieur
D<>{1.5pt} % offset des traits
O{.75cm} % offset de cotation
m % premier point
m % second point
m % étiquette
D<>{o} % () coordonnées -> angle
% h -> horizontal,
% v -> vertical
% o or what ever -> oblique
O{} % parametre du tikzset
}{%
{\tikzset{#8}
\coordinate (@1) at #4 ;
\coordinate (@2) at #5 ;
\if #7v % Cotation verticale
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@4) at (@0|-@1) ;
\coordinate (@5) at (@0|-@2) ;
\else
\if #7h % Cotation horizontale
\coordinate (@0) at ($($#4!.5!#5$) + (0,#3)$) ;
\coordinate (@4) at (@0-|@1) ;
\coordinate (@5) at (@0-|@2) ;
\else % cotation encoche
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\coordinate (@5) at ($#7!#3!#5$) ;
\coordinate (@4) at ($#7!#3!#4$) ;
\else % cotation oblique
\coordinate (@5) at ($#5!#3!90:#4$) ;
\coordinate (@4) at ($#4!#3!-90:#5$) ;
\fi\fi\fi
\draw[very thin,shorten >= #2,shorten <= -2*#2] (@4) -- #4 ;
\draw[very thin,shorten >= #2,shorten <= -2*#2] (@5) -- #5 ;
\IfBooleanTF #1 {% avec étoile
\draw[Cote arrow,-] (@4) -- (@5)
node[Cote node] {#6\strut};
\draw[Cote arrow,<-] (@4) -- ($(@4)!-6pt!(@5)$) ;
\draw[Cote arrow,<-] (@5) -- ($(@5)!-6pt!(@4)$) ;
}{% sans étoile
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\draw[Cote arrow] (@5) to[bend right]
node[Cote node] {#6\strut} (@4) ;
\else
\draw[Cote arrow] (@4) -- (@5)
node[Cote node] {#6\strut};
\fi
}}
}
\makeatother
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\coordinate (O) at (0,0);
\fill[blue!30,opacity=0.65] (0,0.5)-- (4,0.5)--(4,-0.5) -- (0,-0.5) -- cycle;
\draw[thick] (0,0.5)--(4,0.5)--(4,-0.5);
\draw[thick] (4,-0.5)--(0,-0.5) --(0,0.5);
\Cote{(4,-.5)}{(4,.5)}{$h$}[ Cote node/.append style={right,rotate=-90}];
\Cote[1cm]{(0,0)}{(4,0)}{$a$};
\draw (0,0.25)--(4,0.25);
\draw (0,-0.25)--(4,-0.25);
\draw (0,0.25/2)--(4,0.25/2);
\draw (0,-0.25/2)--(4,-0.25/2);
\draw (0,3*0.25/2)--(4,3*0.25/2);
\draw (0,-3*0.25/2)--(4,-3*0.25/2);
\draw (0,0)--(4,0);
\fill [fill=red, pattern=crosshatch dots]
(0,0.5)-- (4,0.5)--(2,0) -- cycle;
\fill [fill=red, pattern=crosshatch dots]
(0,-0.5)-- (4,-0.5)--(2,0) -- cycle;
\end{tikzpicture}
\end{document}
答案1
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,patterns,calc}
\begin{document}
\def\yysep{0.25}
\def\xxsep{4}
\begin{tikzpicture}
\foreach \y in {1,...,9} {%
\node[inner sep=0pt] (l\y) at (0,\yysep*\y) {};
\node[inner sep=0pt] (r\y) at (\xxsep,\yysep*\y) {};
\draw (l\y) -- (r\y);
};
\begin{pgfonlayer}{background}
\draw[fill=blue!20] (l1) rectangle (r9);
\end{pgfonlayer}
\coordinate (auxa) at ($(l1)!0.5!(r1)$);
\coordinate (auxb) at ($(l1)!0.5!(l9)$);
\coordinate (boxcenter) at (auxa |- auxb);
\begin{scope}
\pgfsetfillpattern{dots}{red}
\fill (l1) -- (boxcenter) -- (r1) -- cycle;
\fill (l9) -- (boxcenter) -- (r9) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}