public void referenceFrameEquivalence() { CellVector cv1a = new CellVector(CellIndex.topMiddle, 10); CellVector cv1b = new CellVector(CellIndex.topLeft, 8); PatternCoordinate pc1 = new PatternCoordinate(0, 0) + (cv1a - cv1b); PatternCoordinate pc2 = (new PatternCoordinate(0, 0) + cv1a) - cv1b; Assert.AreEqual(pc1.col, pc2.col); Assert.AreEqual(pc1.row, pc2.row); cv1a = new CellVector(2, -1); cv1b = new CellVector(CellIndex.topLeft, 8); pc1 = new PatternCoordinate(0, 0) + (cv1a - cv1b); pc2 = (new PatternCoordinate(0, 0) + cv1a) - cv1b; Assert.AreEqual(pc1.col, pc2.col); Assert.AreEqual(pc1.row, pc2.row); }
public void TestArithmetic() { PatternCoordinate pc = new PatternCoordinate(0, 0); CellVector cv = new CellVector(0, 0); PatternCoordinate exp = new PatternCoordinate(0, 0); PatternCoordinate act = new PatternCoordinate(0, 0); // pc(3,0) + cv(3,0) = pc(6,2) pc.col = 3; pc.row = 0; cv.i = 3; cv.j = 0; exp.col = 6; exp.row = 2; act = pc + cv; Assert.AreEqual(exp.col, act.col, "col: pc(3,0) + cv(3,0) = pc(6,2)"); Assert.AreEqual(exp.row, act.row, "row: pc(3,0) + cv(3,0) = pc(6,2)"); // pc(5,1) + cv(2,1) = pc(7,3) pc.col = 5; pc.row = 1; cv.i = 2; cv.j = 1; exp.col = 7; exp.row = 3; act = pc + cv; Assert.AreEqual(exp.col, act.col, "col: pc(5,1) + cv(2,1) = pc(7,3)"); Assert.AreEqual(exp.row, act.row, "row: pc(5,1) + cv(2,1) = pc(7,3)"); // pc(0,0) + cv(2,0) = pc(2,1) pc.col = 0; pc.row = 0; cv.i = 2; cv.j = 0; exp.col = 2; exp.row = 1; act = pc + cv; Assert.AreEqual(exp.col, act.col, "col: pc(0,0) + cv(2,0) = pc(2,1)"); Assert.AreEqual(exp.row, act.row, "row: pc(0,0) + cv(2,0) = pc(2,1)"); // pc(0,0) + cv(3,0) = pc(3,1) pc.col = 0; pc.row = 0; cv.i = 3; cv.j = 0; exp.col = 3; exp.row = 1; act = pc + cv; Assert.AreEqual(exp.col, act.col, "col: pc(0,0) + cv(3,0) = pc(3,1)"); Assert.AreEqual(exp.row, act.row, "row: pc(0,0) + cv(3,0) = pc(3,1)"); // pc(0,0) - cv(1,0) = pc(-1,-1) pc.col = 0; pc.row = 0; cv.i = 1; cv.j = 0; exp.col = -1; exp.row = -1; act = pc - cv; Assert.AreEqual(exp.col, act.col, "col: pc(0,0) - cv(1,0) = pc(-1,-1)"); Assert.AreEqual(exp.row, act.row, "row: pc(0,0) - cv(1,0) = pc(-1,-1)"); // pc(0,0) - cv(2,0) = pc(-2,-1) pc.col = 0; pc.row = 0; cv.i = 2; cv.j = 0; exp.col = -2; exp.row = -1; act = pc - cv; Assert.AreEqual(exp.col, act.col, "col: pc(0,0) - cv(2,0) = pc(-2,-1)"); Assert.AreEqual(exp.row, act.row, "row: pc(0,0) - cv(2,0) = pc(-2,-1)"); // pc(1,0) - cv(2,0) = pc(-1,-1) pc.col = 1; pc.row = 0; cv.i = 2; cv.j = 0; exp.col = -1; exp.row = -1; act = pc - cv; Assert.AreEqual(exp.col, act.col, "col: pc(1,0) - cv(2,0) = pc(-1,-1)"); Assert.AreEqual(exp.row, act.row, "row: pc(1,0) - cv(2,0) = pc(-1,-1)"); // pc(1,2) - cv(5,0) = pc(-4,0) pc.col = 1; pc.row = 2; cv.i = 5; cv.j = 0; exp.col = -4; exp.row = 0; act = pc - cv; Assert.AreEqual(exp.col, act.col, "col: pc(1,2) - cv(5,0) = pc(-4,0)"); Assert.AreEqual(exp.row, act.row, "row: pc(1,2) - cv(5,0) = pc(-4,0)"); // (pc(7,10) + cv(3,12)) - cv(3,12) = pc(7,10) pc.col = 7; pc.row = 10; cv.i = 3; cv.j = 13; exp.col = 7; exp.row = 10; act = (pc + cv) - cv; Assert.AreEqual(exp.col, act.col, "col: (pc(7,10) + cv(3,12)) - cv(3,12) = pc(7,10)"); Assert.AreEqual(exp.row, act.row, "row: (pc(7,10) + cv(3,12)) - cv(3,12) = pc(7,10)"); // (pc(2,10) - cv(8,99)) + cv(8,99) = pc(2,10) pc.col = 2; pc.row = 10; cv.i = 8; cv.j = 99; exp.col = 2; exp.row = 10; act = (pc - cv) + cv; Assert.AreEqual(exp.col, act.col, "col: (pc(2,10) - cv(8,99)) + cv(8,99) = pc(2,10)"); Assert.AreEqual(exp.row, act.row, "row: (pc(2,10) - cv(8,99)) + cv(8,99) = pc(2,10)"); }
public void traversal() { int[] exps = { 5, 2, 6, 2, 7, 1, 8, 1 }; PatternCoordinate pc = new PatternCoordinate(0, 0) + new CellVector(5, 0); for (int i=0; i<exps.Length/2; i+=2) { Assert.AreEqual(exps[i ], pc.col, "col: i="+i); Assert.AreEqual(exps[i+1], pc.row, "row: i="+i); pc += new CellVector(1, -1); } }
public void vec2Conversion() { Vector2 vec2 = Vector2.zero; CellVector exp = new CellVector(0, 0); CellVector act = new CellVector(0, 0); // vec2(0,-0.51) -> cv(0,-1) vec2.x = 0.0f; vec2.y = -0.51f; exp.i = 0; exp.j = -1; act = CellVector.fromVector2(vec2); Assert.AreEqual(exp.i, act.i, "i: vec2(0,-0.51) -> cv(0,-1)"); Assert.AreEqual(exp.j, act.j, "j: vec2(0,-0.51) -> cv(0,-1)"); // vec2(1.16,0) -> cv(2,-1) vec2.x = 1.16f; vec2.y = 0.0f; exp.i = 2; exp.j = -1; act = CellVector.fromVector2(vec2); Assert.AreEqual(exp.i, act.i, "i: vec2(1.16,0) -> cv(2,-1)"); Assert.AreEqual(exp.j, act.j, "j: vec2(1.16,0) -> cv(2,-1)"); // vec2(0.44,0.44) -> cv(1,0) vec2.x = 0.44f; vec2.y = 0.44f; exp.i = 1; exp.j = 0; act = CellVector.fromVector2(vec2); Assert.AreEqual(exp.i, act.i, "i: vec2(0.44,0.44) -> cv(1,0)"); Assert.AreEqual(exp.j, act.j, "j: vec2(0.44,0.44) -> cv(1,0)"); // vec2(0,10.49) -> cv(0,10) vec2.x = 0.0f; vec2.y = 10.49f; exp.i = 0; exp.j = 10; act = CellVector.fromVector2(vec2); Assert.AreEqual(exp.i, act.i, "i: vec2(0,10.49) -> cv(0,10)"); Assert.AreEqual(exp.j, act.j, "j: vec2(0,10.49) -> cv(0,10)"); // vec2(0,-10.49) -> cv(0,-10) vec2.x = 0.0f; vec2.y = -10.49f; exp.i = 0; exp.j = -10; act = CellVector.fromVector2(vec2); Assert.AreEqual(exp.i, act.i, "i: vec2(0,-10.49) -> cv(0,-10)"); Assert.AreEqual(exp.j, act.j, "j: vec2(0,-10.49) -> cv(0,-10)"); }
public void rotation() { CellVector cv = new CellVector(0, 0); CellVector exp = new CellVector(0, 0); CellVector act = new CellVector(0, 0); // cv(0,10).rot(1) = cv(10,0) cv.i = 0; cv.j = 10; exp.i = 10; exp.j = 0; act = cv.rotated(CellIndex.topRight); Assert.AreEqual(exp.i, act.i, "i: cv(0,10).rot(1) = cv(10,0)"); Assert.AreEqual(exp.j, act.j, "j: cv(0,10).rot(1) = cv(10,0)"); // cv(1,1).rot(2) = cv(2,-1) cv.i = 1; cv.j = 1; exp.i = 1; exp.j = -2; act = cv.rotated(CellIndex.bottomRight); Assert.AreEqual(exp.i, act.i, "i: cv(1,1).rot(2) = cv(2,-1)"); Assert.AreEqual(exp.j, act.j, "j: cv(1,1).rot(2) = cv(2,-1)"); // cv(1,1).rot(3) = cv(-1,-1) cv.i = 1; cv.j = 1; exp.i = -1; exp.j = -1; act = cv.rotated(CellIndex.bottomMiddle); Assert.AreEqual(exp.i, act.i, "i: cv(1,1).rot(3) = cv(-1,-1)"); Assert.AreEqual(exp.j, act.j, "j: cv(1,1).rot(3) = cv(-1,-1)"); // cv(1,1).rot(4) = cv(-2,1) cv.i = 1; cv.j = 1; exp.i = -2; exp.j = 1; act = cv.rotated(CellIndex.bottomLeft); Assert.AreEqual(exp.i, act.i, "i: cv(1,1).rot(4) = cv(-2,1)"); Assert.AreEqual(exp.j, act.j, "j: cv(1,1).rot(4) = cv(-2,1)"); // cv(1,1).rot(5) = cv(2,-1) cv.i = 1; cv.j = 1; exp.i = -1; exp.j = 2; act = cv.rotated(CellIndex.topLeft); Assert.AreEqual(exp.i, act.i, "i: cv(1,1).rot(5) = cv(2,-1)"); Assert.AreEqual(exp.j, act.j, "j: cv(1,1).rot(5) = cv(2,-1)"); // cv(11,5).rot(1).rot(3).rot(2) = cv(11,5) cv.i = 11; cv.j = 5; exp.i = 11; exp.j = 5; act = cv.rotated(CellIndex.topRight).rotated(CellIndex.bottomMiddle).rotated(CellIndex.bottomRight); Assert.AreEqual(exp.i, act.i, "i: cv(11,5).rot(1).rot(3).rot(2) = cv(11,5)"); Assert.AreEqual(exp.j, act.j, "j: cv(11,5).rot(1).rot(3).rot(2) = cv(11,5)"); // cv(55,3).rot(5).rot(1).rot(-3).rot(-2).rot(-4).rot(3) = cv(5,3) cv.i = 55; cv.j = 3; exp.i = 55; exp.j = 3; act = cv.rotated(new CellIndex( 5)).rotated(new CellIndex( 1)).rotated(new CellIndex(-3)) .rotated(new CellIndex(-2)).rotated(new CellIndex(-4)).rotated(new CellIndex( 3)); Assert.AreEqual(exp.i, act.i, "i: cv(55,3).rot(5).rot(1).rot(-3).rot(-2).rot(-4).rot(3) = cv(5,3)"); Assert.AreEqual(exp.j, act.j, "j: cv(55,3).rot(5).rot(1).rot(-3).rot(-2).rot(-4).rot(3) = cv(5,3)"); }
// Quantise Vector2 public static CellVector fromVector2(Vector2 vec2) { CellVector cv = new CellVector(0, 0); // Affine transform for finding i float x = (vec2.x * 2.0f * sin60) + (vec2.y * ((2.0f * sin60 * sin60) - 0.5f)); float y = vec2.y - (2.0f * vec2.x * sin60); cv.i = (int)Math.Floor(((int)Math.Floor(x) - (int)Math.Floor(y) + 1) / 3.0f); // Affine transform for finding j x = (2.0f * vec2.x * sin60) - vec2.y; y = vec2.y * 2.0f; cv.j = (int)Math.Floor(((int)Math.Floor(y) - (int)Math.Floor(x) + 1) / 3.0f); return cv; }
public CellVector rotated(CellIndex ang) { CellVector result = new CellVector(0, 0); if (ang == CellIndex.topMiddle) { result.i = i_; result.j = j_; } else if (ang == CellIndex.topRight) { result.i = i_ + j_; result.j = -i_; } else if (ang == CellIndex.bottomRight) { result.i = j_; result.j = -i_ - j_; } else if (ang == CellIndex.bottomMiddle) { result.i = -i_; result.j = -j_; } else if (ang == CellIndex.bottomLeft) { result.i = -i_ - j_; result.j = i_; } else if (ang == CellIndex.topLeft) { result.i = -j_; result.j = i_ + j_; } else { Debug.LogError("CellVector can't rotate by invalid angle: "+ang); } return result; }
public PatternCoordinate(CellVector cellVector) { col = 0; row = 0; this += cellVector; }