refactored Average method in PPos

This commit is contained in:
Sascha Biedermann
2013-03-19 19:58:30 +01:00
parent 021e1116b8
commit e562f8d2cc
2 changed files with 21 additions and 6 deletions

View File

@@ -29,8 +29,6 @@ namespace OpenRA
public static explicit operator PVecInt(PPos a) { return new PVecInt(a.X, a.Y); }
public static explicit operator PVecFloat(PPos a) { return new PVecFloat(a.X, a.Y); }
public static PPos operator /(PPos a, int b) { return new PPos(a.X / b , a.Y / b); }
public static PPos operator +(PPos a, PPos b) { return new PPos(a.X + b.X, a.Y + b.Y); }
public static PPos operator +(PPos a, PVecInt b) { return new PPos(a.X + b.X, a.Y + b.Y); }
public static PVecInt operator -(PPos a, PPos b) { return new PVecInt(a.X - b.X, a.Y - b.Y); }
public static PPos operator -(PPos a, PVecInt b) { return new PPos(a.X - b.X, a.Y - b.Y); }
@@ -46,6 +44,25 @@ namespace OpenRA
return a + ((PVecInt)(b - a) * mul / div);
}
public static PPos Average(params PPos[] list)
{
if (list == null || list.Length == 0)
throw new ArgumentException("PPos: Cannot calculate average of empty list.");
var x = 0;
var y = 0;
foreach(var pos in list)
{
x += pos.X;
y += pos.Y;
}
x /= list.Length;
y /= list.Length;
return new PPos(x,y);
}
public float2 ToFloat2() { return new float2(X, Y); }
public int2 ToInt2() { return new int2(X, Y); }
public CPos ToCPos() { return new CPos((int)(1f / Game.CellSize * X), (int)(1f / Game.CellSize * Y)); }