The Wayback Machine - https://web.archive.org/web/20240823160305/https://www.geeksforgeeks.org/check-if-the-given-point-lies-inside-given-n-points-of-a-convex-polygon/
Open In App

Check if the given point lies inside given N points of a Convex Polygon

Last Updated : 04 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples:

Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 9 Output: NO

Approach: The idea is to use Graham Scan Algorithm to find if the given point lies inside the Convex Polygon or not. Below are some of the observations:

  • Suppose the point (X, Y) is a point in the set of points of the convex polygon. If the Graham Scan Algorithm is used on this set of points, another set of points would be obtained, which makes up the Convex Hull.
  • If the point (X, Y) lies inside the polygon, it won’t lie on the Convex Hull and hence won’t be present in the newly generated set of points of the Convex Hull.
  • If the point (X, Y) lies outside the polygon, it will then lie on the Convex Hull formed and hence would be present in the newly generated set of points of the Convex Hull.

Below are the steps to solve the problem:

  1. Sort the given points along with the query point in the increasing order of their abscissa values. If the abscissa values(x-coordinates) of any two points are the same, then sort them on the basis of their ordinate value.
  2. Set the bottom-left point as the start point and top-right point as the end point of the convex hull.
  3. Iterate over all the points and find out the points, forming the convex polygon, that lies in between the start and endpoints in the clockwise direction. Store these points in a vector.
  4. Iterate over all the points and find out the points, forming the convex polygon, that lies in between the start and endpoints in the counter-clockwise direction. Store these points in the vector.
  5. Check if the query point exists in the vector then the point lies outside the convex hull. So return “No”.
  6. If the point doesn’t exist in the vector, then the point lies inside the convex hull print “Yes”.

Below is the implementation based on the above approach: 

CPP




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
   
// Sorting Function to sort points
bool cmp(pair<int, int>& a,
         pair<int, int>& b)
{
   
    if (a.first == b.first)
        return a.second < b.second;
    return a.first < b.first;
}
   
// Function To Check Clockwise
// Orientation
int cw(pair<int, int>& a,
       pair<int, int>& b,
       pair<int, int>& c)
{
   
    int p = a.first * (b.second - c.second)
            + b.first * (c.second - a.second)
            + c.first * (a.second - b.second);
   
    return p < 0ll;
}
   
// Function To Check Counter
// Clockwise Orientation
int ccw(pair<int, int>& a,
        pair<int, int>& b,
        pair<int, int>& c)
{
   
    int p = a.first * (b.second - c.second)
            + b.first * (c.second - a.second)
            + c.first * (a.second - b.second);
   
    return p > 0ll;
}
   
// Graham Scan algorithm to find Convex
// Hull from given points
vector<pair<int, int> > convexHull(
    vector<pair<int, int> >& v)
{
    // Sort the points
    sort(v.begin(),
         v.end(), cmp);
   
    int n = v.size();
    if (n <= 3)
        return v;
   
    // Set starting and ending points as
    // left bottom and top right
    pair<int, int> p1 = v[0];
    pair<int, int> p2 = v[n - 1];
   
    // Vector to store points in
    // upper half and lower half
    vector<pair<int, int> > up, down;
   
    // Insert StartingEnding Points
    up.push_back(p1);
    down.push_back(p1);
   
    // Iterate over points
    for (int i = 1; i < n; i++) {
   
        if (i == n - 1 || !ccw(p1, v[i], p2)) {
   
            while (up.size() > 1
                   && ccw(up[up.size() - 2],
                          up[up.size() - 1],
                          v[i])) {
   
                // Exclude this point
                // if we can form better
   
                up.pop_back();
            }
   
            up.push_back(v[i]);
        }
   
        if (i == n - 1 || !cw(p1, v[i], p2)) {
   
            while (down.size() > 1
                   && cw(down[down.size() - 2],
                         down[down.size() - 1],
                         v[i])) {
   
                // Exclude this point
                // if we can form better
                down.pop_back();
            }
            down.push_back(v[i]);
        }
    }
   
    // Combine upper and  lower half
    for (int i = down.size() - 2;
         i > 0; i--)
        up.push_back(down[i]);
   
    // Remove duplicate points
    up.resize(unique(up.begin(),
                     up.end())
              - up.begin());
   
    // Return the points on Convex Hull
    return up;
}
   
