Create a random point within a polygon using arcobjects in C#?

1.1k views Asked by At

Im trying to create a function in C# that would return a random IPoint feature that whould be within a selected polygon, but I'm complete buffed on how to proceed.

Ideally the definiotion of the function would like bellow:

public IPoint Create_Random_Point(IGeometry inGeom)
2

There are 2 answers

2
user528025 On BEST ANSWER

Just for future reference I created a custom function that tries to find a random point within the extends of the polgon.

 private double GetRandomDouble(double Min, double Max)
        {
            //TODO:
            // seed
            Random random = new Random();
            return random.NextDouble() * (Max - Min) + Min;
        }



private IPoint Create_Random_Point(IGeometry inGeom)
        {

                double x = GetRandomDouble(inGeom.Envelope.XMin, inGeom.Envelope.XMax);
                double y = GetRandomDouble(inGeom.Envelope.YMin, inGeom.Envelope.YMax);


                IPoint p = new PointClass();
                p.X = x;
                p.Y = y;

                return p;
        }
2
pvdev On

There is a geoprocessing tool called CreateRandomPoints which can be used to generate points within a particular boundary (for example within the window extent, within a polygon, or along a line). Have a look:

http://resources.arcgis.com/en/help/arcobjects-java/api/arcobjects/com/esri/arcgis/geoprocessing/tools/datamanagementtools/CreateRandomPoints.html

Geoprocessing tools are fairly easy to implement into arcobjects code, but can sometimes be a little slow to execute.