Skip to content

DigitalSmile/hexagon

Repository files navigation

Hexagon Java library

Java+ GitHub Workflow Status Quality Gate Status codecov Lines of Code GitHub

Small and pure Java library to deal with hexagons math and operations with zero dependencies. The library provides an abstract level of hexagon manipulation and is render engine agnostic, so it can be used with any type of visual libraries (AWT, JavaFX, libGDX, etc.)

Documentation can be found in wiki.

The code and the library itself are highly inspired by blog posts by @redblobgames (https://www.redblobgames.com/grids/hexagons/).

Features

  • Pure Java 17+ library
  • Zero Dependencies
  • Supports regular hexagons with flat or pointy orientation
  • Supports custom object to be tied to hexagon
  • Supports grid creation via simple builder
  • Supports grid with rectangle and hexagonal shape

Usage

Add dependency to your project. Latest version can be found at the jitpack badge.

Gradle:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.DigitalSmile:hexagon:{version}'
}

Maven:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
...
<dependency>
    <groupId>com.github.DigitalSmile</groupId>
    <artifactId>hexagon</artifactId>
    <version>{version}</version>
</dependency>

Use a HexagonGridBuilder to create a grid or use Hexagon and Operations classes directly:

var hexagonGrid = new HexagonGrid.HexagonGridBuilder<>()
                        .shape(new HexagonalShape(5), Orientation.FLAT) // hexagonal shape with radius of 5 hexagons and flat orientation
                        .hexagonWidth(150)                              // width of hexagon in physical units
                        .build();
hexagonGrid.generateHexagons();
var hexagonList = hexagonGrid.getHexagons(); // returns all generated hexagons
var lineHexagonList = HexagonOperations.hexagonLinePath(new Hexagon(0,0,0), new Hexagon(0, -3, 3)); // returns hexagons, that are in line between two hexagons

Next milestone - Mercury GitHub milestone