Hexmath 2.0.0
dotnet add package Hexmath --version 2.0.0
NuGet\Install-Package Hexmath -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hexmath" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexmath" Version="2.0.0" />
<PackageReference Include="Hexmath" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hexmath --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hexmath, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hexmath@2.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hexmath&version=2.0.0
#tool nuget:?package=Hexmath&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HexMath
A C# library for hexagonal grid mathematics with type-safe coordinates.
Installation
dotnet add package Hexmath
Features
- Type-safe
HexCoordstruct (axial storage, computed S coordinate) HexDirectionenum for 6 cardinal directions- Coordinate conversions between hex and pixel systems
- Distance calculations, rings, and spirals
- 60-degree rotation operations
- Support for flat-top and pointy-top orientations
- Configurable stretch factors for non-uniform grids
Usage
using Hexmath;
// Create hex coordinates
var origin = HexCoord.Zero;
var hex = new HexCoord(2, -1); // Q=2, R=-1, S=-1 (computed)
// Directions
var neighbor = HMath.Neighbor(origin, HexDirection.East);
var opposite = HexDirection.East.Opposite(); // West
var next = HexDirection.East.Next(); // SouthEast
// Distance between hexes
int dist = HMath.Distance(origin, hex);
// Get all neighbors
var neighbors = HMath.GetAllNeighbors(origin);
// Pixel conversions
var metadata = new HexMetaData(Size: 32.0f, IsPointyTop: true);
Vector2 pixel = HMath.HexToPixel(hex, metadata);
HexCoord back = HMath.PixelToHex(pixel, metadata);
// Rings and spirals
var ring = HMath.GetRing(origin, radius: 2); // 12 hexes
var spiral = HMath.GetSpiral(origin, maxRadius: 3); // 37 hexes
// Rotation (2 steps = 120 degrees)
HexCoord rotated = HMath.RotateBySteps(hex, pivot: origin, steps: 2);
// Game engine interop
Vector3 vec = hex.ToVector3(); // (Q, R, S)
HexCoord coord = HexCoord.FromVector3(vec);
Types
HexCoord
public readonly struct HexCoord
{
public int Q { get; } // Column
public int R { get; } // Row
public int S => -Q - R; // Computed (Q + R + S = 0)
public static readonly HexCoord Zero;
// Operators: +, -, *, ==, !=
// Methods: ToVector3(), FromVector3(), Equals(), GetHashCode()
}
HexDirection
public enum HexDirection
{
East = 0, // (1, 0, -1)
SouthEast = 1, // (1, -1, 0)
SouthWest = 2, // (0, -1, 1)
West = 3, // (-1, 0, 1)
NorthWest = 4, // (-1, 1, 0)
NorthEast = 5 // (0, 1, -1)
}
// Extension methods
direction.ToOffset() // HexCoord offset
direction.Next(clockwise: true) // Next direction
direction.Opposite() // Opposite direction
Requirements
- .NET 8.0+
Breaking Changes in 2.0
Vector3replaced withHexCoordstructDirectionsarray replaced withHexDirectionenum- Removed:
IsValidHexCoordinate,IsValidHexDirection,ToAxial,ToCube
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.