View Source Scurry.PolygonMap (Scurry v2.0.1)
Utility functions to work on a polygon map.
A polygon map is a set of a primary polygon - the main boundary that outlines the world - and a list of polygons that make "holes" in the main polygon.
See Polygon for details on how polygons are composed.
The use case is eg. making a map with obstacles, and use the Astar module
to find the shortest path between points in the map.
Summary
Functions
Given a polygon map (main & holes) and list of vertices, makes the graph.
Given a polygon map (main & holes), list of vertices and the initial graph,
extend the graph with extra points.
Given a polygon map (main, & holes), returns a list of vertices.
Checks if there's a line-of-sight (LOS) from start to stop within the map.
Find the nearest point on the line that is inside the map and outside a hole.
Functions
Given a polygon map (main & holes) and list of vertices, makes the graph.
Params
cost_fun, anode, node :: costfunction, defaults toVector.distance
TODO: this should ideally take line_of_sight as a function so users can
customise which vertices can reach each other. But for now, users can make
the graph themselves just as easily.
extend_graph(graph, polygon, holes, vertices, points, cost_fun \\ nil)
View SourceGiven a polygon map (main & holes), list of vertices and the initial graph,
extend the graph with extra points.
This is used to "temporarily" expand the fixed walk graph with the start and end-point. This is a performance optimisation that saves work by reusing the fixed nodes and extend it with the moveable points.
Params
polygons, a%{main: [...], hole: [...], hole2: [...]}polygon map.graph, the fixed graph, eg. created viacreate_graph/2.verticesthe nodes used to creategraph.pointsa list of coordinates,[{x, y}, {x, y}...], to extendcost_fun, anode, node :: costfunction, defaults toVector.distance
Returns an extended graph plus the combined list of vertices and new points,
{new_graph, new_vertices}.
Given a polygon map (main, & holes), returns a list of vertices.
The vertices are the main polygon's concave vertices and the convex ones of the holes.
Checks if there's a line-of-sight (LOS) from start to stop within the map.
Params
polygon, a list of{x, y}vertices. This is the main boundary map.holes, a list of lists of{x, y}vertices. These are holes withinpolygon.linea tuple of points ({{ax, ay}, {bx, by}}) describing a line.
Returns true if there's a line-of-sight and none of the main polygon or
holes obstruct the path. false otherwise.
As the map consists of a boundary polygon with holes, LOS implies a few things;
- If either
startorstopis outsidepolygon, the result will be false. Even if both are outside, that's not considered a valid LOS. - If the distance between
startandstopis tiny (< 0.1 arbitrarily), LOS is true. - Next, it checks that the line between
startandstophas no intersections withpolygonorholes. - Finally it checks if the middle of the line between
startandstopis insidepolygonand outside all holes - this ensures that corner-to-corner across a hole isn't considered a LOS.
Find the nearest point on the line that is inside the map and outside a hole.
Params
polygon, a list of{x, y}vertices. This is the main boundary map.holes, a list of lists of{x, y}vertices. These are holes withinpolygon.pointa tuple of coordinates ({x, y}) describing a point
The function will return a new point {bx, by} for b such that;
if
{bx, by}is outside the main map, the new b is the closest point on the main map.if b is inside the main map, but also inside a hole, the new bis the closest point on the holes edges.