public tCoord clamp(tCoord min, tCoord max) { Debug.Assert(!(min.x > max.x || min.y > max.y)); int clamped_x = System.Math.Max(System.Math.Min(max.x, this.x), min.x); int clamped_y = System.Math.Max(System.Math.Min(max.y, this.y), min.y); return(new tCoord(clamped_x, clamped_y)); }
public static void unitTest() { //Framework.Assert( false, "hello" ); tCoord a = new tCoord(); Framework.Assert(a.x == 0); Framework.Assert(a.y == 0); tCoord b = a; Framework.Assert(b.x == 0); Framework.Assert(b.y == 0); tCoord c = new tCoord(-10, 20.0f); Framework.Assert(c.x == -10); Framework.Assert(c.y == 20); tCoord d = 5 * c; tCoord e = new tCoord(-50, 100); Framework.Assert(d == e); Framework.Assert(d != b); Framework.Assert(d.Equals(e)); Framework.Assert(!d.Equals(a)); #pragma warning disable CS1718 Framework.Assert(d == d); #pragma warning restore CS1718 tCoord f = new tCoord(2, 6); tCoord g = new tCoord(-11, 33); tCoord h = f + g; tCoord i = f - g; tCoord j = f - f; tCoord k = g - g - g; Framework.Assert(h == new tCoord(-9, 39)); Framework.Assert(i == new tCoord(13, -27)); Framework.Assert(j == new tCoord()); Framework.Assert(k == -g); tCoord l = new tCoord(2, -5); tCoord m = 4 * l; tCoord n = -3.5f * l; tCoord o = l * 4; Framework.Assert(m == new tCoord(8, -20)); Framework.Assert(n == new tCoord(-7, 17)); Framework.Assert(o == m); tCoord p = new tCoord(11.4, -55.6); tCoord q = new tCoord(11.6, -55.4); Framework.Assert(p == new tCoord(11, -55)); Framework.Assert(p == new tCoord(11, -55)); Framework.AssertVal(a.getLength(), 0, 0.0001); Framework.AssertVal(f.getLength(), Math.Sqrt(40), 0.0001); Framework.AssertVal(g.getLength(), Math.Sqrt(33 * 33 + 11 * 11), 0.001); tCoord r = g.clamp(new tCoord(0, 0), new tCoord(100, 100)); tCoord s = g.clamp(new tCoord(-30, -10), new tCoord(0, -1)); tCoord t = g.clamp(new tCoord(-1000, -1000), new tCoord(1000, 1000)); tCoord u = g.clamp(new tCoord(), new tCoord()); Framework.Assert(r == new tCoord(0, 33)); Framework.Assert(s == new tCoord(-11, -1)); Framework.Assert(t == g); Framework.Assert(u == new tCoord(0, 0)); }