View Source Scurry.Geo (Scurry v2.0.0)

Functions related to lines relevant for 2D map pathfinding.

Link to this section Summary

Functions

Get the distance from a point to a line/segment, aka the square root of distance_squared/2.

Get the distance squared from a point to a line/segment.

Check if two lines intersect

Determine if, where and how two lines intersect.

Link to this section Functions

Link to this function

distance_to_segment(line, point)

View Source

Get the distance from a point to a line/segment, aka the square root of distance_squared/2.

params

Params

  • line a tuple of points ({{ax, ay}, {bx, by}}) describing a line.
  • point a tuple {x, y} describing a point

This returns the distance beween the given point and segment as a float.

examples

Examples

iex> Geo.distance_to_segment({{2, 0}, {2, 2}}, {0, 1})
2.0
Link to this function

distance_to_segment_squared(line, point)

View Source

Get the distance squared from a point to a line/segment.

params

Params

  • line a tuple of points ({{ax, ay}, {bx, by}}) describing a line.
  • point a tuple {x, y} describing a point

This returns the square of the distance beween the given point and segment as a float.

examples

Examples

iex> Geo.distance_to_segment_squared({{2, 0}, {2, 2}}, {0, 1})
4.0
Link to this function

do_lines_intersect?(l1, l2)

View Source

Check if two lines intersect

This is a simpler version of line_segment_intersection/2, which is typically a better choice since it handles endpoints and segment overlap too.

params

Params

  • line1 a {{x1, y1}, {x2, y2}} line segment
  • line2 a {{x3, y13, {x4, y4}} line segment

Returns true if they intersect, false otherwise.

Note that this doesn't handle segment overlap or points touching. Use line_segment_intersection/2 instead for that level of detail.

Link to this function

line_segment_intersection(line1, line2)

View Source

Determine if, where and how two lines intersect.

params

Params

  • line1 a {{x1, y1}, {x2, y2}} line segment
  • line2 a {{x3, y13, {x4, y4}} line segment

Returns

  • :on_segment one line is on the other.
  • :parallel the lines are parallel and do not intersect.
  • {:point_intersection, {x, y}} either line has an endpoint ({x, y}) on the other line.
  • {:intersection, {x, y}} the lines intersect at {x, y}.
  • :none no intersection.

examples

Examples

iex> Geo.line_segment_intersection({{1, 2}, {4, 2}}, {{2, 0}, {3, 0}})
:parallel
iex> Geo.line_segment_intersection({{1, 2}, {4, 2}}, {{2, 2}, {3, 2}})
:on_segment
iex> Geo.line_segment_intersection({{1, 2}, {4, 2}}, {{2, 0}, {2, 1}})
:none
iex> Geo.line_segment_intersection({{1, 2}, {4, 2}}, {{2, 0}, {2, 2}})
{:point_intersection, {2.0, 2.0}}
iex> Geo.line_segment_intersection({{1, 2}, {4, 2}}, {{2, 0}, {2, 3}})
{:intersection, {2.0, 2.0}}