static void Main(string[] args) { Coordinates c = new Coordinates(123, 456); Coordinates d = new Coordinates(321, 654); Dictionary<Coordinates, int> indices = new Dictionary<Coordinates, int>(); indices.Add(new Coordinates(123, 456), 1); indices.Add(new Coordinates(321, 654), 2); Console.WriteLine(indices[c]); Console.WriteLine(indices[d]); Console.ReadLine(); }
/// <summary> /// Initialise a Map from XML /// </summary> /// <param name="reader">The XmlReader to load from.</param> public Map(XmlReader reader) { string widthString = reader.GetAttribute("width"); string heightString = reader.GetAttribute("height"); int.TryParse(widthString, out width); int.TryParse(heightString, out height); int depth = 1; while (depth > 0 && reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name) { case ("start"): start = new Coordinates(reader); break; case ("end"): end = new Coordinates(reader); break; case ("town"): towns.Add(new Coordinates(reader)); break; } if (!reader.IsEmptyElement) { depth++; } break; case XmlNodeType.EndElement: depth--; break; } } }
/// <summary> /// Randomly choose an adjacent point. /// </summary> /// <param name="coordinates">The starting point</param> /// <returns>A point adjacent to the starting point</returns> private static Coordinates RandomAdjacentPoint(Coordinates coordinates) { int x = coordinates.X; int y = coordinates.Y; switch (random.Next(8)) { case 0: x++; break; case 1: x++; y++; break; case 2: y++; break; case 3: x--; y++; break; case 4: x--; break; case 5: x--; y--; break; case 6: y--; break; case 7: x++; y--; break; } return new Coordinates(x, y); }
/// <summary> /// Add a vertex to the network. /// /// When this method is used, the network remembers the coordinates of the vertex and when anther /// vertex is added (through AddVerexUnique() method or CopyVerticesUnique()) with the same coordinates /// the duplicate is not added, instead the index of the original is returned. /// </summary> /// <param name="coordinates">The coordinates of the new Vertex</param> /// <returns>The index of the Vertex added.</returns> public int AddVertexUnique(Coordinates coordinates) { if (vertexIndices.ContainsKey(coordinates)) { return vertexIndices[coordinates]; } else { int index = vertices.Count; AddVertex(coordinates); vertexIndices.Add(coordinates, index); return index; } }
/// <summary> /// Add a vertex to the network /// </summary> /// <param name="coordinates"> /// A <see cref="Coordinates"/> object denoting the position /// </param> /// <returns> /// The added <see cref="Vertex"/> /// </returns> public Vertex AddVertex(Coordinates coordinates) { Vertex vertex = new Vertex(this, coordinates); vertices.Add(vertex); return vertex; }
/// <summary> /// Determine if another set of coordinates is equivalent to this one. /// </summary> /// <param name="c">The other set of coordinates</param> /// <returns>True iff the other set of coordinates are equivalent</returns> public bool Equals(Coordinates c) { return x == c.x && y == c.y; }
/// <summary> /// Calcuate the square of the distance between this locations and another. /// /// This is faster than calculating the distance as no square root operation is required. /// </summary> /// <param name="c">The coordinates of the other location</param> /// <returns>The square of the distance between c0 and c1</returns> public int GetDistanceSquared(Coordinates c) { int dx = x - c.x; int dy = y - c.y; return dx * dx + dy * dy; }
/// <summary> /// Calculate the distance between this locations and another. /// </summary> /// <param name="c">The coordinates of the other location</param> /// <returns>The distance between c0 and c1</returns> public double GetDistance(Coordinates c) { return Math.Sqrt((double)GetDistanceSquared(c)); }
/// <summary> /// Initialise a new Vertex /// </summary> /// <param name="network">The RoadNetwork this Vertex belongs to.</param> /// <param name="coordinates">The location of the Vertex.</param> public Vertex(RoadNetwork network, Coordinates coordinates) { this.network = network; this.coordinates = coordinates; this.edges = new List<Edge>(); }