// Function to find if point lies inside
// a convex polygon
bool isInside(vector<pair<int, int> > points,
              pair<int, int> query)
{
    // Include the query point in the
    // polygon points
    points.push_back(query);
   
    // Form a convex hull from the points
    points = convexHull(points);
   
    // Iterate over the points
    // of convex hull
    for (auto x : points) {
   
        // If the query point lies
        // on the convex hull
        // then it wasn't inside
        if (x == query)
            return false;
    }
   
    // Otherwise it was Inside
    return true;
}
   
// Driver Code
int main()
{
   
    // Points of the polygon
    // given in any order
    int n = 7;
    vector<pair<int, int> > points;
   
    points = { { 1, 1 }, { 2, 1 }, { 3, 1 },
               { 4, 1 }, { 4, 2 }, { 4, 3 },
               { 4, 4 } };
   
    // Query Points
    pair<int, int> query = { 3, 2 };
   
    // Check if its inside
    if (isInside(points, query)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
   
    return 0;
}


Java




import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class GFG {
    // Function To Check Clockwise Orientation
    static boolean cw(int[] a, int[] b, int[] c)
    {
        int p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1])
                + c[0] * (a[1] - b[1]);
 
        return p < 0;
    }
 
    // Function To Check Counter Clockwise Orientation
    static boolean ccw(int[] a, int[] b, int[] c)
    {
        int p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1])
                + c[0] * (a[1] - b[1]);
 
        return p > 0;
    }
 
    // Graham Scan Algorithm To Find Convex Hull From Given
    // Points
    static int[][] convexHull(int[][] v)
    {
        // Sort The Points
        Arrays.sort(v, (a, b) -> a[0] - b[0]);
 
        int n = v.length;
        if (n <= 3) {
            return v;
        }
 
        // Set Starting And Ending Points As Left Bottom And
        // Top Right
        int[] p1 = v[0];
        int[] p2 = v[n - 1];
 
        // Vector To Store Points In Upper Half And Lower
        // Half
        List<int[]> up = new ArrayList<int[]>();
        List<int[]> down = new ArrayList<int[]>();
 
        // Insert Starting/Ending Points
        up.add(p1);
        down.add(p1);
 
        // Iterate Over Points
        for (int i = 1; i < n; i++) {
            if (i == n - 1 || !ccw(p1, v[i], p2)) {
                while (up.size() > 1
                       && ccw(up.get(up.size() - 2),
                              up.get(up.size() - 1),
                              v[i])) {
                    // Exclude This Point If We Can Form
                    // Better
                    up.remove(up.size() - 1);
                }
 
                up.add(v[i]);
            }
 
            if (i == n - 1 || !cw(p1, v[i], p2)) {
                while (down.size() > 1
                       && cw(down.get(down.size() - 2),
                             down.get(down.size() - 1),
                             v[i])) {
                    // Exclude This Point If We Can Form
                    // Better
                    down.remove(down.size() - 1);
                }
 
                down.add(v[i]);
            }
        }
 
        // Combine Upper And Lower Half
        for (int i = down.size() - 2; i >= 0; i--) {
            up.add(down.get(i));
        }
 
        // Return The Points On Convex Hull
        return up.toArray(new int[0][]);
    }
 
    // Function To Find If Point Lies Inside A convex
    // polygon
    static boolean isInside(int[][] points, int[] query)
    {
        // Include the query point in the
        // polygon points
        int[][] points1 = new int[points.length + 1][];
        for (int i = 0; i < points.length; i++)
            points1[i] = points[i];
        points1[points.length] = query;
 
        // Form a convex hull from the points
        points = convexHull(points);
 
        // Iterate over the points
        // of convex hull
        for (int[] x : points) {
 
            // If the query point lies
            // on the convex hull
            // then it wasn't inside
            if (Arrays.equals(x, query))
                return false;
        }
 
        // Otherwise it was Inside
        return true;
    }
 
    // Driver Code
 
    public static void main(String[] args)
    {
        // Points of the polygon
        // given in any order
        int n = 7;
        int[][] points
            = { new int[] { 1, 1 }, new int[] { 2, 1 },
                new int[] { 3, 1 }, new int[] { 4, 1 },
                new int[] { 4, 2 }, new int[] { 4, 3 },
                new int[] { 4, 4 } };
 
        // Query Points
        int[] query = { 3, 2 };
 
        // Check if its inside
        if (isInside(points, query)) {
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}


Python3




# Python3 program for the above approach
 
# Function To Check Clockwise
# Orientation
def cw(a, b, c):
    p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]);
   
    return p < 0;
 
