Hex class
An immutable abstraction of a hexagon in a hexagonal grid.
Offers several useful methods to measure distance, find path etc. Position of the hexagon is defined by a Cube coordinates.
Constructors
- Hex(int q, int r, int s)
- Creates a new hexagon with the given Cube coordinates.
- Hex.fromAxial(int q, int r)
- Creates hexagon from a given axial coordinates, see Cube.fromAxial.
- Hex.fromCube(Cube cube)
- Creates hexagon from a given Cube.
- Hex.fromId(String id)
-
Creates hexagon from a given
id. - Hex.fromOffset(GridOffset offset, {GridLayout gridLayout = GridLayout.POINTY_TOP})
- Creates hexagon from a given GridOffset coordinates, using the given GridLayout.
- Hex.fromPixelPoint(PixelPoint point, double hexSize, {GridLayout gridLayout = GridLayout.POINTY_TOP})
-
Creates hexagon from a given PixelPoint coordinates, using the given GridLayout and size of one hex in grid.
factory
- Hex.zero()
- Creates hexagon in 0,0,0
Properties
- cube → Cube
-
Coordinate of the hexagon in a hexagonal grid.
final
- hashCode → int
-
The hash code for this object.
no setteroverride
- id → String
-
Returns a unique id of the hexagon. Id is a string representation of the underlaying Cube
coordinates of this hexagon. It can be used to restore the Hex using Hex.fromId constructor.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
centerPoint(
double size, {GridLayout gridLayout = GridLayout.POINTY_TOP}) → PixelPoint - Center of this hex in a pixel grid.
-
cheapestPathTo(
Hex to, {MoveCost? costFunction, int? maximumDistanceFromTo}) → HexPath? -
Computes the shortest path to other hex. The path is a list of connected hexes, including 'this' and
to. If there is no path, method returns null. The path is computed using the givencostFunction, which is a function that returns the cost of transition from one hex to another. See MoveCost. DefaultcostFunctionassigns value 1 to any transition. The searched area is limited by a circle with radiusmaximumDistanceFromToand center into. Default value ofmaximumDistanceFromTois arbitrary value ofmax(distanceTo(to) * 2, 10). -
distanceTo(
Hex other) → int - Distance to the given hexagon - the number of steps needed to get to the given hexagon.
-
interpolate(
Hex target, double t, [EasingFunction easing = Easing.linear]) → Hex - Interpolates between this hex and the target hex using the specified easing function. The parameter t should be between 0 and 1, where 0 returns this hex and 1 returns the target hex. Returns a new Hex instance.
-
isNeighborOf(
Hex other) → bool -
line(
Hex to) → Iterable< Hex> -
neighbors(
) → List< Hex> - All my six neighboring hexagons.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
randomHexInArea(
int radius) → Hex - Random hex with distance to 'this' <= radius. Probability is the same for all hexes within the area (as opposed to higher probability near the center).
-
randomNeighbor(
) → Hex - Chooses random neighbor.
-
randomNeighborWhere(
HexFilter filter) → Hex? - Chooses random neighbor that matches the given filter.
-
randomShape(
int hexCount, {num spread = 0.3}) → Iterable< Hex> -
Generates random shape. The shape is a list of randomly chosen (but connected) hexes, with
hexCountmembers, one of these hexes is 'this'. There is no guarantee of position of 'this' within the shape. -
ring(
int radius) → Iterable< Hex> - Creates a list of all hexes with the given radius=distance (a 'ring' with 'this' in the center).
-
rotateAround(
Hex center, int steps) → Hex - Rotates this hex around the given center hex by the specified number of steps. Each step is a 60-degree rotation clockwise. Returns a new Hex instance.
-
serialize(
) → String - Convenient method to serialize hex to string (uses id)
-
spiral(
int radius) → Iterable< Hex> - Creates a list of all hexes with the distance <= radius (a 'spiral' with 'this' in the center).
-
toOffset(
{GridLayout gridLayout = GridLayout.POINTY_TOP}) → GridOffset - Creates GridOffset coordinates from this hexagon, using the given GridLayout.
-
topLeftPoint(
double size, {GridLayout gridLayout = GridLayout.POINTY_TOP}) → PixelPoint - Top left corner of this hex in a pixel grid (use it to draw the hex using a raster graphics).
-
toString(
) → String -
A string representation of this object.
override
-
vertices(
double size, {GridLayout gridLayout = GridLayout.POINTY_TOP, double padding = 0}) → List< PixelPoint> - Returns a list of vertices of this hex in a pixel grid (use it to draw the hex using a vector graphics). See: https://www.redblobgames.com/grids/hexagons/#basics
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Static Methods
-
deserialize(
String id) → Hex - Convenient method to deserialize hex from string (uses Hex.fromId)