获取foreach循环中的下一组点,以便找到多边形的长度

时间:2015-07-04 18:15:38

标签: c#

我使用list存储多个多边形,并指向类来存储多边形的点。我可以单独得到每个点我需要计算多边形的长度。所以我也需要下一个点。如何获得它们

 foreach (Point point in and[reqi])
 {             
     int x1 = point.X;
     int y1 = point.Y;

     MessageBox.Show(x1.ToString());
     MessageBox.Show(y1.ToString());
 }

如何计算此部分中Class类的下一个点来计算多边形的长度?

1 个答案:

答案 0 :(得分:0)

不要忘记最后一段:

// Normal loop on each point : compute distance to previous point
float polyLength=0 ;
for (int i=0;i<PolyPoints[i].Count;i++) 
{ 
  float deltaX = (PolyPoint[i].X-PolyPoint[i-1].X) ;
  float deltaY = (PolyPoint[i].Y-PolyPoint[i-1].Y) ;
  PolyLength  += (float)Math.Sqrt(deltaX*deltaX+deltaY*deltaY) ;
}

if (PolyPoint[PolyPoint.Count-1]!=PolyPoint[0])
{ // last point different from first point : compute distance from last point to first one
  float deltaX = (PolyPoint[PolyPoint.Count-1].X-PolyPoint[0].X) ;
  float deltaY = (PolyPoint[PolyPoint.Count-1].Y-PolyPoint[0].Y) ;
  PolyLength  += (float)Math.Sqrt(deltaX*deltaX+deltaY*deltaY) ;
}