Code Monkey home page Code Monkey logo

hexagonallib's Introduction

HexagonalLib implementation for .NET

I was highly inspired by this article: https://www.redblobgames.com/grids/hexagons/ and this repo is mostly just C# implementation of it. Recommended for reading. Here I will describe some technical details regarding implementation.

Hexagonal grid

To use hexagonal grids you need to create HexagonalGrid and initialize it with grid type and inscribed radius of hex.

var grid = new HexagonalGrid(HexagonalGridType.PointyEven, 1.0f);

Grids layouts and orientations are merged to one enum HexagonalGridType.

  • PointyOdd - Horizontal layout shoves odd rows right [odd-r]
  • PointyEven - Horizontal layout shoves even rows right [even-r]
  • FlatOdd - Vertical layout shoves odd columns down [odd-q]
  • FlatEven - Vertical layout shoves even columns down [even-q]

Coordinates systems

There is three coordinates systems represented in lib:

Note: there is no Doubled coordinates yet. I will add them in later versions.

Struct HexagonalGrid contains a bunch of methods for coordinates conversion (all details are described in original article):

var offsetFromCubic = grid.ToOffset(cubic);
var offsetFromAxial = grid.ToOffset(axial);
 
var cubicFromOffset = grid.ToCubic(offset);
var cubicFromAxial = grid.ToCubic(axial);
 
var axialFromOffset = grid.ToAxial(offset);
var axialFromCubic = grid.ToAxial(cubic);

Neighbors

There is the order of neighbors of hex:

You can take a neighbor of any hex by providing a coordinate of hex and required neighbor index.

var oNeighbor = grid.GetNeighbor(offset, neighborIndex);
var cNeighbor = grid.GetNeighbor(cubic, neighborIndex);
var aNeighbor = grid.GetNeighbor(axial, neighborIndex);

The neighbor index can be negative or greater than 5 (it will be normalized)

Or you can take all neighbors of particular hex

var oNeighbors = grid.GetNeighbors(offset);
var cNeighbors = grid.GetNeighbors(cubic);
var aNeighbors = grid.GetNeighbors(axial);

There is also option to check are two hexes neighbors or not:

var isNeighbors1 = grid.IsNeighbors(offset, oNeighbor);
var isNeighbors2 = grid.IsNeighbors(cubic, cNeighbor);
var isNeighbors3 = grid.IsNeighbors(axial, aNeighbor);

Ring of neighbors

You also can get ring of neighbor for particular hex

var oNeighbors = grid.GetNeighborsRing(offset, radius);
var cNeighbors = grid.GetNeighborsRing(cubic, radius);
var aNeighbors = grid.GetNeighborsRing(axial, radius);

Circle of neighbors

Same as ring but you will receive hexes from all rings

var oNeighbors = grid.GetNeighborsAround(offset, radius);
var cNeighbors = grid.GetNeighborsAround(cubic, radius);
var aNeighbors = grid.GetNeighborsAround(axial, radius);

2D space

There is also list of methods to convert hex coordinate to its center (point represented as tuple):

var pointFromOffset = grid.ToPoint2(offset);
var pointFromCubic = grid.ToPoint2(cubic);
var pointFromAxial = grid.ToPoint2(axial);

To convert point into a hex coordinate which contains this point you can use:

var offsetFromPoint = grid.ToOffset(x, y);
var cubicFromPoint = grid.ToCubic(x, y);
var axialFromPoint = grid.ToAxial(x, y);

Hexagons have 6 sides and 6 corners. There is corners order of the hexes:

To take them in code simly use:

var cornerFromOffset = grid.GetCorner(offset, cornerIndex);
var cornerFromCubic = grid.GetCorner(cubic, cornerIndex);
var cornerFromAxial = grid.GetCorner(axial, cornerIndex);

The corner index can be negative or greater than 5 (it will be normalized)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.