# Function To Check Counter
# Clockwise Orientation
def ccw(a, b, c):
    p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]);
   
    return p > 0;
 
# Graham Scan algorithm to find Convex
# Hull from given points
def convexHull(v):
 
    # Sort the points
    v.sort();
     
   
    n = len(v);
    if (n <= 3):
        return v;
   
    # Set starting and ending points as
    # left bottom and top right
    p1 = v[0];
    p2 = v[n - 1];
   
    # Vector to store points in
    # upper half and lower half
    up = []
    down = [];
   
    # Insert StartingEnding Points
    up.append(tuple(p1));
    down.append(p1);
   
    # Iterate over points
    for i in range(1, n):
         
        if i == n - 1 or (not ccw(p1, v[i], p2)):
 
            while len(up) > 1 and ccw(up[len(up) - 2], up[len(up) - 1], v[i]):
 
                # Exclude this point
                # if we can form better
   
                up.pop();
             
   
            up.append(tuple(v[i]));
         
   
        if i == n - 1 and  (not cw(p1, v[i], p2)):
   
            while (len(down) > 1) and cw(down[len(down) - 2], down[len(down) - 1], v[i]):
   
                # Exclude this point
                # if we can form better
                down.pop();
             
            down.append(v[i]);
         
    # Combine upper and  lower half
    for i in range(len(down) - 2, -1, -1):
        up.append(tuple(down[i]));
   
    # Remove duplicate points
    up = set(up)
    up = list(up)
   
    # Return the points on Convex Hull
    return up;
 
# Function to find if point lies inside
# a convex polygon
def isInside( points, query):
 
    # Include the query point in the
    # polygon points
    points.append(query);
   
    # Form a convex hull from the points
    points = convexHull(points);
   
    # Iterate over the points
    # of convex hull
    for x in points:
   
        # If the query point lies
        # on the convex hull
        # then it wasn't inside
        if x == query:
            return False;
     
    # Otherwise it was Inside
    return True;
 
# Driver Code
 
# Points of the polygon
# given in any order
n = 7;
points = [[1, 1 ], [2, 1 ], [ 3, 1 ], [ 4, 1 ], [ 4, 2 ], [4, 3 ], [ 4, 4 ]];
   
# Query Points
query = [ 3, 2 ];
   
# Check if its inside
if (isInside(points, query)) :
    print("YES");
     
else :
    print("NO");
     
  # This code is contributed by phasing17.


C#




