如下代码只是自写输入了等差数列,进行圆的等分。
\begin{tikzpicture}
%Circle
\draw[blue] (0,0) circle (8);
%Line
\foreach \i in {10,20,...,360}
\foreach \j in {10,20,...,360}
{\draw[red,thin] (\j:8)--(\i:8);};
\end{tikzpicture}
\end{center}
做了一点修改:
\begin{tikzpicture}
%Circle
\draw[blue] (0,0) circle (8);
%Line
\foreach \i in {10,20,...,360}
{
\foreach \j in {10,20,...,360}
{
\draw[red,thin] (\j:8)--(\i:8);
}
}
\end{tikzpicture}
以下代码有点问题。
\begin{tikzpicture}
%Circle
\draw[blue] (0,0) circle (8);
\def\n{36};%定义圆等分的份数
%Line
\foreach \i in {{360.0/\n},{360.0*2/\n},...,360.0}
\foreach \j in {{360.0/\n},{360.0*2/\n},...,360.0}
{\draw[red,thin] (\j:8)--(\i:8);};
\end{tikzpicture}
大家帮忙一下,如何才能正常运行。
只需给\foreach
命令加上参数[parse=true]
即可
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%Circle
\draw[blue] (0,0) circle (8);
\def\n{36};%定义圆等分的份数
%Line
\foreach[parse=true] \i in {{360.0/\n},{360.0*2/\n},...,360.0}
\foreach[parse=true] \j in {{360.0/\n},{360.0*2/\n},...,360.0}
\draw[red,thin] (\j:8)--(\i:8);
\end{tikzpicture}
\end{document}
另外,循坏也可以使用l3fp
来做,但不建议
\documentclass{standalone}
\usepackage{expl3,tikz}
\begin{document}
\ExplSyntaxOn
\begin{tikzpicture}
%Circle
\draw[blue] (0,0) circle (8);
\def\n{36};%定义圆等分的份数
%Line
\fp_step_inline:nnnn{360/\n}{360/\n}{360}
{
\fp_step_inline:nnnn{360/\n}{360/\n}{360}
{\draw[red,thin]({##1} \c_colon_str 8)--({#1}\c_colon_str 8);}
}
\end{tikzpicture}
\ExplSyntaxOff
\end{document}
可以基于l3draw宏包,用LaTeX3实现,圆的半径和等分数可以通过可选参数用key-value方式指定(默认半径为8cm,等分数为10),如:
\documentclass{standalone}
\usepackage{l3draw}
\usepackage{xparse}
\ExplSyntaxOn
\bool_new:N \l__circdiv_intersection_bool
\int_new:N \l__circdiv_cnt_int
\dim_new:N \l__circdiv_radius_dim
% key_value选项设计
\keys_define:nn { circdiv }
{
% 等分数
cnt .int_set:N = \l__circdiv_cnt_int,
cnt .initial:n = 10,
% 圆的半径
r .dim_set:N = \l__circdiv_radius_dim ,
r .initial:n = 1cm,
unknown .code:n = { \msg_error:nn { circdiv } { unknown-option } }
}
\msg_new:nnn { circdiv } { unknown-option }
{ package~ option~ "\l_keys_key_tl"~ is~ unknown. }
\keys_set:nn { circdiv }
{
cnt = 10 ,
r = 8cm,
}
\NewDocumentCommand \circdivset { m }
{ \keys_set:nn { circdiv } {#1} }
\cs_new:Npn \__circdiv_div:
{
\draw_begin:
\color_select:n { blue }
\draw_path_circle:nn { 0cm, 0cm } { \l__circdiv_radius_dim }
\draw_path_use_clear:n { stroke }
\color_select:n { red }
\int_step_inline:nnnn { 0 } { 360/\l__circdiv_cnt_int } { 359 }
{
\int_step_inline:nnnn { 0 } { 360/\l__circdiv_cnt_int } { 359 }
{
\draw_path_moveto:n
{ \draw_point_polar:nn { \l__circdiv_radius_dim } { ##1 } }
\draw_path_lineto:n
{ \draw_point_polar:nn { \l__circdiv_radius_dim } { ####1 } }
}
}
\draw_path_close:
\draw_path_use_clear:n { stroke }
\draw_end:
}
\NewDocumentCommand{\circdiv}{ s O{} }
{
% 可以定义一个星号命令以绘制交点(待研究)
% \IfBooleanTF{#1}
% {
% \bool_set_false:N \l__circdiv_intersection_bool
% }{
% \bool_set_true:N \l__circdiv_intersection_bool
% }
\group_begin:
\keys_set:nn { circdiv } {#2}
\__circdiv_div:
\group_end:
}
\ExplSyntaxOff
\begin{document}
\circdiv
\circdiv[cnt=36]
\end{document}
绘制结果为:
如何更改一下代码,只保留线线相交的点,而不画出线?