Git Product home page Git Product logo

hex's Introduction

hex

linux

A header-only C++23 hexagonal grid library.

Examples

Storing Data on a Hex Grid

#include <hex/hex.hpp>
#include <print>

int main()
{
    using namespace hex;
    using namespace hex::literals;
    
    // Typedef for a grid that can have any convex polygon shape.
    using my_grid = grid<int, convex_polygon_view<int>>;

    // Create a grid with the shape of a regular polygon with radius=1.
    my_grid grid(views::convex_polygon(make_regular_hexagon_parameters(1)));

    // Set the data of tile (0, 0, 0) to 42.
    grid[{0_q, 0_r}] = 42;

    // Print all 7 tiles.
    for (auto [v, i] : grid)
        std::println("{}: {}", v, i);
}

Output:

(-1, 0, 1): 0
(-1, 1, 0): 0
(0, -1, 1): 0
(0, 0, 0): 42
(0, 1, -1): 0
(1, -1, 0): 0
(1, 0, -1): 0

Views

#include <hex/hex.hpp>
#include <print>

int main()
{
    using namespace hex;
    using namespace hex::literals;

    // Print all tiles on a line from (1, 2, -3) to (-1, -1, 2), but rotated by 60° around the origin.
    for (auto&& v : views::line(vector{1_q, 2_r}, vector{-1_q, 2_s}) | views::transform(rot_60_cw))
        std::println("{}", v);
}

Output:

(-2, 3, -1)
(-1, 2, -1)
(-1, 1, 0)
(0, 0, 0)
(0, -1, 1)
(1, -2, 1)

Conversion to and from Cartesian Coordinates

#include <hex/hex.hpp>
#include <print>

int main()
{
    using namespace hex;
    
    auto const vec = from_cartesian(std::array{1, 2});
    std::println("Fractional: {}", vec);
    std::println("Nearest: {}", round(vec));

    auto const pos = to_cartesian(round(vec));
    std::println("Cartesian of Nearest: ({}, {})", pos[0], pos[1]);
}

Output:

Fractional: (0.6666666666666666, 0.8213672050459181, -1.4880338717125847)
Nearest: (1, 1, -2)
Cartesian of Nearest: (1.5, 2.598076211353316)

hex's People

Contributors

jan-moeller avatar

Watchers

 avatar

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.