generative-art-0.1.0.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Geometry.Coordinates.Hexagonal

Description

Hexagonal coordinate systems.

Nice article about the topic: https://www.redblobgames.com/grids/hexagons/

(image code)

Expand
>>> :{
haddockRender "Geometry/Coordinates/Hexagonal/cubes.svg" 360 360 $ \_ -> do
    let cellSize = 10
        hexagons = runST $ do
            gen <- MWC.create
            points <- gaussianDistributedPoints gen canvas (40*.mempty) (2^14)
            let hexs = fmap (fromVec2 cellSize) points
            pure $ foldl' (\weight hex -> M.insertWith (+) hex (1::Int) weight) mempty hexs
        canvas = shrinkBoundingBox (cellSize*2) [zero, Vec2 360 360]
        MinMax minW maxW = foldMap (\x -> MinMax x x) hexagons
    for_ (sortOn (\(_,weight) -> weight) (M.toList hexagons)) $ \(hex, weight) -> do
        let value = lerp ((sqrt (fromIntegral minW)), (sqrt (fromIntegral maxW))) (0,1) (sqrt (fromIntegral weight))
            growth = lerp (0,1) (3,6) value
        sketch (growPolygon growth (hexagonPoly cellSize hex))
        let color = twilight value
        setColor color
        C.fillPreserve
        let hexCenter = toVec2 cellSize hex
        sketch [Line hexCenter (hexCenter +. polar (deg (d+30)) (cellSize + 2/sqrt 3*growth)) | d <- [0,120,240]]
        setColor black
        C.stroke
:}
Generated file: size 194KB, crc32: 0xcde33b0b
Synopsis

Documentation

data Hex Source #

Hexagonal coordinate.

Constructors

Hex !Int !Int

The choice of values is called »cubal«. Use s (= -q-r) to get the omitted coordinate’s value.

Instances

Instances details
Monoid Hex Source #

hexZero

Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

mempty :: Hex #

mappend :: Hex -> Hex -> Hex #

mconcat :: [Hex] -> Hex #

Semigroup Hex Source #

hexAdd

Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

(<>) :: Hex -> Hex -> Hex #

sconcat :: NonEmpty Hex -> Hex #

stimes :: Integral b => b -> Hex -> Hex #

Show Hex Source # 
Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

showsPrec :: Int -> Hex -> ShowS #

show :: Hex -> String #

showList :: [Hex] -> ShowS #

NFData Hex Source # 
Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

rnf :: Hex -> () #

ChaosSource Hex Source # 
Instance details

Defined in Geometry.Chaotic

Methods

perturb :: Hex -> Int Source #

MwcChaosSource Hex Source # 
Instance details

Defined in Geometry.Chaotic

Methods

mwcChaos :: Hex -> Word32 Source #

Eq Hex Source # 
Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

(==) :: Hex -> Hex -> Bool #

(/=) :: Hex -> Hex -> Bool #

Ord Hex Source # 
Instance details

Defined in Geometry.Coordinates.Hexagonal

Methods

compare :: Hex -> Hex -> Ordering #

(<) :: Hex -> Hex -> Bool #

(<=) :: Hex -> Hex -> Bool #

(>) :: Hex -> Hex -> Bool #

(>=) :: Hex -> Hex -> Bool #

max :: Hex -> Hex -> Hex #

min :: Hex -> Hex -> Hex #

toVec2 Source #

Arguments

:: Double

Size of a hex cell (radius, side length)

-> Hex 
-> Vec2 

Convert a hexagonal coordinate’s center to an Euclidean Vec2.

fromVec2 Source #

Arguments

:: Double

Size of a hex cell (radius, side length)

-> Vec2 
-> Hex 

Convert a Euclidean Vec2 to the coordiante of the hexagon it is in.

Painting aid

hexagonalCoordinateSystem Source #

Arguments

:: Double

Side length of a hexagon (equivalent to its radius)

-> Int

How many hexagons to draw in each direction

