private static void abstractSquare(int sidelength) { ASquare square = new ASquare(sidelength); Console.WriteLine("De omtrek is: {0}", square.GetPerimeter()); Console.WriteLine("De oppervlakte is {0}", square.GetArea()); }
public static ActorTree New() { var tree = new ActorTree(); var background = new ASquare(Vector2D.Zero, new Vector2D(256, 256), new Color3(0x000004)); var text = new AText(Vector2D.Zero, "12:00", Color3.White); tree.AddActor(background); tree.AddActor(text); return(tree); }
public ASquare GetSupportAPoint(int goalI, int goalJ) { ASquare resSquare = null; if (goalI > 0 && _table[goalI - 1, goalJ].Weight < BigWeight) { resSquare = _table[goalI - 1, goalJ]; } else if (goalJ > 0 && _table[goalI, goalJ - 1].Weight < BigWeight) { resSquare = _table[goalI, goalJ - 1]; } else if (goalI > 0 && goalJ > 0 && _table[goalI - 1, goalJ - 1].Weight < BigWeight) { resSquare = _table[goalI - 1, goalJ - 1]; } return(resSquare); }
/// <summary> /// Finds a path between startLoc and target locations, using A*. Path is returned in the path list /// </summary> /// <param name="startLoc">Start location.</param> /// <param name="target">Target Location</param> public List <Vector2> findPath(Vector2 startLoc, Vector2 target) { List <ASquare> open = new List <ASquare>(); List <Vector2> closed = new List <Vector2>(); List <Vector2> path = new List <Vector2>(); ASquare current = new ASquare((startLoc - target).magnitude, 0, startLoc); int maxSteps = 0; while (!current.loc.Equals(target) && maxSteps < 100) { maxSteps++; Vector2[] moves = getAdjacentMoves(current.loc); for (int i = 0; i < 4; i++) { Vector2 move = moves[i]; if (!isClosed(move, closed)) { ASquare newMove = new ASquare((move - target).magnitude, current.pathLength + 1, move); newMove.setPrevious(current); open.Add(newMove); } } open.Sort(); if (open.Count == 0) // return empty list if there is no possible path { return(new List <Vector2> ()); } current = open[0]; open.RemoveAt(0); closed.Add(current.loc); } while (current.getPrevious() != null) { path.Add(current.loc); current = current.getPrevious(); } return(path); }
public void UpdateDynamicAStar( int groupIndex, IList <Vehicle> vehicles, IDictionary <int, VehicleType> reversedGroupIndexes, MyStrategy ms) { var centerX = vehicles.Average(v => v.X); var centerY = vehicles.Average(v => v.Y); var myLeftX = centerX - SquareSize / 2; var leftN = (int)(myLeftX / SquareSize); var myTopY = centerY - SquareSize / 2; var topM = (int)(myTopY / SquareSize); for (var i = 0; i < _n; ++i) { for (var j = 0; j < _m; ++j) { var square = _table[i, j]; square.X = _startX + i * SquareSize; square.Y = _startY + j * SquareSize; square.Weight = 1d; } } _startSquare = _table[leftN, topM]; foreach (var index in reversedGroupIndexes.Keys) { if (index == groupIndex) { continue; } if (!MyStrategy.IsSameTypes(reversedGroupIndexes[groupIndex], reversedGroupIndexes[index])) { continue; // они друг другу не мешают } var currVeichles = ms.GetVehicles(index, MyStrategy.Ownership.ALLY); if (!currVeichles.Any()) { continue; // все сдохли } var currCenterX = currVeichles.Average(v => v.X); var currCenterY = currVeichles.Average(v => v.Y); var currLeftX = currCenterX - SquareSize / 2; var currLeftN = (int)(currLeftX / SquareSize); var currTopY = currCenterY - SquareSize / 2; var currTopM = (int)(currTopY / SquareSize); _table[currLeftN, currTopM].Weight = BigWeight; } //if (leftN > 0 && _table[leftN - 1, topM].Weight == BigWeight) //{ // if (topM > 0) _table[leftN - 1, topM - 1].Weight = BigWeight; // if (topM < _m - 1) _table[leftN - 1, topM + 1].Weight = BigWeight; //} //if (topM > 0 && _table[leftN, topM - 1].Weight == BigWeight) //{ // if (leftN > 0) _table[leftN - 1, topM - 1].Weight = BigWeight; // if (leftN < _n - 1) _table[leftN + 1, topM - 1].Weight = BigWeight; //} //if (leftN < _n - 1 && _table[leftN + 1, topM].Weight == BigWeight) //{ // if (topM > 0) _table[leftN + 1, topM - 1].Weight = BigWeight; // if (topM < _m - 1) _table[leftN + 1, topM + 1].Weight = BigWeight; //} //if (topM < _m - 1 && _table[leftN, topM + 1].Weight == BigWeight) //{ // if (leftN > 0) _table[leftN - 1, topM + 1].Weight = BigWeight; // if (leftN < _n - 1) _table[leftN + 1, topM + 1].Weight = BigWeight; //} }
public AStar(World world) { _squares = new List <ASquare>(); _n = (int)(world.Width / SquareSize); _m = (int)(world.Height / SquareSize); _table = new ASquare[_n, _m]; for (var i = 0; i < _n; ++i) { for (var j = 0; j < _m; ++j) { var square = new ASquare( SquareSize, _startX + i * SquareSize, _startY + j * SquareSize, 1d, i + ":" + j); _squares.Add(square); _table[i, j] = square; } } for (var i = 0; i < _n; ++i) { for (var j = 0; j < _m; ++j) { var neighbors = new List <ASquare>(); if (i != 0) { neighbors.Add(_table[i - 1, j]); } if (i != _n - 1) { neighbors.Add(_table[i + 1, j]); } if (j != 0) { neighbors.Add(_table[i, j - 1]); } if (j != _m - 1) { neighbors.Add(_table[i, j + 1]); } //if (i != 0 && j != 0) //{ // neighbors.Add(_table[i - 1, j - 1]); //} //if (i != _n - 1 && j != _m - 1) //{ // neighbors.Add(_table[i + 1, j + 1]); //} //if (i != 0 && j != _m - 1) //{ // neighbors.Add(_table[i - 1, j + 1]); //} //if (i != _n - 1 && j != 0) //{ // neighbors.Add(_table[i + 1, j - 1]); //} var square = _table[i, j]; square.Neighbors = neighbors; } } }