2

I'm trying to draw points of the same dimensions with the following code. My goal is to modify the code below to get the right result (I'd like to avoid different code):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,shapes.arrows,shadows}
\usepackage{xparse}



\def\height{3.6/3}
\def\width{3.9}
\def\numpoints{60}
\def\maxpointwidth{2}


\title{\textbf{Cerchi}}
\date{\vspace{-10ex}}

\begin{document}

\maketitle

\begin{center}
    \begin{tikzpicture}
    \draw[cyan,fill=cyan](0,0) -- (4,0) -- (4,4/3) -- (0,4/3) -- (0,0);
        \draw(0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);

        \foreach \point in {1,...,\numpoints}{
        \pgfmathparse{random()}
        \pgfmathsetmacro\xpos{\width*\pgfmathresult}
        \pgfmathparse{random()}
        \pgfmathsetmacro\ypos{\height*\pgfmathresult}
        \pgfmathrandom{0.1,\maxpointwidth}
        \let\pointwidth\pgfmathresult

        \node[circle,inner sep=\pointwidth pt,fill=yellow] (point-\point) at (\xpos,\ypos) {};
}
    \end{tikzpicture}
\end{center}

\end{document}

enter image description here

A colleague said me that the solutions are the keys height rect=4,width rect=8, but I don't know how use it.

Thank you so much for your help.

  • 1
    So you want \node[circle,inner sep=5pt,fill=yellow] (point-\point) at (\xpos,\ypos) {};, where you modify 5pt to get whatever size you'd like? – Torbjørn T. Nov 7 '17 at 13:07
1

The size of the points is determined by the \pointwidth macro. inner sep actually sets the distance from the content of the node to the edge of the node, so it is related to the radius. Might be more predictable to set inner sep=0pt,minimum size=\pointwidth, where the minimum size is the diameter of the points.

What \pgfmathrandom{0.1,\maxpointwidth} \let\pointwidth\pgfmathresult does is to set \pointwidth to a random value between 0.1 and \maxpointwidth. You want to disable the randomization, so set the \pointwidth to a fixed value instead.

Below is a slightly different implementation, though for the most part the same:

output of code

\documentclass{article}
\usepackage{tikz}

% \newcommand is safer than \def, you wont accidentally overwrite an existing macro
\newcommand\height{3.6/3}
\newcommand\width{3.9}
\newcommand\numpoints{60}
% new macro, to set point size
\newcommand\pointwidth{4pt}

\title{\textbf{Cerchi}}
\date{\vspace{-10ex}}

\begin{document}

\begin{center}
    \begin{tikzpicture}
    \draw[cyan,fill=cyan] (0,0) rectangle (4,4/3);
    \begin{scope} % to limit effect of \clip
    % if any circles end up partly outside frame, cut off that part
    \clip (0,0) rectangle (4,4/3);

    \foreach \point in {1,...,\numpoints}{
        \pgfmathsetmacro\xpos{\width*random}
        \pgfmathsetmacro\ypos{\height*random}

        \node[circle,inner sep=0pt,minimum size=\pointwidth,fill=yellow] (point-\point) at (\xpos,\ypos) {};
        }
    \end{scope}

    \draw(0,0) rectangle (4,4);
    \end{tikzpicture}
\end{center}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.