using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
    // Function To Check Clockwise Orientation
    static bool cw(int[] a, int[] b, int[] c)
    {
        int p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1])
                + c[0] * (a[1] - b[1]);
 
        return p < 0;
    }
 
    // Function To Check Counter Clockwise Orientation
    static bool ccw(int[] a, int[] b, int[] c)
    {
        int p = a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1])
                + c[0] * (a[1] - b[1]);
 
        return p > 0;
    }
 
    // Graham Scan Algorithm To Find Convex Hull From Given
    // Points
    static int[][] convexHull(int[][] v)
    {
        // Sort The Points
        Array.Sort(v, (a, b) => a[0] - b[0]);
 
        int n = v.Length;
        if (n <= 3) {
            return v;
        }
 
        // Set Starting And Ending Points As Left Bottom And
        // Top Right
        int[] p1 = v[0];
        int[] p2 = v[n - 1];
 
        // Vector To Store Points In Upper Half And Lower
        // Half
        List<int[]> up = new List<int[]>();
        List<int[]> down = new List<int[]>();
 
        // Insert Starting/Ending Points
        up.Add(p1);
        down.Add(p1);
 
        // Iterate Over Points
        for (int i = 1; i < n; i++) {
            if (i == n - 1 || !ccw(p1, v[i], p2)) {
                while (up.Count > 1
                       && ccw(up[up.Count - 2],
                              up[up.Count - 1], v[i])) {
                    // Exclude This Point If We Can Form
                    // Better
                    up.RemoveAt(up.Count - 1);
                }
 
                up.Add(v[i]);
            }
 
            if (i == n - 1 || !cw(p1, v[i], p2)) {
                while (down.Count > 1
                       && cw(down[down.Count - 2],
                             down[down.Count - 1], v[i])) {
                    // Exclude This Point If We Can Form
                    // Better
                    down.RemoveAt(down.Count - 1);
                }
 
                down.Add(v[i]);
            }
        }
 
        // Combine Upper And Lower Half
        for (int i = down.Count - 2; i >= 0; i--) {
            up.Add(down[i]);
        }
 
        // Remove Duplicate Points
        up = new List<int[]>(new HashSet<int[]>(up));
 
        // Return The Points On Convex Hull
        return up.ToArray();
    }
 
    // Function To Find If Point Lies Inside A
    //  convex polygon
    static bool isInside(int[][] points, int[] query)
    {
        // Include the query point in the
        // polygon points
        int[][] points1 = new int[points.Length + 1][];
        for (int i = 0; i < points.Length; i++)
            points1[i] = points[i];
        points1[points.Length] = query;
 
        // Form a convex hull from the points
        points = convexHull(points);
 
        // Iterate over the points
        // of convex hull
        foreach(var x in points)
        {
 
            // If the query point lies
            // on the convex hull
            // then it wasn't inside
            if (Enumerable.SequenceEqual(x, query))
                return false;
        }
 
        // Otherwise it was Inside
        return true;
    }
 
    // Driver Code
 
    public static void Main(string[] args)
    {
        // Points of the polygon
        // given in any order
        int n = 7;
        int[][] points
            = { new int[] { 1, 1 }, new int[] { 2, 1 },
                new int[] { 3, 1 }, new int[] { 4, 1 },
                new int[] { 4, 2 }, new int[] { 4, 3 },
                new int[] { 4, 4 } };
 
        // Query Points
        int[] query = { 3, 2 };
 
        // Check if its inside
        if (isInside(points, query)) {
            Console.WriteLine("YES");
        }
        else {
            Console.WriteLine("NO");
        }
    }
}


Javascript




// JS program for the above approach
 
   
// Sorting Function to sort points
function cmp(a, b)
{
   
    if (a[0] == b[0])
        return a[1] < b[1];
    return a[0] < b[0];
}
   
// Function To Check Clockwise
// Orientation
function cw(a, b, c)
{
   
    let p = a[0] * (b[1] - c[1])
            + b[0] * (c[1] - a[1])
            + c[0] * (a[1] - b[1]);
   
    return p < 0;
}
   
// Function To Check Counter
// Clockwise Orientation
function ccw(a, b, c)
{
   
    let p = a[0] * (b[1] - c[1])
            + b[0] * (c[1] - a[1])
            + c[0] * (a[1] - b[1]);
   
    return p > 0;
}
   
