我正在尝试创建显示罗马数字或标准数字的模拟时钟表盘,同时创建另一个与其镜像相同的表盘。我成功做到了这一点,但不知何故出现了第三个。以下是我尝试的代码。
\documentclass[tikz, margin=2mm]{standalone}
\usetikzlibrary{spy,decorations.fractals}
\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother
\tikzset{
hour pointer/.style={line cap=round, thick},
minute pointer/.style={thin, line cap=round},
ticks sep/.style={outer sep=-#1}, ticks sep={0pt},
ticks size/.store in=\clocktickssize,
ticks size=.25cm,
ticks/.style={thin},
main ticks/.style={very thick},
clock/.style={draw, minimum size=4cm, circle},
clock text/.code={\let\clocktext\textrm},
hour pointer size/.store in=\hourpointersize,
hour pointer size=0.5cm,
minute pointer size/.store in=\minutepointersize,
minute pointer size=0.75cm,
clock text sep/.store in=\clocktextsep,
clock text sep=1em,
pics/clock/.style args={#1:#2}{
code={%
\pgfmathsetmacro{\hourangle}{90-#1*30-#2*0.5} \pgfmathsetmacro{\minuteangle}{90-#2*6}
\node[clock] (-clock) {};
\foreach \angle [count=\xi] in {60,30,...,-270}
{
\draw[clock text, ticks] (-clock.\angle) -- ++(\angle:- \clocktickssize) node[anchor=center, shift={(\angle:-\clocktextsep)}] {\clocktext{\xi}};
};
\foreach \angle in {0,90,180,270} \draw[ticks, main ticks] (-clock.\angle) -- ++(\angle:-\clocktickssize);
\draw[minute pointer] (-clock.center) -- ++(\minuteangle:\minutepointersize);
\draw[hour pointer] (-clock.center) -- ++(\hourangle:\hourpointersize);}}}
\tikzset{
mirror scope/.is family,
mirror scope/angle/.store in=\mirrorangle,
mirror scope/center/.store in=\mirrorcenter,
mirror setup/.code={\tikzset{mirror scope/.cd,#1}},
mirror scope/.style={mirror setup={#1},spy scope={
rectangle,lens={rotate=\mirrorangle,yscale=-1,rotate=-1*\mirrorangle},size=80cm}},}
\usetikzlibrary{arrows.meta}
\newcommand\mirror[1][]{\spy[overlay,#1] on (\mirrorcenter) in node at (\mirrorcenter)}
\begin{document}
\begin{tikzpicture}
\pic[left=6cm,
clock text/.code={\let\clocktext\rom},
clock/.style={minimum size=5cm, draw}] (Original) at (0,0) {clock=15:00};
\begin{scope}[mirror scope={center={5,0},angle=90}]
\pic[right=3cm,
clock text/.code={\let\clocktext\rom},
clock/.style={minimum size=5cm, draw}] (Reversed) at (4,0) {clock=15:00};
\mirror;
%\foreach \clock in { Reversed, Original} \node[above=.25cm] %at (\clock -clock.north) {\clock\ Clock};
\end{scope}
\end{tikzpicture}
\end{document}
输出是 左边的前两个还可以。但第三个不应该在那里。显然我的编码不正确或效率不高。有没有建议镜像图像或如何调整原始代码以仅获取前两个图像。谢谢。
答案1
(注:模拟时钟代码来自https://tex.stackexchange.com/a/343524/14500镜像代码来自如何镜像和定位模拟时钟表盘)
在您的示例中,您绘制两个时钟(Original
和Reversed
),然后调用它\mirror
来镜像一个Reversed
时钟……因此您得到了三个时钟!
这是一个带注释的解决方案:
\documentclass[tikz,margin=2mm]{standalone}
% code from <https://tex.stackexchange.com/a/343524/14500>
\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother
\tikzset{
hour pointer/.style={line cap=round, thick},
minute pointer/.style={thin, line cap=round},
ticks sep/.style={outer sep=-#1}, ticks sep={0pt},
ticks size/.store in=\clocktickssize,
ticks size=.25cm,
ticks/.style={thin},
main ticks/.style={very thick},
clock/.style={minimum size=4cm,draw,circle},
clock text/.code={\let\clocktext\textrm},
hour pointer size/.store in=\hourpointersize,
hour pointer size=0.5cm,
minute pointer size/.store in=\minutepointersize,
minute pointer size=0.75cm,
clock text sep/.store in=\clocktextsep,
clock text sep=1em,
pics/clock/.style args={#1:#2}{
code={%
\pgfmathsetmacro{\hourangle}{90-#1*30-#2*0.5}
\pgfmathsetmacro{\minuteangle}{90-#2*6}
\node[clock] (-clock) {};
\foreach \angle [count=\xi] in {60,30,...,-270}
{
\draw[clock text, ticks] (-clock.\angle) -- ++(\angle:- \clocktickssize) node[anchor=center, shift={(\angle:-\clocktextsep)}] {\clocktext{\xi}};
};
\foreach \angle in {0,90,180,270} \draw[ticks, main ticks] (-clock.\angle) -- ++(\angle:-\clocktickssize);
\draw[minute pointer] (-clock.center) -- ++(\minuteangle:\minutepointersize);
\draw[hour pointer] (-clock.center) -- ++(\hourangle:\hourpointersize);}
},
}
% code from <https://tex.stackexchange.com/q/568227/14500>
\usetikzlibrary{spy}
\tikzset{
mirror scope/.is family,
mirror scope/angle/.store in=\mirrorangle,
mirror scope/center/.store in=\mirrorcenter,
mirror setup/.code={\tikzset{mirror scope/.cd,#1}},
mirror scope/.style={mirror setup={#1},spy scope={
rectangle,lens={rotate=\mirrorangle,yscale=-1,rotate=-1*\mirrorangle},size=80cm}},}
\newcommand\mirror[1][]{\spy[overlay,#1] on (\mirrorcenter) in node at (\mirrorcenter)}
\begin{document}
\begin{tikzpicture}
% to fix the bounding box
\path (-2.5,-2.5) rectangle (8.5,2.5);
\begin{scope}[mirror scope={center={3,0},angle=90}]
% to draw the clock
\pic[clock text/.code={\let\clocktext\rom},clock/.style={minimum size=5cm, draw}] at (0,0) {clock=15:00};
% to mirror it
\mirror;
\end{scope}
\end{tikzpicture}
\end{document}
答案2
另一个选项是使用\foreach
(每个时钟)并在其中\scope
使用可以更改的xscale
。我还添加了可自定义的时间设置和细微的外观更改(指针)。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\newcounter{hours}
% the time
\def\hour{2}
\def\minute{25}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\def\l {4} % clock size
\def\ml{0.35*\l} % minute hand lenght
\def\mw{0.005*\l} % minute hand width
\def\hl{0.3*\l} % hour hand lenght
\def\hw{0.01*\l} % hour hand width
\foreach\i in {-1,1}
{%
\begin{scope}[shift={(0.55*\i*\l,0)},xscale=-\i,transform shape]
\draw[clip] (-0.5*\l,-0.5*\l) rectangle (0.5*\l,0.5*\l);
\foreach\j in {30,60,120,150}
{%
\draw (\j:\l) -- (180+\j:\l);
}
\draw[thick] ( 0:\l) -- (180:\l);
\draw[thick] (90:\l) -- (270:\l);
\fill[white] (-0.45*\l,-0.45*\l) rectangle (0.45*\l,0.45*\l);
\foreach\j in {1,...,12}
{%
\pgfmathsetmacro\r{0.45*\l+(Mod(\j,3)==0?-0.075*\l:0}
\ifnum\j = 4
\node at (90-30*4:\r) {\small IIII};
\else
\setcounter{hours}{\j}
\node at (90-30*\j:\r) {\small\Roman{hours}};
\fi
}
\fill (0,0) circle (0.02*\l);
\begin{scope}[rotate=90-30*(\hour+\minute/60)]
\fill (-0.03*\l,-\hw) rectangle (\hl,\hw);
\end{scope}
\begin{scope}[rotate=90-6*\minute]
\fill (-0.03*\l,-\mw) rectangle (\ml,\mw);
\end{scope}
\end{scope}
}
\end{tikzpicture}
\end{document}