Divide integers and get a floating point number (for a pstricks coordinate)

Divide integers and get a floating point number (for a pstricks coordinate)

I'm making a picture with pstricks and would like to plot points whose coordinates are fractions a/c, b/c, where a, b, and c run over certain ranges. I tried using \numexpr but it rounds down. I wouldn't use \dimexpr, as I assume that a dimension (ending with pt or suchlike) isn't welcome as a pstricks coordinate. I tried using pgf but I can't get the syntax to work:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks,pst-node,multido}
\RequirePackage{pgf}%
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
  \multido{\iA=0+1}{\iC}{%
    \multido{\iB=0+1}{\iC}{%
      \pscircle*(\pgfmathparse{\iA/\iC},\pgfmathparse{\iB/\iC}){0.02}
    }
  }
}
\end{pspicture}
\end{document}

Desired output (produced using TikZ, which works for this 5x5 example but runs out TeX's memory on the larger scales I want to use). Desired output

답변1

\documentclass{article}
\usepackage{pstricks,multido}
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
    \multido{\iA=0+1}{\iC}{%
        \multido{\iB=0+1}{\iC}{%
            \psdot(!\iA\space \iC\space div \iB\space \iC\space div)
        }%
    }%
}%
\end{pspicture}
\end{document}

or

\documentclass{article}
\usepackage{pstricks,multido,pst-calculate}
\begin{document}

\psset{unit=8}
\begin{pspicture}(2,2)
\multido{\iC=1+1}{20}{%
    \multido{\iA=0+1}{\numexpr\iC+1}{%
        \multido{\iB=0+1}{\numexpr\iC+1}{%
            \psdot[dotscale=0.5](\pscalculate{\iA/\iC},\pscalculate{\iB/\iC})
        }%
    }%
}%
\end{pspicture}
\end{document}

enter image description here

답변2

You forgot to invoke \pgfmathresult.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pstricks,pst-node,multido}
\RequirePackage{pgf}%
\begin{document}

\psset{xunit=5cm,yunit=5cm}
\begin{pspicture}(0,0)(1,1)
\multido{\iC=1+1}{5}{%
  \multido{\iA=0+1}{\iC}{%
    \multido{\iB=0+1}{\iC}{%
    \pgfmathparse{\iA/\iC}\xdef\myx{\pgfmathresult}
    \pgfmathparse{\iB/\iC}\xdef\myy{\pgfmathresult}
    \pscircle*(\myx,\myy){0.02}
    }
  }
}
\end{pspicture}
\end{document}

Just for curiosity, could you please also add your TikZ example? I am a bit surprised about the error you get.

UPDATE: I do not get any problems when doing this with TikZ, where the code is shorter, no packages are required and the compilation is more convenient (even though I really like PSTricks).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=10]
\foreach \iC in {1,...,15}{%
  \foreach \iA in {0,...,\iC}{%
   \foreach \iB in {0, ...,\iC}{%
      \draw[fill=black] ({\iA/\iC},{\iB/\iC}) circle (0.002);
    }
  }
}
\end{tikzpicture}
\end{document}

A 15x15 example

답변3

Also available in Metapost, here wrapped up in luamplib so compile with lualatex or work out how to adapt it for GMP, or plain MP.

enter image description here

I've added a bit of color too.

\documentclass[border=5mm]{standalone}
\usepackage{luatex85, luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    n = 25;
    for i = 1 upto n:
        for j = 0 upto i:
            for k = 0 upto i:
                fill fullcircle 
                     scaled 3 
                     shifted ((j/i, k/i) scaled (n*cm)) 
                     withcolor 3/4(j/i, k/i, i/n);
            endfor
        endfor
    endfor
endfig;
\end{mplibcode}
\end{document}

관련 정보