public static BasicMap Rotate(this BasicMap m, Coord origin, int radius, int degrees) { BasicMap rotated = new BasicMap(m.Width, m.Height, 1, Distance.EUCLIDEAN); //only rotating the bottom right corner, and not even rotating it correctly if (degrees % 90 == 0) { //this rotates more than just the circle - bug, deal with it later rotated.Add(m.RotateDiscreet(degrees)); } else { BasicMap map = m.RotateDiscreet(360); while (degrees > 45) { degrees -= 90; //rotates more than just this circle - bug - deal with it later map = m.RotateDiscreet(90); } while (degrees <= -45) { degrees += 90; //rotates more than just the circle - bug, fix later map = m.RotateDiscreet(270); } double radians = Calculate.DegreesToRadians(degrees); for (int x = -radius; x < radius; x++) { for (int y = -radius; y < radius; y++) { if (radius > Math.Sqrt(x * x + y * y)) { int xPrime = (int)(x * Math.Cos(radians) - y * Math.Sin(radians)); int yPrime = (int)(x * Math.Sin(radians) + y * Math.Cos(radians)); Coord source = new Coord(x, y) + origin; Coord target = new Coord(xPrime, yPrime) + origin; BasicTerrain terrain = map.GetTerrain <BasicTerrain>(source); if (terrain != null && rotated.Contains(target)) { rotated.SetTerrain(_factory.Copy(terrain, target)); } } } } } return(rotated); }
public void RotateDiscreetTest() { BasicMap rotated = _map.RotateDiscreet(90); //Top left corner becomes top right corner Coord a = new Coord(0, 0); Coord b = new Coord(_map.Width - 1, 0); BasicTerrain t1 = rotated.GetTerrain <BasicTerrain>(a); BasicTerrain t2 = _map.GetTerrain <BasicTerrain>(b); Assert.AreEqual(t1.Glyph, t2.Glyph, "Top left corner wasn't moved to the top right corner."); //top right corner becomes bottom right corner a = b; b = new Coord(_map.Width - 1, _map.Height - 1); t1 = rotated.GetTerrain <BasicTerrain>(a); t2 = _map.GetTerrain <BasicTerrain>(b); Assert.AreEqual(t1.Glyph, t2.Glyph, "Top right quadrant didn't move to the bottom right quadrant."); //bottom right corner become bottom left corner a = b; b = new Coord(0, _map.Height - 1); t1 = rotated.GetTerrain <BasicTerrain>(a); t2 = _map.GetTerrain <BasicTerrain>(b); Assert.AreEqual(t1.Glyph, t2.Glyph, "Bottom right Quadrant didn't move to the bottom left."); //bottom left corner becomes top left corner a = b; b = new Coord(0, 0); t1 = rotated.GetTerrain <BasicTerrain>(a); t2 = _map.GetTerrain <BasicTerrain>(b); Assert.AreEqual(t1.Glyph, t2.Glyph, "Bottom left quadrant didn't move to the top left."); }