/// <summary>Calculates a (canonical X or Y) grid-coordinate for a point, from the supplied 'picking' matrix.</summary> /// <param name="this"></param> /// <param name="matrix">The 'picking-matrix' matrix</param> /// <param name="point">The screen point identifying the hex to be 'picked'.</param> /// <returns>A (canonical X or Y) grid coordinate of the 'picked' hex.</returns> public static int GetCoordinate(this IHexgrid @this, HexMatrix matrix, HexPoint point) { var points = new HexPoint[] { point }; matrix.TransformPoints(points); return((int)Math.Floor((points[0].X + points[0].Y + 2F) / 3F)); }
/// <summary><c>HexCoords</c> for the hex at the screen point, with the given AutoScroll position.</summary> /// <param name="this"></param> /// <param name="point">Screen point specifying hex to be identified.</param> /// <param name="autoScroll">AutoScrollPosition for game-display Panel.</param> static HexCoords GetHexCoordsInner(this IHexgrid @this, HexPoint point, HexSize autoScroll) { // Adjust for origin not as assumed by GetCoordinate(). var grid = new HexSize((int)(@this.GridSizeF().Width *2F / 3F), (int)@this.GridSizeF().Height); point -= autoScroll + grid - @this.Margin; return(HexCoords.NewCanonCoords(@this.GetCoordinate(@this.MatrixX(), point), @this.GetCoordinate(@this.MatrixY(), point))); }
/// <summary>Returns the pixel coordinates of the center of the specified hex.</summary> /// <param name="this"></param> /// <param name="coords"><see cref="HexCoords"/> specification for which pixel center is desired.</param> /// <returns>Pixel coordinates of the center of the specified hex.</returns> public static HexPoint HexOrigin(this IHexgrid @this, HexCoords coords) => new HexPoint( (int)(@this.GridSizeF().Width *coords.User.X), (int)(@this.GridSizeF().Height *coords.User.Y + @this.GridSizeF().Height / 2 * (coords.User.X + 1) % 2) );
/// <summary>Returns ScrollPosition that places given hex in the upper-Left of viewport.</summary> /// <param name="this"></param> /// <param name="coordsNewULHex"><c>HexCoords</c> for new upper-left hex</param> /// <returns>Pixel coordinates in Client reference frame.</returns> static HexPoint HexCenterPointInner(this IHexgrid @this, HexCoords coordsNewULHex) => @this.HexOrigin(coordsNewULHex) + new HexSize((int)(@this.GridSizeF().Width *2F / 3F), (int)@this.GridSizeF().Height);
/// <summary>Returns ScrollPosition that places given hex in the upper-Left of viewport.</summary> /// <param name="this"></param> /// <param name="coordsNewULHex"><c>HexCoords</c> for new upper-left hex</param> /// <returns>Pixel coordinates in Client reference frame.</returns> public static HexPoint HexCenterPoint(this IHexgrid @this, HexCoords coordsNewULHex) => @this.IsTransposed ? TransposePoint(@this.HexCenterPointInner(coordsNewULHex)) : @this.HexCenterPointInner(coordsNewULHex);
/// <summary><c>HexCoords</c> for the hex at the screen point, with the given AutoScroll position.</summary> /// <param name="this"></param> /// <param name="point">Screen point specifying hex to be identified.</param> /// <param name="autoScroll">AutoScrollPosition for game-display Panel.</param> public static HexCoords GetHexCoords(this IHexgrid @this, HexPoint point, HexSize autoScroll) => @this.IsTransposed ? @this.GetHexCoordsInner(TransposePoint(point), TransposeSize(autoScroll)) : @this.GetHexCoordsInner(point, autoScroll);
/// <summary>.</summary> /// <param name="this"></param> public static HexSizeF GridSizeF(this IHexgrid @this) => @this.GridSize.Scale(@this.Scale);
/// <summary>Scroll position on the (possibly transposed) HexGrid.</summary> /// <param name="this"></param> /// <param name="scrollPosition"></param> public static HexPoint GetScrollPosition(this IHexgrid @this, HexPoint scrollPosition) => @this.IsTransposed ? TransposePoint(scrollPosition) : scrollPosition;
/// <summary><see cref="HexMatrix"/> for 'picking' the <B>Y</B> hex coordinate</summary> public static HexMatrix MatrixY(this IHexgrid @this) => new HexMatrix( 0.0F, (3.0F / 2.0F) / @this.GridSizeF().Width, 2.0F / @this.GridSizeF().Height, 1.0F / @this.GridSizeF().Height, -0.5F, -0.5F);
/// <summary>TODO</summary> /// <param name="this"></param> /// <param name="mapSizePixels"></param> /// <param name="mapScale"></param> public static HexSize GetSize(this IHexgrid @this, HexSize mapSizePixels, float mapScale) => HexSize.Ceiling(mapSizePixels.Scale(mapScale));
/// <summary>Returns the scroll position to center a specified hex in viewport.</summary> /// <param name="this"></param> /// <param name="coordsNewCenterHex"><c>HexCoords</c> for the hex to be centered in viewport.</param> /// <param name="visibleRectangle"></param> /// <returns>Pixel coordinates in Client reference frame.</returns> public static HexPoint ScrollPositionToCenterOnHex(this IHexgrid @this, HexCoords coordsNewCenterHex, CoordsRectangle visibleRectangle) => @this.HexCenterPoint(HexCoords.NewUserCoords(coordsNewCenterHex.User - (visibleRectangle.Size.User / 2)));