Free Support Forum -

Iterate through all (x,y) points within polygon

I have created a polygon by adding points to a LinearRing and then set Polygon.ExteriorRing = LinearRing
Is there a way to iterate through all x,y points (pixels) that within that polygon? I need to be able to process each (x,y) pixel within the polygon

Thanks in advance for the help.

Hello, @Simon123Q!

Thank you for your interest in the Aspose.GIS product!

In the general case, our library processes figures located in continuous space. But for working with pixels, you can create a grid. And use the “intersection” function between the grid and the geometry (see the code below). In your case, this is a polygon. The intersection of two geometries with a large number of points is a time-consuming task, please check the performance with your data.

        // Create Polygon with Hole
        Polygon polygon = new Polygon();

        LinearRing ring = new LinearRing();
        ring.AddPoint(0, 0);
        ring.AddPoint(0, 100);
        ring.AddPoint(100, 100);
        ring.AddPoint(100, 0);
        ring.AddPoint(0, 0);

        LinearRing hole = new LinearRing();
        hole.AddPoint(30, 30);
        hole.AddPoint(30, 70);
        hole.AddPoint(70, 70);
        hole.AddPoint(70, 30);
        hole.AddPoint(30, 30);
        polygon.ExteriorRing = ring;

        // Emulate Pixel Field
        var pixels = new GeometryCollection();
        for (int x = 0; x < 100; x++)
        for (int y = 0; y < 100; y++)
            pixels.Add(new Point(x + 0.5, y + 0.5));

        // polygon content in “pixels”
        var subPixels = polygon.Intersection(pixels);

More examples for working with geometry:

Hope this helps. Thanks.

Great, that worked, but it seems way to slow for what I need. Thanks anyway