# `pymunk.autogeometry` Module

This module contain functions for automatic generation of geometry, for example from an image.

Example:

    >>> import pymunk
    >>> from pymunk.autogeometry import march_soft
    >>> img = [
    ...     "  xx   ",
    ...     "  xx   ",
    ...     "  xx   ",
    ...     "  xx   ",
    ...     "  xx   ",
    ...     "  xxxxx",
    ...     "  xxxxx",
    ... ]
    >>> segments = []

    >>> def segment_func(v0, v1):
    ...     segments.append((tuple(v0), tuple(v1)))
    >>> def sample_func(point):
    ...     x = int(point.x)
    ...     y = int(point.y)
    ...     return 1 if img[y][x] == "x" else 0

    >>> march_soft(pymunk.BB(0,0,6,6), 7, 7, .5, segment_func, sample_func)
    >>> print(len(segments))
    13

The information in segments can now be used to create geometry, for example as a Pymunk Poly or Segment:

    >>> s = pymunk.Space()
    >>> for (a,b) in segments:
    ...     segment = pymunk.Segment(s.static_body, a, b, 5)
    ...     s.add(segment)

`pymunk.autogeometry.``is_closed`(polyline)

  Returns true if the first vertex is equal to the last.

  Parameters

    polyline ([(float,float)]) – Polyline to simplify.

  Return type

    bool

`pymunk.autogeometry.``simplify_curves`(polyline, tolerance)

  Returns a copy of a polyline simplified by using the Douglas-Peucker algorithm.

  This works very well on smooth or gently curved shapes, but not well on straight edged or angular shapes.

  Parameters

    - polyline ([(float,float)]) – Polyline to simplify.

    - tolerance (float) – A higher value means more error is tolerated.

  Return type

    [(float,float)]

`pymunk.autogeometry.``simplify_vertexes`(polyline, tolerance)

  Returns a copy of a polyline simplified by discarding “flat” vertexes.

  This works well on straight edged or angular shapes, not as well on smooth shapes.

  Parameters

    - polyline ([(float,float)]) – Polyline to simplify.

    - tolerance (float) – A higher value means more error is tolerated.

  Return type

    [(float,float)]

`pymunk.autogeometry.``to_convex_hull`(polyline, tolerance)

  Get the convex hull of a polyline as a looped polyline.

  Parameters

    - polyline ([(float,float)]) – Polyline to simplify.

    - tolerance (float) – A higher value means more error is tolerated.

  Return type

    [(float,float)]

`pymunk.autogeometry.``convex_decomposition`(polyline, tolerance)

  Get an approximate convex decomposition from a polyline.

  Returns a list of convex hulls that match the original shape to within tolerance.

  Note

  If the input is a self intersecting polygon, the output might end up overly simplified.

  Parameters

    - polyline ([(float,float)]) – Polyline to simplify.

    - tolerance (float) – A higher value means more error is tolerated.

  Return type

    [(float,float)]

class `pymunk.autogeometry.``PolylineSet`

  Bases: `collections.abc.Sequence`

  A set of Polylines.

  Mainly intended to be used for its `collect_segment()` function when generating geometry with the `march_soft()` and `march_hard()` functions.

  `__init__`()

    Initialize self. See help(type(self)) for accurate signature.

  `collect_segment`(v0, v1)

    Add a line segment to a polyline set.

    A segment will either start a new polyline, join two others, or add to or loop an existing polyline. This is mostly intended to be used as a callback directly from `march_soft()` or `march_hard()`.

    Parameters

      - v0 ((float,float)) – Start of segment

      - v1 ((float,float)) – End of segment

  `count`(value) → integer -- return number of occurrences of value
  `index`(value[, start[, stop]]) → integer -- return first index of value.

    Raises ValueError if the value is not present.

    Supporting start and stop arguments is optional, but recommended.

`pymunk.autogeometry.``march_soft`(bb, x_samples, y_samples, threshold, segment_func, sample_func)

  Trace an anti-aliased contour of an image along a particular threshold.

  The given number of samples will be taken and spread across the bounding box area using the sampling function and context.

  Parameters

    - bb (BB) – Bounding box of the area to sample within

    - x_samples (int) – Number of samples in x

    - y_samples (int) – Number of samples in y

    - threshold (float) – A higher value means more error is tolerated

    - segment_func (`func(v0 : Vec2d, v1 : Vec2d)`) – The segment function will be called for each segment detected that lies along the density contour for threshold.

    - sample_func (`func(point: Vec2d) -> float`) – The sample function will be called for x_samples * y_samples spread across the bounding box area, and should return a float.

`pymunk.autogeometry.``march_hard`(bb, x_samples, y_samples, threshold, segment_func, sample_func)

  Trace an aliased curve of an image along a particular threshold.

  The given number of samples will be taken and spread across the bounding box area using the sampling function and context.

  Parameters

    - bb (BB) – Bounding box of the area to sample within

    - x_samples (int) – Number of samples in x

    - y_samples (int) – Number of samples in y

    - threshold (float) – A higher value means more error is tolerated

    - segment_func (`func(v0 : Vec2d, v1 : Vec2d)`) – The segment function will be called for each segment detected that lies along the density contour for threshold.

    - sample_func (`func(point: Vec2d) -> float`) – The sample function will be called for x_samples * y_samples spread across the bounding box area, and should return a float.