Even Odd method & Winding number Method-Inside & Outside Test of a Polygon
Last Updated :
17 Apr, 2023
Introduction :
A polygon may be represented as a number of line segments connected, end to form a closed figure.
Polygons can be represented in two ways -
(i) outline from using move commands and
(ii) as a solid objects by setting the pixels high inside the polygon including pixels on the boundary.
To determine a point lies inside a polygon or not, in computer graphics, we have two methods :
(a) Even-Odd method (odd-parity rule)
(b) Winding number Method-Inside
Even-Odd method :
Constructing a line segment between the point (P) to be examined and a known point outside the polygon is the one way to determine a point lies inside a polygon or not. The number of times the line segment intersects the polygon boundary is then counted. The point (P) is an internal point if the number of polygon edges intersected by this line is odd; otherwise, the point is an external point.
In the figure, the line segment from 'A' crosses single edge & hence point A is inside the polygon. The point B is also inside the polygon as the line segment from B crosses three (odd) edges. But point C is outside the polygon it as the line segment from C crosses two (even) edges.
Polygon
But this even-odd test fails when the intersection point is a vertex.
To handle this case, we have to make few modifications.
We must look at the other end points of the two segments of a polygon which meet at this vertex. If these points lies on the same side of the constructed line A'P', then the intersection point counts as an even number of intersection. But if they lie on the opposite side of constructed line AP, then the intersection points counts as a single intersection.
Polygon
As we can see the line segment A'P' intersects at M which is a vertex and L & Z are the other end points of the two segments meeting at M. L & Z lie on same side of the line segment A'P', so the count is taken as even.
Winding Number Method :
There is another alternative method for defining a polygons' interior point is called the winding number method. Conceptually, one can stretch a piece of elastic between the point (P) to be checked and the point on the polygon boundary.
Treat that, elastic is tied to point (P) to be checked firmly and the other end of the elastic is sliding along the boundary of the polygon until it has made one complete circuit. Then we check that how many times the elastic has been wound around the point of intersection. If it is wound at-least once, point is inside. If there is no net winding then point is outside.
In this method, instead of just counting the intersections, we give each boundary line crossed a direction number, and we sum these directions numbers. The direction number indicates the direction of the polygon edge was drawn relative to the line segment we constructed for the test.
Example : To test a point (xi, yi), let us consider a horizontal line segment y = yi which runs from outside the polygon to (xi, yi). We find all the sides which crossed this line segment.
Now there are 2 ways for side to cross, the side could be drawn starting below end, cross it and end above the line. In this case we can give direction numbers - 1, to the side or the edge could start above the line & finish below it in this case, given a direction 1. The sum of the direction numbers for the sides that cross the constructed horizontal line segment yield the "Winding Number" for the point.
If the winding number is non-zero , the point is interior to polygon, else, exterior to polygon.
Polygon
In the above figure, the line segment crosses 4 edges having different direction numbers : 1, -1, 1& -1 respectively, then :
Winding Number = 1 + (-1) + 1 + (-1) = 0
So the point P is outside the Polygon. The edge has direction Number -1 because it starts below the line segment & finishes above. Similarly, edge has direction Number +1 because it starts from above the line segment & finishes below the line segment (See the directions in the figure).
Similar Reads
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec
14 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
6 min read