The even–odd rule is an algorithm implemented in vector-based graphic software, like the PostScript language and Scalable Vector Graphics (SVG), which determines how a graphical shape with more than one closed outline will be filled. Unlike the nonzero-rule algorithm, this algorithm will alternatively color and leave uncolored shapes defined by nested closed paths irrespective of their winding.
The SVG defines the even–odd rule by saying:
This rule determines the "insideness" of a point on the canvas by drawing a ray from that point to infinity in any direction and counting the number of path segments from the given shape that the ray crosses. If this number is odd, the point is inside; if even, the point is outside.
On a simple curve, the even–odd rule reduces to a decision algorithm for the point in polygon problem.
def is_point_in_path(x: int, y: int, poly) -> bool: # Determine if the point is on the path, corner, or boundary of the polygon # # Args: # x -- The x coordinates of point. # y -- The y coordinates of point. # poly -- a list of tuples [(x, y), (x, y), ...] # # Returns: # True if the point is in the path or is a corner or on the boundary num = len(poly) j = num - 1 c = False for i in range(num): if (x == poly[i]) and (y == poly[i]): # point is a corner return True if ((poly[i] > y) != (poly[j] > y)): slope = (x-poly[i])*(poly[j]-poly[i])-(poly[j]-poly[i])*(y-poly[i]) if slope == 0: # point is on boundary return True if (slope < 0) != (poly[j] < poly[i]): c = not c j = i return c
- J. D. Foley, A. van Dam, S. K. Feiner, and J. F. Hughes. Computer Graphics: Principles and Practice. The Systems Programming Series. Addison-Wesley, Reading, 2nd edition, 1990.
- , w3c.org, retrieved 2019-03-28
- "PNPOLY - Point Inclusion in Polygon Test - WR Franklin (WRF)".