How to draw random circles

View: New views
5 Messages — Rating Filter:   Alert me  

How to draw random circles

by Rouben Rostamian-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here is latex file in its entirety:

%--- begin -----------------------
\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \draw (0,0) circle (rnd);
  \draw (2,0) circle (rnd);
  \draw (4,0) circle (rnd);
\end{tikzpicture}

\end{document}
%--- end -----------------------

The idea is to draw 3 circles with random radii.  My
reading of the documentation is that rnd returns a
random number in the range 0 to 1.

Then I don't know why instead of getting circles, the
code above produces ellipses of various sizes and aspect
ratios.  This is with tikz from the CVS that I checked
out in June.

--
Rouben Rostamian


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: How to draw random circles

by Alain Matthes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Le 27 sept. 2009 à 07:55, Rouben Rostamian a écrit :

> \documentclass{article}
> \usepackage{tikz}
> \begin{document}
>
> \begin{tikzpicture}
>  \draw (0,0) circle (rnd);
>  \draw (2,0) circle (rnd);
>  \draw (4,0) circle (rnd);
> \end{tikzpicture}
>
> \end{document}

Hi Rouben,

Perhaps "circle" is a shortcut for "ellipse" and the new syntax
uses "x radius" and "y radius"
For example
\draw (1,0) circle [x radius=1cm, y radius=5mm, rotate=30];
gives an ellipse.

Perhaps (???) with rnd you gives  x radius=rnd, y radius=rnd

A solution

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \pgfmathparse{random()}
  \pgfmathsetmacro{\mynb}{\pgfmathresult}
  \draw (rnd,rnd) circle (\mynb);
\end{tikzpicture}
\end{document}

Remark :

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \tikzset{r/.style={radius=#1},rx/.style={x radius=#1},ry/.style={y  
radius=#1}}
  \draw (1,0) circle [r=rnd];
\end{tikzpicture}

\end{document}

gives an ellipse

So you need to fix the argument but I'm not sure of my explanation !!

Best Regards

Hope this helps

Alain Matthes
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: How to draw random circles

by Mark Wibrow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Yes, rightly or wrongly (probably wrongly), rnd is calculated once for
the x radius and once for the y radius. The calc library provides
another way around:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
        \foreach \i in {1,...,5}
                \draw let \n1={rnd} in (rnd,rnd) circle (\n1);
\end{tikzpicture}

\end{document}

To take advantage of the newer syntax (can't actually remember when it
was introduced), you can hack into the radius key:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\makeatletter
\tikzset{
        radius/.code={%
                \pgfmathparse{#1}\let\tikz@temp@value=\pgfmathresult%
                \tikzset{x radius=\tikz@temp@value,y radius=\tikz@temp@value}%
        }
}
\makeatother

\begin{tikzpicture}
        \foreach \i in {1,...,5}
                \draw (rnd,rnd) circle [radius=rnd];
\end{tikzpicture}

\end{document}

regards

Mark

2009/9/27 Alain Matthes <alain.matthes@...>:

>
> Le 27 sept. 2009 à 07:55, Rouben Rostamian a écrit :
>
>> \documentclass{article}
>> \usepackage{tikz}
>> \begin{document}
>>
>> \begin{tikzpicture}
>>  \draw (0,0) circle (rnd);
>>  \draw (2,0) circle (rnd);
>>  \draw (4,0) circle (rnd);
>> \end{tikzpicture}
>>
>> \end{document}
>
> Hi Rouben,
>
> Perhaps "circle" is a shortcut for "ellipse" and the new syntax
> uses "x radius" and "y radius"
> For example
> \draw (1,0) circle [x radius=1cm, y radius=5mm, rotate=30];
> gives an ellipse.
>
> Perhaps (???) with rnd you gives  x radius=rnd, y radius=rnd
>
> A solution
>
> \documentclass{article}
> \usepackage{tikz}
> \begin{document}
>
> \begin{tikzpicture}
>  \pgfmathparse{random()}
>  \pgfmathsetmacro{\mynb}{\pgfmathresult}
>  \draw (rnd,rnd) circle (\mynb);
> \end{tikzpicture}
> \end{document}
>
> Remark :
>
> \documentclass{article}
> \usepackage{tikz}
> \begin{document}
>
> \begin{tikzpicture}
>  \tikzset{r/.style={radius=#1},rx/.style={x radius=#1},ry/.style={y
> radius=#1}}
>  \draw (1,0) circle [r=rnd];
> \end{tikzpicture}
>
> \end{document}
>
> gives an ellipse
>
> So you need to fix the argument but I'm not sure of my explanation !!
>
> Best Regards
>
> Hope this helps
>
> Alain Matthes
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
> _______________________________________________
> pgf-users mailing list
> pgf-users@...
> https://lists.sourceforge.net/lists/listinfo/pgf-users
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: How to draw random circles

by Alain Matthes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Le 27 sept. 2009 à 14:53, Mark Wibrow a écrit :

> To take advantage of the newer syntax (can't actually remember when it
> was introduced), you can hack into the radius key:
>
> \documentclass{article}
> \usepackage{tikz}
>
> \begin{document}
>
> \makeatletter
> \tikzset{
> radius/.code={%
> \pgfmathparse{#1}\let\tikz@temp@value=\pgfmathresult%
> \tikzset{x radius=\tikz@temp@value,y radius=\tikz@temp@value}%
> }
> }
> \makeatother

Thanks for this hack. It's very interesting to know the possibility to  
use
/.code like this. When I wrote my answer, I thought a few seconds to do
  something like that but
We can do a lot of things with \tikzset !

Best Regards

Alain Matthes
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: How to draw random circles

by Rouben Rostamian-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Sep 27, 2009 at 01:53:32PM +0100, Mark Wibrow wrote:

>> \begin{tikzpicture}
>>  \draw (0,0) circle (rnd);
>>  \draw (2,0) circle (rnd);
>>  \draw (4,0) circle (rnd);
>> \end{tikzpicture}
>>
>> \end{document}
>
> Yes, rightly or wrongly (probably wrongly), rnd is calculated once for
> the x radius and once for the y radius. The calc library provides
> another way around:
>
> \documentclass{article}
> \usepackage{tikz}
> \usetikzlibrary{calc}
> \begin{document}
>
> \begin{tikzpicture}
> \foreach \i in {1,...,5}
> \draw let \n1={rnd} in (rnd,rnd) circle (\n1);
> \end{tikzpicture}
>
> \end{document}

Thank you Mark, and Alain, for your explanations and
the workaround.

Till, if you read this message, you may want to look
into the unexpected behavior that started out this
thread and fix it if possible.

Rouben

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users