// Graham Scan algorithm to find Convex
// Hull from given points
function convexHull(v)
{
    // Sort the points
    v.sort(cmp);
     
   
    let n = v.length;
    if (n <= 3)
        return v;
   
    // Set starting and ending points as
    // left bottom and top right
    let p1 = v[0];
    let p2 = v[n - 1];
   
    // Vector to store points in
    // upper half and lower half
    let  up = [], down = [];
   
    // Insert StartingEnding Points
    up.push(p1);
    down.push(p1);
   
    // Iterate over points
    for (var i = 1; i < n; i++) {
   
        if (i == n - 1 || !ccw(p1, v[i], p2)) {
   
            while (up.length > 1
                   && ccw(up[up.length - 2],
                          up[up.length - 1],
                          v[i])) {
   
                // Exclude this point
                // if we can form better
   
                up.pop();
            }
   
            up.push(v[i]);
        }
   
        if (i == n - 1 || !cw(p1, v[i], p2)) {
   
            while (down.length > 1
                   && cw(down[down.length - 2],
                         down[down.length - 1],
                         v[i])) {
   
                // Exclude this point
                // if we can form better
                down.pop();
            }
            down.push(v[i]);
        }
    }
   
    // Combine upper and  lower half
    for (var i = down.length - 2;
         i > 0; i--)
        up.push(down[i]);
   
    // Remove duplicate points
    up = new Set(up)
    up = Array.from(up)
   
    // Return the points on Convex Hull
    return up;
}
   
// Function to find if point lies inside
// a convex polygon
function isInside( points, query)
{
    // Include the query point in the
    // polygon points
    points.push(query);
   
    // Form a convex hull from the points
    points = convexHull(points);
   
    // Iterate over the points
    // of convex hull
    for (let x of points) {
   
        // If the query point lies
        // on the convex hull
        // then it wasn't inside
        if (x == query)
            return false;
    }
   
    // Otherwise it was Inside
    return true;
}
   
// Driver Code
 
   
    // Points of the polygon
    // given in any order
let n = 7;
let  points = [[1, 1 ], [2, 1 ], [ 3, 1 ],
               [ 4, 1 ], [ 4, 2 ], [4, 3 ],
               [ 4, 4 ]];
   
    // Query Points
    let query = [ 3, 2 ];
   
    // Check if its inside
    if (isInside(points, query)) {
        console.log("YES");
    }
    else {
       console.log("NO");
    }
  


Output:

YES

Time Complexity: O(N * log(N)) Auxiliary Space: O(N)



Previous Article
Next Article

Similar Reads

