Algorithm for finding a point in an irregular polygon

假装没事ソ 提交于 2019-12-03 03:14:11

You can use polygon triangulation to break your polygon apart into triangles.

One such algorithm is demonstrated using c# in this CodeProject article.

Once you have triangles, finding arbitrary points that lie within the triangle is easy. Any barycentric coordinate with a sum of 1.0 multiplied by the vertices of the triangle will give you a point inside the triangle.

The center can be derived using the barycentric coordinate [0.333333, 0.333333, 0.333333] :

float centerX = A.x * 0.333333 + B.x * 0.333333 + C.x * 0.3333333;
float centerY = A.y * 0.333333 + B.y * 0.333333 + C.y * 0.3333333;

or more simply:

float centerX = (A.x + B.x + C.x) / 3f;
float centerY = (A.y + B.y + C.y) / 3f;

Use This:

private Point getCentroid(pointArray)
{
   double centroidX = 0.0;
   double centroidY = 0.0;

   for (int i = 0; i < pointArray.Length; i++)
   {
          centroidX += pointArray[i].X;
          centroidY += pointArray[i].Y;`
   }
   centroidX /= pointArray.Length;
   centroidY /= pointArray.Length;

   return(new Point(centroidX ,centroidY));
}

this code is just to find Center of Mass of Polygon. To check whether a point is inside or outside polygon check this link http://bbs.dartmouth.edu/~fangq/MATH/download/source/Determining%20if%20a%20point%20lies%20on%20the%20interior%20of%20a%20polygon.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!