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);
}
}
}

View File

@@ -68,7 +68,7 @@ namespace OpenRA.Graphics
for (var i = 0; i < length - skip - 4; i++)
{
var j = next - skip - i - 2;
var nextPos = WPos.Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
var nextPos = Average(trail[idx(j)], trail[idx(j-1)], trail[idx(j-2)], trail[idx(j-3)]);
var nextCell = nextPos.ToCPos();
var nextColor = Exts.ColorLerp(i * 1f / (length - 4), color, Color.Transparent);
@@ -90,6 +90,11 @@ namespace OpenRA.Graphics
return j < 0 ? j + trail.Length : j;
}
WPos Average(params WPos[] list)
{
return list.Average();
}
public void Update(WPos pos)
{
trail[next] = pos;