Search code examples
c#arcobjects

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


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)

Solution

  • 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;
            }