Graphics, Figures & Tables[pstricks] psRandom - distance to clipping object

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
mushroom20
Posts: 2
Joined: Wed Dec 14, 2016 10:44 am

[pstricks] psRandom - distance to clipping object

Post by mushroom20 »

Dear all,

I would like to draw a certain amount of dots or circles randomly arranged in an ellipse. So I used the command \psRandom from the package pstricks-add and got the following result

Code: Select all

\documentclass[a4paper]{scrbook}

\usepackage[ngerman]{babel}
\usepackage{pstricks-add}

\begin{document}
  \begin{pspicture}(4,4)
  \definecolor{orange}{cmyk}{0,0.61,0.87,0.001}
  \psgrid[gridcolor=orange, gridwidth=0.5pt,subgridwidth=0.1pt, subgridcolor=orange,subgriddiv=10]
  \psRandom[dotsize=0.5mm, randomPoints=500](1.9,1)(2.9,2){%
  \psellipse[linestyle=none](2.5,1.5)(0.5,0.3)}
  \end{pspicture}
\end{document}
Unfortunately, the dots at the edge of the ellipse are cut, because \psRandom arranges the dots in a rectangular object. Is there a possibility to arrange the dots with a certain distance to the edge of the ellipse, so that all dots are completely within the ellipse and not cut anymore?

I posted the issue also in http://golatex.de/pstricks-psrandom-abs ... 18455.html in german.

Thanks
Markus

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

[pstricks] psRandom - distance to clipping object

Post by Stefan Kottwitz »

Hi Markus,

welcome to the forum!

Very good!
  • You made an excellent minimal example.
  • You linked the cross-post for information.
So I really want to answer even though I'm not so familiar with PSTricks, but with TikZ. So here is a TikZ solution - you can adapt it to PSTricks of cause.

Truly random points can look bad: some dense clusters, some big gaps. So I would do a small trick: distribute the points equally along the x axis, and make the y value random. Using the math function for an ellipse, you can plot a function in x with the value of the y height multiplied with a random value between -1 and 1. Make it a bit smaller to stay inside the ellipse.

Code: Select all

\documentclass[tikz, border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\pgfmathsetseed{5}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [
      hide axis,
      axis equal
    ]
    \addplot [only marks, samples=120, domain=-3.9:3.9]
      ({ x },{rand * (sqrt( 2^2 * (1 - \x^2/(4^2) ) - 0.4)} );
    \draw (0,0) ellipse [x radius=4, y radius=2];
  \end{axis}
\end{tikzpicture}
\end{document}
random-circles.png
random-circles.png (17.33 KiB) Viewed 5374 times
Of course you could also make a loop and make the x value random, the way is the same. I can show that too, if desired. I just like that a bit equally distributed still random looking plot.

Stefan
LaTeX.org admin
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

[pstricks] psRandom - distance to clipping object

Post by CrazyHorse »

Code: Select all

\documentclass[pstricks]{standalone}
\usepackage{pst-plot}
\begin{document}
	
\begin{pspicture}[showgrid](-4.3,-2.3)(4,2.2)
\psplot[plotstyle=dots,algebraic,plotpoints=200]{-3.9}{3.9}{Rand*(sqrt(4-x^2/4)-0.1)}
\psplot[plotstyle=dots,algebraic,plotpoints=200]{-3.9}{3.9}{Rand*(-sqrt(4-x^2/4)+0.1)}
\psellipse(0,0)(4,2)
\end{pspicture}
\end{document}
Bildschirmfoto vom 2017-01-19 17-45-12.png
Bildschirmfoto vom 2017-01-19 17-45-12.png (38.71 KiB) Viewed 4574 times
mushroom20
Posts: 2
Joined: Wed Dec 14, 2016 10:44 am

[pstricks] psRandom - distance to clipping object

Post by mushroom20 »

Thanks, Crazyhorse

with your hint, it's also working now in my case.
Post Reply