|
|
Re: Random points in triangle
Posted:
Aug 9, 2010 5:15 AM
|
|
For a much simpler and probably naive approach, what about filtering for those random coordinates lying inside a triangle bounded by: y>x, y<x, y<1-x I know this is not very sophisticated, but randomly generated points inside a rectangle should be random inside a triangle also.
pts = RandomReal[{0, 1}, {10^5, 2}] ; ptsT = Select[ pts, ( #[[2]] >= 0 && #[[2]] < #[[1]] && #[[2]] < 1 - #[[1]] ) & ] ; ListPlot[ ptsT, PlotStyle -> PointSize[.001] ]
David
On 6 August 2010 17:55, S. B. Gray <stevebg@roadrunner.com> wrote:
> I was looking for a simple way to place random points inside a triangle > with uniform distribution. Here's a good way: > > newtri := Module[{x}, > ptri = RandomReal[{-5, +5}, {3, 2}]; > tredg = Subsets[ptri, {2}]; > ] > newpts[nump_] := Module[{wts}, > inpoints = {}; > Do [ wts = RandomReal[GammaDistribution[1, 2], 3]; > wts = wts/Total[wts]; > newin = Total[ptri*wts]; > inpoints = Append[inpoints, newin], {nump}]; > ] > shotri := Module[{x}, > Graphics[{Blue, Line[tredg], Red, Point[inpoints]}, ImageSize -> 500] > ] > > The same idea works for points in a tetrahedron; they will be uniformly > distributed if you use args such as GammaDistribution[.6,.1]. > > Steve Gray > >
|
|