-> Render () 

Draw a hexagonal coordinate system as a helper grid, similar to cartesianCoordinateSystem.

Movement

move :: Direction -> Int -> Hex -> Hex Source #

Move x steps in a direction

Arithmetic

hexAdd :: Hex -> Hex -> Hex Source #

Add two Hex coordinates.

hexSubtract :: Hex -> Hex -> Hex Source #

Subtract two Hex coordinates.

hexTimes :: Int -> Hex -> Hex Source #

Multiply a Hex coordinate with a whole number.

hexZero :: Hex Source #

The origin of the hexagonal coordinate system.

Measurement and transformation

distance :: Hex -> Hex -> Int Source #

How many steps are between two coordinates?

rotateAround Source #

Arguments

:: Hex

Center

-> Int

number of 60° rotations. Positive for clockwise (in Cairo coordinates).

-> Hex

Point to rotate

-> Hex 

Rotate around a center by a number of 60° angles.

cubeRound :: Double -> Double -> Hex Source #

Given fractional cubical coordinates, yield the hexagon the coordinate is in.

Geometry

line :: Hex -> Hex -> [Hex] Source #

Line between two Hex.

(image code)

Expand
>>> :{
haddockRender "Geometry/Coordinates/Hexagonal/line.svg" 300 200 $ \_ -> do
    let cellSize = 20
        canvas = shrinkBoundingBox 10 [zero, Vec2 300 200]
        hexes = line hexZero (move R 5 (move UR 3 hexZero))
        polygons = map (shrinkPolygon 1 . hexagonPoly cellSize) hexes
        fitToCanvas = transform (transformBoundingBox polygons canvas def)
    for_ polygons $ \polygon -> cairoScope $ do
        sketch (fitToCanvas polygon)
        setColor (mma 0 `withOpacity` 0.3)
        C.fillPreserve
        setColor (mma 0 `withOpacity` 0.5)
        C.stroke
:}
Generated file: size 6KB, crc32: 0x5ec289a8

ring Source #

Arguments

:: Int

Radius

-> Hex

Center

-> [Hex] 

All Hex reachable only with one exact number of steps. floodFilling it will yield hexagonsInRange.

(image code)

Expand
>>> :{
haddockRender "Geometry/Coordinates/Hexagonal/ring.svg" 200 200 $ \_ -> do
    let cellSize = 20
        canvas = shrinkBoundingBox 10 [zero, Vec2 200 200]
        hexes = ring 2 hexZero
        polygons = map (shrinkPolygon 1 . hexagonPoly cellSize) hexes
        fitToCanvas = transform (transformBoundingBox polygons canvas def)
    for_ polygons $ \polygon -> cairoScope $ do
        sketch (fitToCanvas polygon)
        setColor (mma 1 `withOpacity` 0.3)
        C.fillPreserve
        setColor (mma 1 `withOpacity` 0.5)
        C.stroke
:}
Generated file: size 7KB, crc32: 0xacce7f95

hexagonsInRange :: Int -> Hex -> [Hex] Source #

Hexagons reachable within a number of steps from the origin. The boundary of this will be the ring.

isOnEdge :: Hex -> HexPolygon -> Bool Source #

Given a hexagonal polygon, is the Hex on its edge?

pointInPolygon :: Hex -> HexPolygon -> Bool Source #

Is the Hex inside the polygon (including its edge)?

edgePoints :: HexPolygon -> Set Hex Source #

All points on a polygon’s edge.

floodFill Source #

Arguments

:: Hex

Starting point

-> Set Hex 
-> Set Hex 

Fill all neighbours of a point, and their neighbours, and…

Drawing and interfacing

polygonSketch Source #

Arguments

:: Double

Cell size

-> HexPolygon 
-> Render () 

Sketch a hexagonal polygon.

hexagonPoly :: Double -> Hex -> Polygon Source #

Polygon to match a HexagonalCoordinate. Useful e.g. for collision checking, and of course also for painting. :-)