From 7ab0e00948ecea81bebb94a7582ecb89762714a2 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 24 Aug 2013 10:56:02 +1200 Subject: [PATCH] Add IEnumerable.Average overload. --- OpenRA.FileFormats/WPos.cs | 50 ++++++++++++---------- OpenRA.Game/Graphics/ContrailRenderable.cs | 7 ++- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/OpenRA.FileFormats/WPos.cs b/OpenRA.FileFormats/WPos.cs index 32ec012970..7ccc4d22f2 100644 --- a/OpenRA.FileFormats/WPos.cs +++ b/OpenRA.FileFormats/WPos.cs @@ -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 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); + } + } } diff --git a/OpenRA.Game/Graphics/ContrailRenderable.cs b/OpenRA.Game/Graphics/ContrailRenderable.cs index ef428044e0..b433aeff2e 100644 --- a/OpenRA.Game/Graphics/ContrailRenderable.cs +++ b/OpenRA.Game/Graphics/ContrailRenderable.cs @@ -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;