# Weiler–Atherton clipping algorithm

The Weiler–Atherton clipping algorithm is used in computer graphics. It allows clipping of a subject polygon by an arbitrarily shaped clip polygon window. It is generally applicable only in 2D. However, it can be used in 3D through visible surface determination and with improved efficiency through Z-ordering.[1]

## Description

Subdivision with the Weiler-Atherton algorithm

The algorithm requires polygons to be clockwise and not reentrant (self intersecting). The algorithm can support holes (as counter-clockwise polygons wholly inside their parent polygon), but requires additional algorithms to decide which polygons are holes. Merging of polygons can also be performed by a variant of the algorithm.

Two lists are created from the coordinates of each polygons A and B, where A is the clip region and B is the polygon to be clipped.

The list entries are labelled as either inside or outside the other polygon. Various strategies can be used to improve the speed of this labelling, and to avoid needing to proceed further.

All the polygon intersections are then found and are inserted into both lists, linking the lists at the intersections. Care will be needed where the polygons share an edge.

If there are no intersections then one of three situations exist:

1. A is inside B - return A for clipping, B for merging.
2. B is inside A - return B for clipping, A for merging.
3. A and B do not overlap - return None for clipping or A & B for merging.

A list of inbound intersections is then generated. Each intersection in the list is then followed clockwise around the linked lists until the start position is found. One or more concave polygons may produce more than one intersecting polygon. Convex polygons will only have one intersecting polygon.

The same algorithm can be used for merging two polygons by starting at the outbound intersections rather than the inbound ones. However this can produce counter-clockwise holes.

Some polygon combinations may be difficult to resolve, especially when holes are allowed.

Points very close to the edge of the other polygon may be considered as both in and out until their status can be confirmed after all the intersections have been found and verified, however this increases the complexity.