5
$\begingroup$

I am a newbie to Mathematica. I have some 2D point data, and I generated a ConvexHullMesh from it. Now, I want to generate a certain number of random points within the convex hull. What is the best way to do it?

$\endgroup$
12
$\begingroup$

A convex hull returned by ConvexHullMesh can be used directly as a region specification in RandomPoint.

Below I generate a few random 2D points to use to generate a convex hull, then use that hull as a region to generate five new points with RandomPoint. The original points are shown in Black; the new random ones in red.

originalPoints = RandomReal[{-10, 10}, {7, 2}];
hull = ConvexHullMesh@originalPoints;
newpts = RandomPoint[hull, 5];

Show[
 hull,
 Graphics[{
   PointSize[0.01], Point@originalPoints,
   Red, PointSize[0.02], Point@newpts
 }]
]

Mathematica graphics

$\endgroup$

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.