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$
1

1 Answer 1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.