private Point2D CalcPolygonCenter()
{
Point2DCollection point2Ds = new Point2DCollection();
Rectangle2D bounds = this.Bounds;
Point2D center = Point2D.Empty;
if (this.Parts == null || this.Parts.Count == 0 || bounds.IsEmpty)
{
return center;
}
int[] parts = new int[this.Parts.Count];
int point2DCount = 0;
foreach (var item in this.Parts)
{
foreach (var item1 in item)
{
point2Ds.Add(item1);
}
parts[point2DCount] = item.Count;
point2DCount++;
}
int i, crossNum, totalPoints;
double x1, y1, x2, y2;
double x3, y3, x4, y4;
double dist, maxLength;
int maxSegmentNo;
double sign1 = 0, sign2 = 0;
bool bAddRepPoint;
int n = 0;
int m = 0;
int lPointCount = 0;
double yMax;
double yMin;
int polyCountsMax = parts[0];
int flag = 0;
for (n = 0; n < parts.Length; n++)
{
if (polyCountsMax <= parts[n])
{
polyCountsMax = parts[n];
flag = n;
}
}
for (n = 0; n < flag; n++)
{
lPointCount += parts[n];
}
yMax = point2Ds[lPointCount].Y;
yMin = point2Ds[lPointCount].Y;
for (m = 0; m < parts[flag]; m++)
{
if (yMax <= point2Ds[lPointCount + m].Y)
{
yMax = point2Ds[lPointCount + m].Y;
}
if (yMin >= point2Ds[lPointCount + m].Y)
{
yMin = point2Ds[lPointCount + m].Y;
}
}
y1 = (yMax + yMin) / 2;
y2 = y1;
x1 = bounds.BottomLeft.X - Math.Abs(bounds.TopRight.X - bounds.BottomLeft.X);
x2 = bounds.TopRight.X + Math.Abs(bounds.TopRight.X - bounds.BottomLeft.X);
Point2D pntResult;
totalPoints = 0;
crossNum = 0;
double[] pSPointX = new double[1];
double y0 = 0;
for (int n1 = 0; n1 < parts.Length; n1++)
{
int crossOfN = 0;
for (int j1 = 1; j1 < parts[n1]; j1++)
{
x3 = point2Ds[totalPoints + j1 - 1].X;
y3 = point2Ds[totalPoints + j1 - 1].Y;
x4 = point2Ds[totalPoints + j1].X;
y4 = point2Ds[totalPoints + j1].Y;
if (((y4 - y3) > SMALL_VALUE) || ((y4 - y3) < -SMALL_VALUE))
{
bAddRepPoint = true;
if (((y3 - y1) > -SMALL_VALUE) && ((y3 - y1) < SMALL_VALUE))
{
y1 = y3;
y2 = y1;
}
else if (((y4 - y1) > -SMALL_VALUE) && ((y4 - y1) < SMALL_VALUE))
{
y1 = y4;
y2 = y1;
}
int nSpy = -1;
if (j1 == 1)
{
sign1 = point2Ds[totalPoints + parts[n1] - 2].Y - point2Ds[totalPoints].Y;
nSpy = parts[n1] - 2;
}
else
{
sign1 = (point2Ds[totalPoints + j1 - 2].Y - point2Ds[totalPoints + j1 - 1].Y);
nSpy = j1 - 2;
}
sign2 = y4 - y3;
if (((y3 - y1) > -SMALL_VALUE) && ((y3 - y1) < SMALL_VALUE))
{
while ((sign1 > -SMALL_VALUE) && (sign1 < SMALL_VALUE))
{
nSpy--;
if (nSpy < 0)
{
nSpy = parts[n1] - 2;
}
sign1 = point2Ds[totalPoints + nSpy].Y - point2Ds[totalPoints + j1 - 1].Y;
}
bAddRepPoint = ((sign1 * sign2) > 0);
}
}
else
{
bAddRepPoint = false;
}
if ((bAddRepPoint) && (this.IntersectLineSect(x1, y1, x2, y2, x3, y3, x4, y4, out pntResult)))
{
double[] tempPointX = new double[crossNum + 1];
int iLength = pSPointX.Length;
Array.Copy(pSPointX, 0, tempPointX, 0, iLength);
pSPointX = tempPointX;
pSPointX[crossNum] = pntResult.X;
crossOfN++;
crossNum++;
if (crossOfN > 1 && pSPointX[crossNum - 1] == pSPointX[crossNum - 2] && (y0 - y3) * (y4 - y3) < 0)
{
crossNum--;
crossOfN--;
}
y0 = y3;
}
}
if (crossNum - 1 >= 0 && crossNum - 1 < pSPointX.Length && pSPointX[crossNum - 1] == pSPointX[0])
{
y0 = point2Ds[totalPoints + 1].Y;
y3 = point2Ds[totalPoints].Y;
y4 = point2Ds[totalPoints + parts[n1] - 1].Y;
if ((y0 - y3) * (y4 - y3) < 0)
{
crossNum--;
}
}
totalPoints = totalPoints + Math.Abs(parts[n1]);
}
if (((crossNum % 2) == 0) && (crossNum >= 2))
{
this.QuickSort(pSPointX, 0, (crossNum - 1));
maxLength = -1.7E+308;
maxSegmentNo = -1;
for (i = 0; i <= (crossNum / 2 - 1); i++)
{
dist = pSPointX[2 * i + 1] - pSPointX[2 * i];
if (dist > maxLength)
{
maxLength = dist;
maxSegmentNo = i;
}
}
center.X = (pSPointX[2 * maxSegmentNo + 1] + pSPointX[2 * maxSegmentNo]) / 2;
center.Y = y1;
}
return center;
}