Skip to content

Radius Shapes

Chris Ridley edited this page Jul 17, 2018 · 13 revisions

The Radius class provides a convenient way to represent possible 2D and 3D shapes of a radius as calculated by the various distance calculations.

Like many grid-related classes, the Radius class cannot be instantiated, but instead provides pre-existing static class instances that represent the possible shapes of a radius resulting from the various methods of calculating distance. While radius in itself currently provides no unique functions, it is implicitly convertible to both the Distance and AdjacencyRule classes, and as such Radius can be provided as a parameter wherever Distance or AdjacencyRule instances are specified.

Table of Contents

Code Examples

Code examples in this section show only code in the Main function. The code provided assumes that the following "using" statements are at the top of the code file:

using GoRogue;
using GoRogue.MapGeneration;
using GoRogue.MapGeneration.Generators;
using GoRogue.MapViews;
using GoRogue.Pathing;
using System.Collections.Generic;
using System.Linq;

Radius Shapes

There are 6 shapes GoRogue provides instances for -- three 2D shapes, and three corresponding 3D shapes. 2D shapes include Radius.CIRCLE, Radius.DIAMOND, and Radius.SQUARE. The corresponding 3D shapes are Radius.SPHERE (corresponding to Radius.CIRCLE), Radius.OCTAHEDRON (corresponding to Radius.DIAMOND), and Radius.CUBE (corresponding to Radius.SQUARE).

Implicit Conversions

The main purpose of the Radius class instances is to provide a type that implicitly converts to Distance and AdjacencyRule instances. It is nearly never necessary for a class to care about the actual shape of a radius as a parameter -- even FOV and RadiusAreaProvider, since they work only in 2D, can assume the shape they produce from a Distance calculation. Therefore, these classes take the "minimum" amount of information they need (a Distance instance, in those cases), and allow passing in of Radius instances to substitute, via implicit type conversions.

Radius to Distance

Because specifying a radius shape implicitly specifies the distance calculation that produces it, Radius implicitly converts to Distance instances as follows:

  • Radius.CIRCLE or Radius.SPHERE -> Distance.EUCLIDEAN
  • Radius.SQUARE or Radius.CUBE -> Distance.CHEBYSHEV
  • Radius.DIAMOND or Radius.OCTAHEDRON -> Distance.MANHATTAN

The following code example demonstrates that Radius instances are implicitly converted to Distance instances:

// Create a map that consists of one single rectangle -- see
// Map Generation documentation for details.
ArrayMap<bool> testMap = new ArrayMap<bool>(10, 10);
RectangleMapGenerator.Generate(testMap);

// Create an AStar-based pathfinder -- see Pathfinding documentation for details,
// however in short, as the second parameter to its constructor it takes a Distance
// instance specifying how to measure distance. Note that in this case a Radius instance
// is passed, but no error occurs -- the conversion is done automatically.
AStar pather = new AStar(testMap, Radius.SQUARE);
var path = pather.ShortestPath(0, 0, 1, 2);

Radius to AdjacencyRule

By transitivity, since Radius implies Distance and Distance implies AdjacencyRule, Radius also implicitly converts to AdjacencyRule. Radius converts to AdjacencyRule instances as follows:

  • Radius.CIRCLE or Radius.SPHERE -> AdjacencyRule.EIGHT_WAY
  • Radius.SQUARE or Radius.CUBE -> AdjacencyRule.EIGHT_WAY
  • Radius.DIAMOND or Radius.OCTAHEDRON -> AdjacencyRule.CARDINALS

The following code example demonstrates that Radius is implicitly convertible to AdjacencyRule instances:

// Create a map that consists of one single rectangle -- see
// Map Generation documentation for details.
ArrayMap<bool> testMap = new ArrayMap<bool>(10, 10);
RectangleMapGenerator.Generate(testMap);

// Finds all distinct (un-connected) areas of a map.
// See Map Generation documentation for details, however in short
// as its second parameter, this function takes an AdjacencyRule
// that specifies which locations are considered adjacent.
// Note that in this case a Radius instance is passed, but no error
// occurs -- the conversion is done automatically.
List<MapArea> areas = MapAreaFinder.MapAreasFor(testMap, Radius.CIRCLE).ToList();