How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check if given polygon is a convex polygon or not
Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No". In a convex polygon, all interior angles are less than or equal to 18
9 min read
Dynamic Convex hull | Adding Points to an Existing Convex Hull
Given a convex hull, we need to add a given number of points to the convex hull and print the convex hull after every point addition. The points should be in anti-clockwise order after addition of every point. Examples: Input : Convex Hull : (0, 0), (3, -1), (4, 5), (-1, 4) Point to add : (100, 100) Output : New convex hull : (-1, 4) (0, 0) (3, -1)
15 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Count points from an array that lies inside a semi-circle
Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the number of points from the array that lies inside or
7 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Check whether two convex regular polygon have same center or not
Given two positive integers N and M which denotes the sides of the convex regular polygon where N &lt; M, the task is to check whether polygons have the same center or not if N-sided polygon was inscribed in an M-sided polygon.Center of Polygon: Point inside a polygon which is equidistant from each vertex of the polygon. Examples: Input: N = 9, M =
3 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Check if given point lies in range of any of the given towers
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not. Examples: Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15,
7 min read
Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
Given a convex polygon with n+2 sides. The task is to calculate the number of ways in which triangles can be formed by connecting vertices with non-crossing line segments.Examples: Input: n = 1 Output: 1 It is already a triangle so it can only be formed in 1 way.Input: n = 2 Output: 2 It can be cut into 2 triangles by using either pair of opposite
5 min read
Find number of diagonals in n sided convex polygon
Given n &gt; 3, find number of diagonals in n sided convex polygon.According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal. Examples :  Input : 5Output : 5Explanation: Five possible diagonals are :
3 min read
Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
Check whether the point (x, y) lies on a given line
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line. Examples: Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line's equation Input: m = 5, c = 2, x = 2, y = 5 Output: No A
4 min read
Find the point on X-axis from given N points having least Sum of Distances from all other points
Given an array arr[] consisting of N integers, denoting N points lying on the X-axis, the task is to find the point which has the least sum of distances from all other points. Example: Input: arr[] = {4, 1, 5, 10, 2} Output: (4, 0) Explanation: Distance of 4 from rest of the elements = |4 - 1| + |4 - 5| + |4 - 10| + |4 - 2| = 12 Distance of 1 from
11 min read
Perimeter of Convex hull for a given set of points
Given n 2-D points points[], the task is to find the perimeter of the convex hull for the set of points. A convex hull for a set of points is the smallest convex polygon that contains all the points. Examples: Input: points[] = {{0, 3}, {2, 2}, {1, 1}, {2, 1}, {3, 0}, {0, 0}, {3, 3}} Output: 12 Input: points[] = {{0, 2}, {2, 1}, {3, 1}, {3, 7}} Out
10 min read
Deleting points from Convex Hull
Given a fixed set of points. We need to find convex hull of given set. We also need to find convex hull when a point is removed from the set. Example: Initial Set of Points: (-2, 8) (-1, 2) (0, 1) (1, 0) (-3, 0) (-1, -9) (2, -6) (3, 0) (5, 3) (2, 5) Initial convex hull:- (-2, 8) (-3, 0) (-1, -9) (2, -6) (5, 3)Point to remove from the set : (-2, 8)F
15+ min read
Count of integral coordinates that lies inside a Square
Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1 = 1, y1 = 1, x2 = 4, x3 = 4 Output: 4 Approach: T
4 min read
Polygon with maximum sides that can be inscribed in an N-sided regular polygon
Given a regular polygon of N sides, the task is to find the maximum sided polygon that can be inscribed inside the given polygon by joining non-adjacent vertices. Print -1, if no such polygon exist. Examples: Input: N = 8Output: 4Explanation:At most a 4 sided polygon can be inscribed inside the given 8-sided polygon as shown below: Input: N = 3Outp
3 min read
Check if a point is inside, outside or on a Hyperbola
Given a hyperbola centered at (h, k), with semi-major axis a, semi-minor axis b, both aligned with the Cartesian plane, the task is to determine if the point (x, y) lies within the area bounded by the hyperbola or not. Examples: Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: Inside Input: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output:
5 min read
Check if a point is inside, outside or on the ellipse
Given an ellipse centered at (h, k), with semi-major axis a, and semi-minor axis b, both aligned with the Cartesian plane. The task is to determine if the point (x, y) is within the area bounded by the ellipse. Examples:  Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: InsideInput: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output: Outside
10 min read
Check if a point is inside, outside or on the parabola
Given a parabola with vertex (h, k), and [Tex]a     [/Tex], the distance between focus and vertex. The task is to determine if the point (x, y) is inside, outside or on the parabola.Examples:   Input: h = 100, k = 500, x = 20, y = 10, a = 4 Output: OutsideInput: h = 0, k = 0, x = 2, y = 1, a = 4Output: Inside     Approach: It is very simple, we hav
5 min read
Minimum area of a Polygon with three points given
Given three points of a regular polygon(n &gt; 3), find the minimum area of a regular polygon (all sides same) possible with the points given.Examples: Input : 0.00 0.00 1.00 1.00 0.00 1.00 Output : 1.00 By taking point (1.00, 0.00) square is formed of side 1.0 so area = 1.00 . One thing to note in question before we proceed is that the number of s
14 min read
Check if any point exists in a plane whose Manhattan distance is at most K from N given points
Given two arrays A[] and B[] consisting of X and Y coordinates of N distinct points in a plane, and a positive integer K, the task is to check if there exists any point P in the plane such that the Manhattan distance between the point and all the given points is at most K. If there exists any such point P, then print "Yes". Otherwise, print "No". E
7 min read
three90RightbarBannerImg