Main Content

ispolycw

Determine if polygon vertices are in clockwise order

    Description

    example

    tf = ispolycw(x,y) determines if the Cartesian vertices of the polygon defined by x and y are in clockwise order. The function returns logical 1 (true) when the vertices are in clockwise order and logical 0 (false) otherwise.

    Examples

    collapse all

    Specify the vertices of a polygon. Close the polygon by repeating the first vertex at the end of the list. Then, determine whether the vertices are in clockwise order.

    x1 = [0 1 4 3 0];
    y1 = [0 1 3 2 0];
    ispolycw(x1,y1)
    ans = logical
       1
    
    

    The vertices are in clockwise order. Flip the order of the vertices and then verify that the flipped vertices are not in clockwise order.

    x2 = fliplr(x1);
    y2 = fliplr(y1);
    ispolycw(x2,y2)
    ans = logical
       0
    
    

    Input Arguments

    collapse all

    x-coordinates of the polygon, specified as a numeric vector or a cell array of numeric vectors.

    • Define one polygon by specifying a vector, such as [39 45 19 39].

    • Define multiple polygons by using one of these options:

      • Specify a vector and separate the polygons using NaN values, such as [37 46 31 20 37 NaN 45 49 35 32 45 NaN 35 40 42 35]. The NaN values in x must correspond to the NaN values in y.

      • Specify a cell array of vectors, such as {[37 46 31 20 37],[45 49 35 32 45],[35 40 42 35]}. The size of the vector in each cell of x must match the size of the vector in the corresponding cell of y.

    The size and type of x must match the size and type of y.

    y-coordinates of the polygon, specified as a numeric vector or a cell array of numeric vectors.

    • Define one polygon by specifying a vector, such as [-113 -49 -100 -113].

    • Define multiple polygons by using one of these options:

      • Specify a vector and separate the polygons using NaN values, such as [69 90 105 79 69 NaN 6 52 43 14 6 NaN 18 32 22 18]. The NaN values in y must correspond to the NaN values in x.

      • Specify a cell array of vectors, such as {[69 90 105 79 69],[6 52 43 14 6],[18 32 22 18]}. The size of the vector in each cell of y must match the size of the vector in the corresponding cell of x.

    The size and type of y must match the size and type of x.

    Output Arguments

    collapse all

    Indicator for vertices in a clockwise order, returned as a logical vector.

    • A logical 1 (true) indicates that the corresponding vertices of the polygon are in clockwise order.

    • A logical 0 (false) indicates that the corresponding vertices of the polygon are not in clockwise order.

    When x and y define multiple polygons, the ispolycw function determines whether the coordinates of each individual polygon are in clockwise order.

    The size of tf is the number of polygons specified by x and y.

    Tips

    • The ispolycw function returns 1 (true) when a polygon contains two or fewer vertices.

    • You can specify geographic coordinates as input to the ispolycw function when the polygon does not cross the Antimeridian or contain a pole. A polygon contains a pole when the longitude data spans 360 degrees. To use geographic coordinates as input, specify x using the longitude vector and y using the latitude vector.

    Algorithms

    When a polygon intersects itself, the order of the vertices is not well defined. In this case, the ispolycw function determines the vertex order using the vertices immediately before and after the leftmost vertex with the lowest y value. If the function does not determine the order from the leftmost vertex with the lowest y value, it determines the order by using a signed area test.

    Version History

    Introduced before R2006a