Bardo.Examples.Applications.Flatland.FlatlandUtils (Bardo v0.1.0)

View Source

Utility functions for the Flatland simulation.

This module provides utility functions for creating and managing the Flatland environment, including world creation, agent placement, and ray-object intersections for sensor calculations.

Summary

Functions

Creates a new Flatland world with the specified dimensions.

Normalizes a vector to a unit vector.

Maps object type to color value.

Places a specified number of predators and prey randomly in the world.

Places a specified number of plants randomly in the world.

Calculates the shortest intersection line between a ray and a circular object.

Calculates the intersection of a ray with the boundary of a rectangular world.

Functions

create_world(width, height)

@spec create_world(integer(), integer()) :: map()

Creates a new Flatland world with the specified dimensions.

Parameters

  • width - The width of the world
  • height - The height of the world

Returns

  • A map representing the Flatland world with plants, predators, and prey

normalize_vector(arg)

@spec normalize_vector({float(), float()}) :: {float(), float()}

Normalizes a vector to a unit vector.

object_color_value(type)

@spec object_color_value(atom()) :: float()

Maps object type to color value.

Returns:

  • -0.5 for plants (green)
  • 0.0 for prey (blue)
  • 0.5 for predators (red)

place_agents_randomly(world, predator_count, prey_count)

@spec place_agents_randomly(map(), integer(), integer()) :: map()

Places a specified number of predators and prey randomly in the world.

Parameters

  • world - The world map
  • predator_count - The number of predators to place
  • prey_count - The number of prey to place

Returns

  • The updated world map with predators and prey

place_plants_randomly(world, plant_count)

@spec place_plants_randomly(map(), integer()) :: map()

Places a specified number of plants randomly in the world.

Parameters

  • world - The world map
  • plant_count - The number of plants to place

Returns

  • The updated world map with plants

shortest_intr_line(ray_origin, ray_dir, object)

@spec shortest_intr_line(
  {float(), float()},
  {float(), float()},
  {float(), float(), float()}
) ::
  float() | :no_intersection

Calculates the shortest intersection line between a ray and a circular object.

Parameters:

  • ray_origin: {x, y} coordinates of ray origin
  • ray_dir: {dx, dy} ray direction vector
  • object: {x, y, r} object position and radius

Returns:

  • :no_intersection if there is no intersection
  • distance to the intersection point

This function is a direct port of the Erlang implementation, which uses vector math to calculate ray-circle intersections.

world_boundary_intersection(ray_origin, ray_dir, width, height)

@spec world_boundary_intersection(
  {float(), float()},
  {float(), float()},
  float(),
  float()
) ::
  {float(), float(), float()} | :no_intersection

Calculates the intersection of a ray with the boundary of a rectangular world.

This is useful for determining how far a ray can travel before hitting a wall, which is important for sensors that need to detect world boundaries.