[SciPy-User] random points within an ellipse
Thu Aug 5 01:54:23 CDT 2010
Whoops, sorry, in the example, the call to rand_in_ellipse should have 4./3
as a single argument, i.e., the line should look like
sample = np.array([rand_in_ellipse(4./3) for i in np.arange(10000)])
On Wed, Aug 4, 2010 at 11:50 PM, David Goldsmith <firstname.lastname@example.org>wrote:
> For the ellipse:
> import numpy as np
> from numpy.random import random_sample as random
> pi = np.pi
> def rand_in_ellipse(a, b=1, offset=0):
> angle = 2*pi*random() - offset
> x = a * random() * np.cos(angle)
> y = b * random() * np.sin(angle)
> return np.array((x,y))
> import matplotlib.pyplot as plt
> fig = plt.figure()
> ax = fig.add_subplot(111)
> sample = np.array([rand_in_ellipse() for i in np.arange(10000)])
> ax.scatter(sample[:,0], sample[:,1])
> plt.show() # Figure attached
> On Wed, Aug 4, 2010 at 8:13 PM, Benjamin Root <email@example.com> wrote:
>> On Wed, Aug 4, 2010 at 7:18 PM, alex <firstname.lastname@example.org> wrote:
>>> On Wed, Aug 4, 2010 at 7:38 PM, Benjamin Root <email@example.com> wrote:
>>>> For a project, I need to create sets of random coordinates within a 2d
>>>> domain. To start, I have been creating random x and y coordinates, which
>>>> has worked very nicely. However, I would like to start doing some fancier
>>>> domains like ellipses and crescents. Does anybody know of any useful tricks
>>>> for doing this?
>>>> Ben Root
>>> For an ellipse you could start with random points in a disk like this
>>> and then I think you can just stretch along an axis to give the points
>>> inside the ellipse disk.
>>> For fancier shapes you can do rejection sampling as long as you can get a
>>> bounding box and
>>> you can tell what is inside vs outside the domain.
>> Thanks Alex, that certainly does put me in the correct direction. It
>> appears that there isn't an analogous expression for an ellipse as there was
>> for a disc. I probably will have to just take your suggestion and do some
>> scaling/rotation upon the set of points to get what I want. It should be
>> more than sufficient, and it does look better than my original attempts.
>> Ben Root
>> SciPy-User mailing list
> Mathematician: noun, someone who disavows certainty when their uncertainty
> set is non-empty, even if that set has measure zero.
> Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
> lies, prevents mankind from committing a general suicide. (As interpreted
> by Robert Graves)
Mathematician: noun, someone who disavows certainty when their uncertainty
set is non-empty, even if that set has measure zero.
Hope: noun, that delusive spirit which escaped Pandora's jar and, with her
lies, prevents mankind from committing a general suicide. (As interpreted
by Robert Graves)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User