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 specification says: "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.
# x, y -- x and y coordinates of point # a list of tuples [(x, y), (x, y), ...] def isPointInPath(x, y, poly): num = len(poly) i = 0 j = num - 1 c = False for i in range(num): if ((poly[i] > y) != (poly[j] > y)) and \ (x < (poly[j] - poly[i]) * (y - poly[i]) / (poly[j] - poly[i]) + poly[i]): c = not c j = i return c
|This computer programming-related article is a stub. You can help Wikipedia by expanding it.|