/// <summary> /// Determines whether this hex is within a specified rectangle. /// </summary> /// <returns><c>true</c> if this instance is within the specified rectangle; otherwise, <c>false</c>.</returns> public bool IsWithinRectangle(HexCoord cornerA, HexCoord cornerB) { if (r > cornerA.r && r > cornerB.r || r < cornerA.r && r < cornerB.r) { return(false); } bool reverse = cornerA.O > cornerB.O; // Travel right to left. bool offset = cornerA.r % 2 != 0; // Starts on an odd row, bump alternate rows left. bool trim = Math.Abs(cornerA.r - cornerB.r) % 2 == 0; // Even height, trim alternate rows. bool odd = (r - cornerA.r) % 2 != 0; // This is an alternate row. int width = Math.Abs(cornerA.O - cornerB.O); bool hasWidth = width != 0; if (reverse && (odd && (trim || !offset) || !(trim || offset || odd)) || !reverse && (trim && odd || offset && !trim && hasWidth)) { width -= 1; } int x = (O - cornerA.O) * (reverse? -1: 1); if (reverse && odd && !offset || !reverse && offset && odd && hasWidth) { x -= 1; } return(x <= width && x >= 0); }
/// <summary> /// Get the corners of a QR-space rectangle containing every cell touching an XY-space rectangle. /// </summary> public static HexCoord[] CartesianRectangleBounds(Vector2 cornerA, Vector2 cornerB) { Vector2 min = new Vector2(Math.Min(cornerA.x, cornerB.x), Math.Min(cornerA.y, cornerB.y)); Vector2 max = new Vector2(Math.Max(cornerA.x, cornerB.x), Math.Max(cornerA.y, cornerB.y)); HexCoord[] results = { HexCoord.AtPosition(min), HexCoord.AtPosition(max) }; Vector2 pos = results[0].Position(); if ((pos + corners[0]).y <= min.y || (pos + corners[5]).y >= min.y) { results[0] += neighbors[4]; } else if ((pos + corners[1]).x <= min.x) { results[0] += neighbors[3]; } pos = results[1].Position(); if ((pos + corners[2]).y <= max.y || (pos + corners[3]).y >= max.y) { results[1] += neighbors[1]; } else if ((pos + corners[1]).x >= max.x) { results[1] += neighbors[0]; } return(results); }
public bool Is(HexCoord hex) { return this.q == hex.q && this.r == hex.r; }
/// <summary> /// Change the Q and R values of this <see cref="Settworks.Hexagons.HexCoordinate"/> to those /// of the specified <see cref="Settworks.Hexagons.HexCoord"/>. /// </summary> /// <param name="hex"><see cref="Settworks.Hexagons.HexCoord"/> to become.</param> public void Become(HexCoord hex) { q = hex.q; r = hex.r; }
/// <summary> /// Initializes a new instance of the <see cref="Settworks.Hexagons.HexCoordinate"/> class /// duplicating the Q and R values of a specified <see cref="Settworks.Hexagons.HexCoord"/>. /// </summary> /// <param name="hex"><see cref="Settworks.Hexagons.HexCoord"/> to duplicate.</param> public HexCoordinate(HexCoord hex) { Become(hex); }
/// <summary> /// Distance between two hexes. /// </summary> public static int Distance(HexCoord a, HexCoord b) { return((a - b).AxialLength()); }
public bool Is(HexCoord hex) { return(this.q == hex.q && this.r == hex.r); }
/// <summary> /// Distance between two hexes. /// </summary> public static int Distance(HexCoord a, HexCoord b) { return (a - b).AxialLength(); }
/// <summary> /// Determines whether this hex is within a specified rectangle. /// </summary> /// <returns><c>true</c> if this instance is within the specified rectangle; otherwise, <c>false</c>.</returns> public bool IsWithinRectangle(HexCoord cornerA, HexCoord cornerB) { if (r > cornerA.r && r > cornerB.r || r < cornerA.r && r < cornerB.r) return false; bool reverse = cornerA.O > cornerB.O; // Travel right to left. bool offset = cornerA.r % 2 != 0; // Starts on an odd row, bump alternate rows left. bool trim = Math.Abs(cornerA.r - cornerB.r) % 2 == 0; // Even height, trim alternate rows. bool odd = (r - cornerA.r) % 2 != 0; // This is an alternate row. int width = Math.Abs(cornerA.O - cornerB.O); bool hasWidth = width != 0; if (reverse && (odd && (trim || !offset) || !(trim || offset || odd)) || !reverse && (trim && odd || offset && !trim && hasWidth)) width -= 1; int x = (O - cornerA.O) * (reverse? -1: 1); if (reverse && odd && !offset || !reverse && offset && odd && hasWidth) x -= 1; return (x <= width && x >= 0); }
public static List<HexCoord> LineDraw(HexCoord a, HexCoord b) { var n = Distance(a, b); List<HexCoord> results = new List<HexCoord>(); for (int i = 0; i < n; i++) { var cubelerp = CubeLerp(HexToCube(a), HexToCube(b), 1.0f / n * i); results.Add(CubeToHex(CubeRound(cubelerp))); } return results; }
/// <summary> /// Cube coordinates. /// </summary> public static Vector3 HexToCube(HexCoord h) { return new Vector3(h.q, -h.q - h.r, h.r); }
public static HexCoord HexRound(HexCoord h) { return CubeToHex(CubeRound(HexToCube(h))); }