M-File Help: Polygon | View code for Polygon

# Polygon

Polygon class

A general class for manipulating polygons and vectors of polygons.

## Methods

plot | Plot polygon
area | Area of polygon
moments | Moments of polygon
centroid | Centroid of polygon
perimeter | Perimter of polygon
transform | Transform polygon
inside | Test if points are inside polygon
intersection | Intersection of two polygons
difference | Difference of two polygons
union | Union of two polygons
xor | Exclusive or of two polygons
display | print the polygon in human readable form
char | convert the polgyon to human readable string

## Properties

vertices | List of polygon vertices, one per column
extent | Bounding box [minx maxx; miny maxy]
n | Number of vertices

## Notes

- This is reference class object
- Polygon objects can be used in vectors and arrays

## Acknowledgement

The methods inside, intersection, difference, union, and xor are based on code written by:

Kirill K. Pankratov, kirill@plume.mit.edu, http://puddle.mit.edu/~glenn/kirill/saga.html

and require a licence. However the author does not respond to email regarding the licence, so use with care, and modify with acknowledgement.

# Polygon.Polygon

Polygon class constructor

p = Polygon(v) is a polygon with vertices given by v, one column per vertex.

p = Polygon(C, wh) is a rectangle centred at C with dimensions wh=[WIDTH, HEIGHT].

# Polygon.area

Area of polygon

a = P.area() is the area of the polygon.

# Polygon.centroid

Centroid of polygon

x = P.centroid() is the centroid of the polygon.

# Polygon.char

String representation

s = P.char() is a compact representation of the polgyon in human readable form.

# Polygon.difference

Difference of polygons

d = P.difference(q) is polygon P minus polygon q.

## Notes

- If polygons P and Q are not intersecting, returns coordinates of P.
- If the result D is not simply connected or consists of several polygons, resulting vertex list will contain NaNs.

# Polygon.display

Display polygon

P.display() displays the polygon in a compact human readable form.

## See also

Polygon.char

# Polygon.inside

Test if points are inside polygon

in = p.inside(p) tests if points given by columns of p are inside the polygon. The corresponding elements of in are either true or false.

# Polygon.intersect

Intersection of polygon with list of polygons

i = P.intersect(plist) indicates whether or not the Polygon P intersects with

i(j) = 1 if p intersects polylist(j), else 0.

# Polygon.intersect_line

Intersection of polygon and line segment

i = P.intersect_line(L) is the intersection points of a polygon P with the line segment L=[x1 x2; y1 y2]. i is an Nx2 matrix with one column per intersection, each column is [x y]'.

# Polygon.intersection

Intersection of polygons

i = P.intersection(q) is a Polygon representing the intersection of polygons P and q.

## Notes

- If these polygons are not intersecting, returns empty polygon.
- If intersection consist of several disjoint polygons (for non-convex P or Q) then vertices of I is the concatenation of the vertices of these polygons.

# Polygon.linechk

Input checking for line segments.

# Polygon.moments

Moments of polygon

a = P.moments(p, q) is the pq'th moment of the polygon.

## See also

mpq_poly

# Polygon.perimeter

Perimeter of polygon

L = P.perimeter() is the perimeter of the polygon.

# Polygon.plot

Plot polygon

P.plot() plot the polygon.

P.plot(ls) as above but pass the arguments ls to plot.

# Polygon.transform

Transformation of polygon vertices

p2 = P.transform(T) is a new Polygon object whose vertices have been transfored by the 3x3 homgoeneous transformation T.

# Polygon.union

Union of polygons

i = P.union(q) is a Polygon representing the union of polygons P and q.

## Notes

- If these polygons are not intersecting, returns a polygon with vertices of both polygons separated by NaNs.
- If the result P is not simply connected (such as a polygon with a "hole") the resulting contour consist of counter- clockwise "outer boundary" and one or more clock-wise "inner boundaries" around "holes".

# Polygon.xor

Exclusive or of polygons

i = P.union(q) is a Polygon representing the union of polygons P and q.

## Notes

- If these polygons are not intersecting, returns a polygon with vertices of both polygons separated by NaNs.
- If the result P is not simply connected (such as a polygon with a "hole") the resulting contour consist of counter- clockwise "outer boundary" and one or more clock-wise "inner boundaries" around "holes".

© 1990-2014 Peter Corke.