Check if a given point lies inside a Polygon

In this problem, one polygon is given, and a point P is also given. We need to check whether the point is inside the polygon or outside the polygon.

For solving it we will draw a straight line from the point P. It extends to the infinity. The line is horizontal, or it is parallel to the x-axis.

From that line, we will count how many times the line intersects the sides of a polygon. When the point is inside the polygon, it will intersect the sides, an odd number of times, if P is placed on any side of the polygon, then it will cut an even number of times. If none of the condition is true, then it is outside polygon.

Input and Output

Input:
Points of a polygon {(0, 0), (10, 0), (10, 10), (0, 10)}. And point P (5, 3) to check.
Output:
Point is inside.

Algorithm

checkInside(Poly, n, p)

Input: The points of the polygon, the number of points of the polygon, the point p to check.

Output: True when p is inside the polygon, otherwise false.

Begin
   if n

Example

#include
using namespace std;

struct Point {
   int x, y;
};

struct line {
   Point p1, p2;
};

bool onLine(line l1, Point p) {        //check whether p is on the line or not
   if(p.x 

Output

Point is inside.
Updated on: 2020-06-17T09:18:13+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements