0

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)
user528025
  • 732
  • 2
  • 10
  • 24

2 Answers2

1

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.

pvdev
  • 230
  • 3
  • 13
-1

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;
        }
user528025
  • 732
  • 2
  • 10
  • 24
  • This doesn't necessarily return a point in the polygon. It's even possible to define a polygon with a vanishingly small chance of this returning a point within it. – Rawling Dec 02 '13 at 13:13
  • Yes that's of its weakness. once the polygon area becomes much smaller in regard with the area of the extend the function breaks. (well technically the possibility is still there but with he very small change o hitting a viable solution i loses its functionality) – user528025 Dec 02 '13 at 13:31