Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Hexagonal coordinate systems.
Nice article about the topic: https://www.redblobgames.com/grids/hexagons/
(image code)
>>>
:{
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
- data Hex = Hex !Int !Int
- toVec2 :: Double -> Hex -> Vec2
- fromVec2 :: Double -> Vec2 -> Hex
- hexagonalCoordinateSystem :: Double -> Int -> Render ()
- data Direction
- move :: Direction -> Int -> Hex -> Hex
- hexAdd :: Hex -> Hex -> Hex
- hexSubtract :: Hex -> Hex -> Hex
- hexTimes :: Int -> Hex -> Hex
- hexZero :: Hex
- distance :: Hex -> Hex -> Int
- rotateAround :: Hex -> Int -> Hex -> Hex
- cubeRound :: Double -> Double -> Hex
- line :: Hex -> Hex -> [Hex]
- ring :: Int -> Hex -> [Hex]
- hexagonsInRange :: Int -> Hex -> [Hex]
- newtype HexPolygon = HexPolygon [Hex]
- isOnEdge :: Hex -> HexPolygon -> Bool
- pointInPolygon :: Hex -> HexPolygon -> Bool
- edgePoints :: HexPolygon -> Set Hex
- floodFill :: Hex -> Set Hex -> Set Hex
- polygonSketch :: Double -> HexPolygon -> Render ()
- hexagonPoly :: Double -> Hex -> Polygon
Documentation
Hexagonal coordinate.
Hex !Int !Int | The choice of values is called »cubal«.
Use |
Convert a hexagonal coordinate’s center to an Euclidean Vec2
.
Convert a Euclidean Vec2
to the coordiante of the hexagon it is in.
Painting aid
hexagonalCoordinateSystem Source #
:: 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
Hexagonal direction, used by move
.
Instances
Bounded Direction Source # | |
Enum Direction Source # | |
Defined in Geometry.Coordinates.Hexagonal succ :: Direction -> Direction # pred :: Direction -> Direction # fromEnum :: Direction -> Int # enumFrom :: Direction -> [Direction] # enumFromThen :: Direction -> Direction -> [Direction] # enumFromTo :: Direction -> Direction -> [Direction] # enumFromThenTo :: Direction -> Direction -> Direction -> [Direction] # | |
Show Direction Source # | |
Eq Direction Source # | |
Ord Direction Source # | |
Defined in Geometry.Coordinates.Hexagonal |
Arithmetic
Measurement and transformation
:: 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)
>>>
:{
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
All Hex
reachable only with one exact number of steps. floodFill
ing it
will yield hexagonsInRange
.
(image code)
>>>
:{
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
.
newtype HexPolygon Source #
Instances
Show HexPolygon Source # | |
Defined in Geometry.Coordinates.Hexagonal showsPrec :: Int -> HexPolygon -> ShowS # show :: HexPolygon -> String # showList :: [HexPolygon] -> ShowS # | |
Eq HexPolygon Source # | |
Defined in Geometry.Coordinates.Hexagonal (==) :: HexPolygon -> HexPolygon -> Bool # (/=) :: HexPolygon -> HexPolygon -> Bool # | |
Ord HexPolygon Source # | |
Defined in Geometry.Coordinates.Hexagonal compare :: HexPolygon -> HexPolygon -> Ordering # (<) :: HexPolygon -> HexPolygon -> Bool # (<=) :: HexPolygon -> HexPolygon -> Bool # (>) :: HexPolygon -> HexPolygon -> Bool # (>=) :: HexPolygon -> HexPolygon -> Bool # max :: HexPolygon -> HexPolygon -> HexPolygon # min :: HexPolygon -> HexPolygon -> HexPolygon # |
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.
Fill all neighbours of a point, and their neighbours, and…