The following code takes in an point array and removes all of the nearby points whilst averaging them based on the conditions past in.
The variable _nCornerTolerance
is an int that is calculated prior in the class. Typically it is around 5;
private Point[] AverageNearbyPoints(Point[] points, bool averageLeft, bool averageHigh)
{
List<Point> plAveragedPoints = new List<Point>();
for (int pi = 0, pc = points.Length; pi < pc; ++pi)
{
Point[] paNearbyPoints = points.Where(p => (Math.Abs(p.X - points[pi].X) <= _nCornerTolerance)
&& (Math.Abs(p.Y - points[pi].Y) <= _nCornerTolerance))
.ToArray();
if (paNearbyPoints.Length > 1)
{
int nX = averageLeft ? (int)Math.Floor(paNearbyPoints.Sum(p => p.X) / (float)paNearbyPoints.Length)
: (int)Math.Ceiling(paNearbyPoints.Sum(p => p.X) / (float)paNearbyPoints.Length);
int nY = averageHigh ? (int)Math.Floor(paNearbyPoints.Sum(p => p.Y) / (float)paNearbyPoints.Length)
: (int)Math.Ceiling(paNearbyPoints.Sum(p => p.Y) / (float)paNearbyPoints.Length);
if (!plAveragedPoints.Any(p => p.X == nX && p.Y == nY))
{
plAveragedPoints.Add(new Point(nX, nY));
}
}
else
{
if (!plAveragedPoints.Any(p => p.X == points[pi].X && p.Y == points[pi].Y))
{
plAveragedPoints.Add(points[pi]);
}
}
}
return plAveragedPoints.ToArray();
}
Is there anyway I can rewrite the int nX = ...
and int nY = ...
parts better so I don't need the conditional?
A full review of this code would also be helpful, although the main part I am asking about is the above.