mt.geo2d.polygon_integral

Polygon in 2D.

A polygon is represented as a collection of 2D points in either clockwise or counter-clockwise order. It is stored in a numpy array of shape (N,2).

Functions

  • trapezium_integral(): Applies the Shoelace algorithm to integrate a function over the interior of a polygon. Shoelace algorithm views the polygon as a sum of trapeziums.

  • signed_area(): Returns the signed area of a polygon.

  • moment_x(): Returns the integral of x over the polygon’s interior.

  • moment_y(): Returns the integral of y over the polygon’s interior.

  • moment_xy(): Returns the integral of x*y over the polygon’s interior.

  • moment_xx(): Returns the integral of x*x over the polygon’s interior.

  • moment_yy(): Returns the integral of y*y over the polygon’s interior.

  • to_moments2d(): Computes all moments, up to 2nd-order of the polygon’s interior.

mt.geo2d.polygon_integral.trapezium_integral(poly, func)

Applies the Shoelace algorithm to integrate a function over the interior of a polygon. Shoelace algorithm views the polygon as a sum of trapeziums.

Parameters:
  • poly (Polygon) – a polygon

  • func (function) – a function that takes x1, y1, x2, y2 as input and returns a scalar

Returns:

the integral over the polygon’s interior

Return type:

scalar

Notes

We use OpenGL’s convention here. x-axis points to the right side and y-axis points upward

Examples

>>> from mt.geo2d.polygon_integral import trapezium_integral
>>> from mt.geo2d.polygon import Polygon
>>> poly = Polygon([[1,2]])
>>> trapezium_integral(poly, None)
0
>>> poly = Polygon([[20,10],[30,20]])
>>> trapezium_integral(poly, None)
0
>>> poly = Polygon([[1,1],[0,0],[1,0]])
>>> trapezium_integral(poly, lambda x1, y1, x2, y2: x1*y2-x2*y1)
1

References

mt.geo2d.polygon_integral.signed_area(poly)

Returns the signed area of a polygon.

Parameters:

poly (Polygon) – a polygon

Returns:

the signed area over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import signed_area
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[10,10],[20,10],[20,20]])
>>> round(signed_area(poly), 3)
-50.0
mt.geo2d.polygon_integral.moment_x(poly)

Returns the integral of x over the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

the moment int(x, dx, dy) over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import moment_x
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,4],[2,3],[3,2]])
>>> round(moment_x(poly), 3)
-2.667
mt.geo2d.polygon_integral.moment_y(poly)

Returns the integral of y over the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

the moment int(y, dx, dy) over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import moment_y
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,3],[2,2],[3,1]])
>>> round(moment_y(poly), 3)
-2.0
mt.geo2d.polygon_integral.moment_xy(poly)

Returns the integral of x*y over the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

the moment int(x*y, dx, dy) over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import moment_xy
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,3],[2,2],[3,1]])
>>> round(moment_xy(poly), 3)
-5.333
mt.geo2d.polygon_integral.moment_xx(poly)

Returns the integral of x*x over the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

the moment int(x*x, dx, dy) over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import moment_xx
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,3],[2,2],[3,1]])
>>> round(moment_xx(poly), 3)
-7.167
mt.geo2d.polygon_integral.moment_yy(poly)

Returns the integral of y*y over the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

the moment int(y*y, dx, dy) over the polygon’s interior

Return type:

scalar

Examples

>>> from mt.geo2d.polygon_integral import moment_yy
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,3],[2,2],[3,1]])
>>> round(moment_yy(poly), 3)
-4.167
mt.geo2d.polygon_integral.to_moments2d(poly)

Computes all moments, up to 2nd-order of the polygon’s interior.

Parameters:

poly (Polygon) – a polygon

Returns:

th ecollection of moments up to 2nd order

Return type:

Momens2d

Examples

>>> from mt.geo2d.polygon_integral import to_moments2d
>>> import mt.geo2d.polygon as mp
>>> poly = mp.Polygon([[3,3],[2,2],[3,1]])
>>> m = to_moments2d(poly)
>>> round(m.m0, 3)
-1.0
>>> round(m.m1.sum(), 3)
-4.667
>>> round(m.m2.sum(), 3)
-22.0
>>> round(m.mean.sum(), 3)
4.667
>>> round(m.cov.sum(), 3)
-22.444