Select to view content in your preferred language

How to calculate Convex hull?

882
2
09-24-2010 09:14 AM
GangHan
Emerging Contributor
Hi, I want to calucate the convex hull of the point set and get the points of the convex hull polygon. The code is as follows:
public ArrayList ConvexHull(ArrayList pts)
        {
            IGeometryCollection geomCol = null;
            object missing = Type.Missing;
            IPoint geom;

            geom = new PointClass();
            geomCol = new MultipointClass();

            for (int i = 0; i < pts.Count; i++)
            {
                geom.X = ((MyPoint)pts).x;
                geom.Y = ((MyPoint)pts).z;
                geomCol.AddGeometry(geom as IGeometry, ref missing, ref missing);
            }           
           
            ITopologicalOperator topoOP =geomCol as ITopologicalOperator;
            if (!topoOP.IsSimple) topoOP.Simplify();
            IGeometry convexHull = topoOP.ConvexHull();
            IPointCollection ps = (convexHull as IPolygon) as IPointCollection;
            ArrayList c=new ArrayList();
            for (int i = 0; i < ps.PointCount; i++)
            {
                c.Add(ps.get_Point(i));
            }
            return c;
        }
But I didn't get right result. Why? Thank you very much.
0 Kudos
2 Replies
NeilClemmons
Honored Contributor
You're adding the same point object over and over.  The For loop is just changing the coordinates of that one point object.

geom = new PointClass();

This should be inside your For loop.
0 Kudos
GangHan
Emerging Contributor
That's right. Thank you very much.
You're adding the same point object over and over.  The For loop is just changing the coordinates of that one point object.

geom = new PointClass();

This should be inside your For loop.
0 Kudos