diff options
author | Karl Berry <karl@freefriends.org> | 2015-03-04 00:13:27 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2015-03-04 00:13:27 +0000 |
commit | ff6fb76f8500ebd621ed48973af743f6d371ef53 (patch) | |
tree | 73bedb11e5d64719ba196e11f887b49a9043b7e7 /Master/texmf-dist/tex/latex | |
parent | 47d688b3f0de132c50103bd25fbd617dc7e213f0 (diff) |
randomwalk (3mar15)
git-svn-id: svn://tug.org/texlive/trunk@36429 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex')
-rw-r--r-- | Master/texmf-dist/tex/latex/randomwalk/randomwalk.sty | 120 |
1 files changed, 63 insertions, 57 deletions
diff --git a/Master/texmf-dist/tex/latex/randomwalk/randomwalk.sty b/Master/texmf-dist/tex/latex/randomwalk/randomwalk.sty index 6e57ab32313..10ff54412c7 100644 --- a/Master/texmf-dist/tex/latex/randomwalk/randomwalk.sty +++ b/Master/texmf-dist/tex/latex/randomwalk/randomwalk.sty @@ -12,7 +12,7 @@ %% to Bruno Le Floch (see the latex-randomwalk repository %% on GitHub). %% -%% File: randomwalk.dtx Copyright (C) 2011-2013 Bruno Le Floch +%% File: randomwalk.dtx Copyright (C) 2011-2015 Bruno Le Floch %% %% It may be distributed and/or modified under the conditions of the %% LaTeX Project Public License (LPPL), either version 1.3c of this @@ -22,11 +22,11 @@ %% http://www.latex-project.org/lppl.txt %% %% ----------------------------------------------------------------------- -\RequirePackage {expl3} [2012/08/14] +\RequirePackage{expl3}[2012/08/14] \ProvidesExplPackage - {randomwalk.sty} {2013/01/08} {0.2c} {Customizable random walks using TikZ} -\RequirePackage {xparse} [2012/08/14] -\RequirePackage {tikz} + {randomwalk.sty} {2015/03/03} {0.3} {Customizable random walks using TikZ} +\RequirePackage{xparse}[2012/08/14] +\RequirePackage{tikz} \int_const:Nn \c__randomwalk_lcg_last_int { \c_max_int - \c_one } \AtBeginDocument { @@ -39,11 +39,14 @@ { lcg } \rand % This \rand avoids some very odd bug. } +\tl_new:N \l__randomwalk_internal_tl +\int_new:N \l__randomwalk_internal_int \int_new:N \l__randomwalk_step_number_int \bool_new:N \l__randomwalk_relative_angles_bool +\bool_new:N \l__randomwalk_degrees_bool \bool_new:N \l__randomwalk_revert_random_bool -\cs_new_protected_nopar:Npn \__randomwalk_rand_angle: { } -\cs_new_protected_nopar:Npn \__randomwalk_rand_length: { } +\cs_new_protected_nopar:Npn \__randomwalk_next_angle: { } +\cs_new_protected_nopar:Npn \__randomwalk_next_length: { } \fp_new:N \l__randomwalk_angle_fp \fp_new:N \l__randomwalk_length_fp \fp_new:N \l__randomwalk_old_x_fp @@ -53,6 +56,8 @@ \seq_new:N \l__randomwalk_angles_seq \seq_new:N \l__randomwalk_lengths_seq \DeclareDocumentCommand \RandomWalk { m } + { \randomwalk:n { #1 } } +\cs_new_protected:Npn \randomwalk:n #1 { \__randomwalk_set_defaults: \keys_set:nn { randomwalk } { #1 } @@ -61,9 +66,9 @@ \cs_new:Npn \__randomwalk_set_defaults: { \int_set:Nn \l__randomwalk_step_number_int {10} - \cs_gset_protected_nopar:Npn \__randomwalk_rand_angle: - { \__randomwalk_fp_set_rand:Nnn \l__randomwalk_angle_fp { - pi } { pi } } - \cs_gset_protected_nopar:Npn \__randomwalk_rand_length: + \cs_gset_protected_nopar:Npn \__randomwalk_next_angle: + { \__randomwalk_fp_set_rand:Nnn \l__randomwalk_angle_fp { 0 } { 360 } } + \cs_gset_protected_nopar:Npn \__randomwalk_next_length: { \fp_set:Nn \l__randomwalk_length_fp {10} } \bool_set_false:N \l__randomwalk_revert_random_bool \bool_set_false:N \l__randomwalk_relative_angles_bool @@ -79,85 +84,86 @@ \seq_set_split:Nnn \l__randomwalk_lengths_seq { , } {#1} \seq_set_map:NNn \l__randomwalk_lengths_seq \l__randomwalk_lengths_seq { \dim_to_fp:n {##1} } - \int_compare:nNnTF { \seq_count:N \l__randomwalk_lengths_seq } = {1} + \cs_gset_protected_nopar:Npn \__randomwalk_next_length: { - \cs_gset_protected_nopar:Npn \__randomwalk_rand_length: - { \fp_set:Nn \l__randomwalk_length_fp {#1} } - } - { - \cs_gset_protected_nopar:Npn \__randomwalk_rand_length: - { - \__randomwalk_fp_set_rand_seq_item:NN - \l__randomwalk_length_fp \l__randomwalk_lengths_seq - } + \__randomwalk_get_rand_seq_item:NN + \l__randomwalk_lengths_seq \l__randomwalk_internal_tl + \fp_set:Nn \l__randomwalk_length_fp { \l__randomwalk_internal_tl } } } , angles .code:n = { \seq_set_split:Nnn \l__randomwalk_angles_seq { , } {#1} - \cs_gset_protected_nopar:Npn \__randomwalk_rand_angle: + \seq_set_map:NNn \l__randomwalk_angles_seq + \l__randomwalk_angles_seq { \fp_to_tl:n {##1} } + \cs_gset_protected_nopar:Npn \__randomwalk_next_angle: { + \__randomwalk_get_rand_seq_item:NN + \l__randomwalk_angles_seq \l__randomwalk_internal_tl + \bool_if:NF \l__randomwalk_degrees_bool + { \tl_put_right:Nn \l__randomwalk_internal_tl { rad } } \bool_if:NTF \l__randomwalk_relative_angles_bool - { \__randomwalk_fp_add_rand_seq_item:NN } - { \__randomwalk_fp_set_rand_seq_item:NN } - \l__randomwalk_angle_fp \l__randomwalk_angles_seq + { \fp_add:Nn } { \fp_set:Nn } + \l__randomwalk_angle_fp { \l__randomwalk_internal_tl } } } , - degree .code:n = - { \__randomwalk_radians_from_degrees:N \l__randomwalk_angles_seq } , - degrees .code:n = - { \__randomwalk_radians_from_degrees:N \l__randomwalk_angles_seq } , - angles-relative .code:n = - { \bool_set_true:N \l__randomwalk_relative_angles_bool } , + degree .bool_set:N = \l__randomwalk_degrees_bool , + degrees .bool_set:N = \l__randomwalk_degrees_bool , + angles-relative .bool_set:N = \l__randomwalk_relative_angles_bool , revert-random .bool_set:N = \l__randomwalk_revert_random_bool , } -\cs_new:Npn \__randomwalk_radians_from_degrees:N #1 - { \seq_set_map:NNn #1 #1 { \fp_eval:n { ##1 deg } } } \cs_new:Npn \__randomwalk_walk: { \begin{tikzpicture} + \fp_zero:N \l__randomwalk_angle_fp + \fp_zero:N \l__randomwalk_length_fp \fp_zero:N \l__randomwalk_old_x_fp \fp_zero:N \l__randomwalk_old_y_fp \fp_zero:N \l__randomwalk_new_x_fp \fp_zero:N \l__randomwalk_new_y_fp - \prg_replicate:nn { \l__randomwalk_step_number_int } { \__randomwalk_step_draw: } + \prg_replicate:nn { \l__randomwalk_step_number_int } + { \__randomwalk_step_draw: } \bool_if:NF \l__randomwalk_revert_random_bool { \int_gset_eq:NN \cr@nd \cr@nd } \end{tikzpicture} } \cs_new:Npn \__randomwalk_step_draw: { - \__randomwalk_rand_length: - \__randomwalk_rand_angle: + \__randomwalk_next_length: + \__randomwalk_next_angle: \fp_set_eq:NN \l__randomwalk_old_x_fp \l__randomwalk_new_x_fp \fp_set_eq:NN \l__randomwalk_old_y_fp \l__randomwalk_new_y_fp - \fp_add:Nn \l__randomwalk_new_x_fp { \l__randomwalk_length_fp * cos \l__randomwalk_angle_fp } - \fp_add:Nn \l__randomwalk_new_y_fp { \l__randomwalk_length_fp * sin \l__randomwalk_angle_fp } - \draw ( \fp_to_dim:N \l__randomwalk_old_x_fp, \fp_to_dim:N \l__randomwalk_old_y_fp ) - -- ( \fp_to_dim:N \l__randomwalk_new_x_fp, \fp_to_dim:N \l__randomwalk_new_y_fp ); - } -\cs_new:Npn \__randomwalk_int_set_rand:Nnn #1#2#3 - { - \rand - \int_set:Nn #1 { #2 + \int_mod:nn {\c@lcg@rand} { #3 + 1 - (#2) } } + \fp_add:Nn \l__randomwalk_new_x_fp + { \l__randomwalk_length_fp * cosd \l__randomwalk_angle_fp } + \fp_add:Nn \l__randomwalk_new_y_fp + { \l__randomwalk_length_fp * sind \l__randomwalk_angle_fp } + \draw ( \fp_to_dim:N \l__randomwalk_old_x_fp , + \fp_to_dim:N \l__randomwalk_old_y_fp ) + -- ( \fp_to_dim:N \l__randomwalk_new_x_fp , + \fp_to_dim:N \l__randomwalk_new_y_fp ); } -\cs_new_nopar:Npn \__randomwalk_fp_set_rand:Nnn - { \__randomwalk_fp_set_rand_aux:NNnn \fp_set:Nn } -\cs_new_nopar:Npn \__randomwalk_fp_add_rand:Nnn - { \__randomwalk_fp_set_rand_aux:NNnn \fp_add:Nn } -\cs_new:Npn \__randomwalk_fp_set_rand_aux:NNnn #1#2#3#4 +\cs_new_protected:Npn \__randomwalk_fp_set_rand:Nnn #1#2#3 { \rand - #1 #2 { #3 + (#4 - (#3)) * \c@lcg@rand / \c__randomwalk_lcg_last_int } + \fp_set:Nn #1 + { #2 + (#3 - (#2)) * \c@lcg@rand / \c__randomwalk_lcg_last_int } } -\cs_new_protected_nopar:Npn \__randomwalk_fp_set_rand_seq_item:NN - { \__randomwalk_fp_set_rand_item_aux:NNNNN \fp_set:Nn \seq_item:Nn \seq_count:N } -\cs_new_protected_nopar:Npn \__randomwalk_fp_add_rand_seq_item:NN - { \__randomwalk_fp_set_rand_item_aux:NNNNN \fp_add:Nn \seq_item:Nn \seq_count:N } -\cs_new_protected:Npn \__randomwalk_fp_set_rand_item_aux:NNNNN #1#2#3#4#5 +\cs_new_protected:Npn \__randomwalk_get_rand_seq_item:NN #1#2 { - \rand - #1 #4 { #2 #5 { 1 + \int_mod:nn { \c@lcg@rand } { #3 #5 } } } + \int_set:Nn \l__randomwalk_internal_int { \seq_count:N #1 } + \int_compare:nTF { \l__randomwalk_internal_int = 1 } + { \tl_set:Nx #2 { \seq_item:Nn #1 { 1 } } } + { + \rand + \tl_set:Nx #2 + { + \seq_item:Nn #1 + { + 1 + + \int_mod:nn { \c@lcg@rand } { \l__randomwalk_internal_int } + } + } + } } %% %% |