Add IEnumerable<WPos>.Average overload.

This commit is contained in:
Paul Chote
2013-08-24 10:56:02 +12:00
parent ea1b5fc89d
commit 7ab0e00948
2 changed files with 34 additions and 23 deletions

View File

@@ -9,6 +9,8 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
namespace OpenRA
@@ -50,28 +52,6 @@ namespace OpenRA
return new WPos(ret.X, ret.Y, ret.Z + offset);
}
public static WPos Average(params WPos[] list)
{
if (list == null || list.Length == 0)
return WPos.Zero;
var x = 0;
var y = 0;
var z = 0;
foreach(var pos in list)
{
x += pos.X;
y += pos.Y;
z += pos.Z;
}
x /= list.Length;
y /= list.Length;
z /= list.Length;
return new WPos(x,y,z);
}
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); }
public override bool Equals(object obj)
@@ -85,4 +65,30 @@ namespace OpenRA
public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); }
}
public static class IEnumerableExtensions
{
public static WPos Average(this IEnumerable<WPos> source)
{
var length = source.Count();
if (length == 0)
return WPos.Zero;
var x = 0L;
var y = 0L;
var z = 0L;
foreach (var pos in source)
{
x += pos.X;
y += pos.Y;
z += pos.Z;
}
x /= length;
y /= length;
z /= length;
return new WPos((int)x, (int)y, (int)z);
}
}
}