The Wayback Machine - https://web.archive.org/web/20240926201017/https://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/
Open In App

Check whether a given point lies inside a triangle or not

Last Updated : 18 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: Inside
Explanation:
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: Outside
Explanation:
B(10,30)
/ \
/ \
/ \
/ \ P
/ \
A(0,0) ----------- C(20,0)

 

Solution: 
Let the coordinates of the three corners be (x1, y1), (x2, y2), and (x3, y3). And coordinates of the given point P be (x, y)

  1. Calculate the area of the given triangle, i.e., the area of the triangle ABC in the above diagram. 
    Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 
  2. Calculate the area of the triangle PAB. We can use the same formula for this. Let this area be A1. 
  3. Calculate the area of the triangle PBC. Let this area be A2. 
  4. Calculate the area of the triangle PAC. Let this area be A3. 
  5. If P lies inside the triangle, then A1 + A2 + A3 must be equal to A. 

C++




#include <bits/stdc++.h>
using namespace std;
  
/* A utility function to calculate area of triangle formed by (x1, y1),
   (x2, y2) and (x3, y3) */
float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
  
/* A function to check whether point P(x, y) lies inside the triangle formed
   by A(x1, y1), B(x2, y2) and C(x3, y3) */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{  
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);
  
   /* Calculate area of triangle PBC */ 
   float A1 = area (x, y, x2, y2, x3, y3);
  
   /* Calculate area of triangle PAC */ 
   float A2 = area (x1, y1, x, y, x3, y3);
  
   /* Calculate area of triangle PAB */  
   float A3 = area (x1, y1, x2, y2, x, y);
    
   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}
  
/* Driver program to test above function */
int main()
{
   /* Let us check whether the point P(10, 15) lies inside the triangle
      formed by A(0, 0), B(20, 0) and C(10, 30) */
   if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
     cout <<"Inside";
   else
     cout <<"Not Inside";
  
   return 0;
}
 
// this code is contributed by shivanisinghss2110


C




#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
/* A utility function to calculate area of triangle formed by (x1, y1),
   (x2, y2) and (x3, y3) */
float area(int x1, int y1, int x2, int y2, int x3, int y3)
{
   return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
  
/* A function to check whether point P(x, y) lies inside the triangle formed
   by A(x1, y1), B(x2, y2) and C(x3, y3) */
bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{  
   /* Calculate area of triangle ABC */
   float A = area (x1, y1, x2, y2, x3, y3);
  
   /* Calculate area of triangle PBC */ 
   float A1 = area (x, y, x2, y2, x3, y3);
  
   /* Calculate area of triangle PAC */ 
   float A2 = area (x1, y1, x, y, x3, y3);
  
   /* Calculate area of triangle PAB */  
   float A3 = area (x1, y1, x2, y2, x, y);
    
   /* Check if sum of A1, A2 and A3 is same as A */
   return (A == A1 + A2 + A3);
}
  
/* Driver program to test above function */
int main()
{
   /* Let us check whether the point P(10, 15) lies inside the triangle
      formed by A(0, 0), B(20, 0) and C(10, 30) */
   if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
     printf ("Inside");
   else
     printf ("Not Inside");
  
   return 0;
}


Java




// JAVA Code for Check whether a given point
// lies inside a triangle or not
import java.util.*;
 
class GFG {
     
    /* A utility function to calculate area of triangle
       formed by (x1, y1) (x2, y2) and (x3, y3) */
    static double area(int x1, int y1, int x2, int y2,
                                        int x3, int y3)
    {
       return Math.abs((x1*(y2-y3) + x2*(y3-y1)+
                                    x3*(y1-y2))/2.0);
    }
      
    /* A function to check whether point P(x, y) lies
       inside the triangle formed by A(x1, y1),
       B(x2, y2) and C(x3, y3) */
    static boolean isInside(int x1, int y1, int x2,
                int y2, int x3, int y3, int x, int y)
    {  
       /* Calculate area of triangle ABC */
        double A = area (x1, y1, x2, y2, x3, y3);
      
       /* Calculate area of triangle PBC */ 
        double A1 = area (x, y, x2, y2, x3, y3);
      
       /* Calculate area of triangle PAC */ 
        double A2 = area (x1, y1, x, y, x3, y3);
      
       /* Calculate area of triangle PAB */  
        double A3 = area (x1, y1, x2, y2, x, y);
        
       /* Check if sum of A1, A2 and A3 is same as A */
        return (A == A1 + A2 + A3);
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        /* Let us check whether the point P(10, 15)
           lies inside the triangle formed by
           A(0, 0), B(20, 0) and C(10, 30) */
       if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
           System.out.println("Inside");
       else
           System.out.println("Not Inside");
             
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python




# A utility function to calculate area
# of triangle formed by (x1, y1),
# (x2, y2) and (x3, y3)
 
def area(x1, y1, x2, y2, x3, y3):
 
    return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
                + x3 * (y1 - y2)) / 2.0)
 
 
# A function to check whether point P(x, y)
# lies inside the triangle formed by
# A(x1, y1), B(x2, y2) and C(x3, y3)
def isInside(x1, y1, x2, y2, x3, y3, x, y):
 
    # Calculate area of triangle ABC
    A = area (x1, y1, x2, y2, x3, y3)
 
    # Calculate area of triangle PBC
    A1 = area (x, y, x2, y2, x3, y3)
     
    # Calculate area of triangle PAC
    A2 = area (x1, y1, x, y, x3, y3)
     
    # Calculate area of triangle PAB
    A3 = area (x1, y1, x2, y2, x, y)
     
    # Check if sum of A1, A2 and A3
    # is same as A
    if(A == A1 + A2 + A3):
        return True
    else:
        return False
 
# Driver program to test above function
# Let us check whether the point P(10, 15)
# lies inside the triangle formed by
# A(0, 0), B(20, 0) and C(10, 30)
if (isInside(0, 0, 20, 0, 10, 30, 10, 15)):
    print('Inside')
else:
    print('Not Inside')
 
# This code is contributed by Danish Raza


C#




// C# Code to Check whether a given point
// lies inside a triangle or not
using System;
 
class GFG {
 
    /* A utility function to calculate area of triangle
    formed by (x1, y1) (x2, y2) and (x3, y3) */
    static double area(int x1, int y1, int x2,
                       int y2, int x3, int y3)
    {
        return Math.Abs((x1 * (y2 - y3) +
                         x2 * (y3 - y1) +
                         x3 * (y1 - y2)) / 2.0);
    }
 
    /* A function to check whether point P(x, y) lies
    inside the triangle formed by A(x1, y1),
    B(x2, y2) and C(x3, y3) */
    static bool isInside(int x1, int y1, int x2,
                         int y2, int x3, int y3,
                         int x, int y)
    {
        /* Calculate area of triangle ABC */
        double A = area(x1, y1, x2, y2, x3, y3);
 
        /* Calculate area of triangle PBC */
        double A1 = area(x, y, x2, y2, x3, y3);
 
        /* Calculate area of triangle PAC */
        double A2 = area(x1, y1, x, y, x3, y3);
 
        /* Calculate area of triangle PAB */
        double A3 = area(x1, y1, x2, y2, x, y);
 
        /* Check if sum of A1, A2 and A3 is same as A */
        return (A == A1 + A2 + A3);
    }
 
/* Driver program to test above function */
public static void Main()
{
    /* Let us check whether the point P(10, 15)
    lies inside the triangle formed by
    A(0, 0), B(20, 0) and C(10, 30) */
    if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
        Console.WriteLine("Inside");
    else
        Console.WriteLine("Not Inside");
}
}
 
// This code is contributed by vt_m.


Javascript




<script>
 
/* A utility function to calculate area of triangle formed by (x1, y1),
(x2, y2) and (x3, y3) */
function area(x1, y1, x2, y2, x3, y3)
{
return Math.abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
}
 
/* A function to check whether point P(x, y) lies inside the triangle formed
by A(x1, y1), B(x2, y2) and C(x3, y3) */
function isInside(x1, y1, x2, y2, x3, y3, x, y)
{
/* Calculate area of triangle ABC */
let A = area (x1, y1, x2, y2, x3, y3);
 
/* Calculate area of triangle PBC */
let A1 = area (x, y, x2, y2, x3, y3);
 
/* Calculate area of triangle PAC */
let A2 = area (x1, y1, x, y, x3, y3);
 
/* Calculate area of triangle PAB */   
let A3 = area (x1, y1, x2, y2, x, y);
     
/* Check if sum of A1, A2 and A3 is same as A */
return (A == A1 + A2 + A3);
}
 
/* Driver program to test above function */
  
/* Let us check whether the point P(10, 15) lies inside the triangle
    formed by A(0, 0), B(20, 0) and C(10, 30) */
if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
    document.write("Inside");
else
    document.write("Not Inside");
 
// This code is contributed by Mayank Tyagi
 
</script>


PHP




<?php
 
/* A utility function to calculate
  area of triangle formed by (x1, y1),
  (x2, y2) and (x3, y3) */
function area($x1, $y1, $x2,
              $y2, $x3, $y3)
{
    return abs(($x1 * ($y2 - $y3) +
                $x2 * ($y3 - $y1) + 
                $x3 * ($y1 - $y2)) / 2.0);
}
 
/* A function to check whether
   P(x, y) lies inside the
   triangle formed by A(x1, y1),
   B(x2, y2) and C(x3, y3) */
function isInside($x1, $y1, $x2, $y2,
                  $x3, $y3, $x, $y)
{
     
    /* Calculate area of triangle ABC */
    $A = area ($x1, $y1, $x2, $y2, $x3, $y3);
     
    /* Calculate area of triangle PBC */
    $A1 = area ($x, $y, $x2, $y2, $x3, $y3);
     
    /* Calculate area of triangle PAC */
    $A2 = area ($x1, $y1, $x, $y, $x3, $y3);
     
    /* Calculate area of triangle PAB */
    $A3 = area ($x1, $y1, $x2, $y2, $x, $y);
     
    /* Check if sum of A1, A2
    and A3 is same as A */
    return ($A == $A1 + $A2 + $A3);
}
 
    // Driver Code
    /* Let us check whether the
       P(10, 15) lies inside the
       triangle formed by A(0, 0),
       B(20, 0) and C(10, 30) */
    if (isInside(0, 0, 20, 0, 10, 30, 10, 15))
        echo "Inside";
    else
        echo "Not Inside";
 
 
// This code is contributed by anuj_67.
?>


Output

Inside

Time Complexity: O(1)
Auxiliary Space: O(1)

Exercise: Given coordinates of four corners of a rectangle, and a point P. Write a function to check whether P lies inside the given rectangle or not.

Another Approach – Using Barycentric Coordinate Method: Below is the algorithm to check if a point P lies inside a triangle ABC using the Barycentric Coordinate Method:

  • Define a function “isInsideTriangle” that takes four input parameters: A, B, C, and P.
  • Calculate the barycentric coordinates of point P with respect to the triangle ABC. To do this, we first need to calculate the area of the triangle ABC. We can use the cross product to find the area of the triangle ABC as:

Area(ABC) = 0.5 * ||AB x AC||, where ||AB x AC|| is the magnitude of the cross product of vectors AB and AC.

  • Then, we can calculate the barycentric coordinates of point P as:
    1. a = 0.5 * ||PB x PC|| / Area(ABC)
    2. b = 0.5 * ||PC x PA|| / Area(ABC)
    3. c = 0.5 * ||PA x PB|| / Area(ABC), where PB, PC, and PA are vectors from point P to vertices B, C, and A, respectively.
  • If all three barycentric coordinates are non-negative, then the point P lies inside the triangle ABC. Return “Inside”. Otherwise, point P lies outside the triangle ABC. Return “Outside”.

Below is the implementation of the above approach:

C++




// c++ code addition
 
#include<iostream>
#include<vector>
using namespace std;
 
// Function to check if the point is inside
// the triangle or not
string isInsideTriangle(vector<int> A, vector<int> B, vector<int> C, vector<int> P) {
    // Calculate the barycentric coordinates
    // of point P with respect to triangle ABC
    double denominator = ((B[1] - C[1]) * (A[0] - C[0]) + (C[0] - B[0]) * (A[1] - C[1]));
    double a = ((B[1] - C[1]) * (P[0] - C[0]) + (C[0] - B[0]) * (P[1] - C[1])) / denominator;
    double b = ((C[1] - A[1]) * (P[0] - C[0]) + (A[0] - C[0]) * (P[1] - C[1])) / denominator;
    double c = 1 - a - b;
 
    // Check if all barycentric coordinates
    // are non-negative
    if (a >= 0 && b >= 0 && c >= 0) {
        return "Inside";
    } else {
        return "Outside";
    }
}
 
// Driver Code
int main() {
    vector<int> A = {0, 0};
    vector<int> B = {10, 30};
    vector<int> C = {20, 0};
    vector<int> P = {10, 15};
 
    // Call the isInsideTriangle function with
    // the given inputs
    string result = isInsideTriangle(A, B, C, P);
 
    // Print the result
    cout << result << endl;
    return 0;
}
 
// The code is contributed by Arushi Goel.


Java




import java.awt.Point;
 
class Main
{
 
  // Function to check if the point is inside
  // the triangle or not
  public static String isInsideTriangle(Point A, Point B, Point C, Point P)
  {
 
    // Calculate the barycentric coordinates
    // of point P with respect to triangle ABC
    double denominator = ((B.y - C.y) * (A.x - C.x) +
                          (C.x - B.x) * (A.y - C.y));
    double a = ((B.y - C.y) * (P.x - C.x) +
                (C.x - B.x) * (P.y - C.y)) / denominator;
    double b = ((C.y - A.y) * (P.x - C.x) +
                (A.x - C.x) * (P.y - C.y)) / denominator;
    double c = 1 - a - b;
 
    // Check if all barycentric coordinates
    // are non-negative
    if (a >= 0 && b >= 0 && c >= 0) {
      return "Inside";
    } else {
      return "Outside";
    }
  }
 
  public static void main(String[] args) {
    Point A = new Point(0, 0);
    Point B = new Point(10, 30);
    Point C = new Point(20, 0);
    Point P = new Point(10, 15);
 
    // Call the isInsideTriangle function with
    // the given inputs
    String result = isInsideTriangle(A, B, C, P);
 
    // Print the result
    System.out.println(result);
  }
}


Python3




# Python program for the above approach
 
# Function to check if the point is inside
# the triangle or not
def isInsideTriangle(A, B, C, P):
 
    # Calculate the barycentric coordinates
    # of point P with respect to triangle ABC
    denominator = ((B[1] - C[1]) * (A[0] - C[0]) +
                   (C[0] - B[0]) * (A[1] - C[1]))
    a = ((B[1] - C[1]) * (P[0] - C[0]) +
         (C[0] - B[0]) * (P[1] - C[1])) / denominator
    b = ((C[1] - A[1]) * (P[0] - C[0]) +
         (A[0] - C[0]) * (P[1] - C[1])) / denominator
    c = 1 - a - b
 
    # Check if all barycentric coordinates
    # are non-negative
    if a >= 0 and b >= 0 and c >= 0:
        return "Inside"
    else:
        return "Outside"
 
 
# Driver Code
A = (0, 0)
B = (10, 30)
C = (20, 0)
P = (10, 15)
 
# Call the isInsideTriangle function with
# the given inputs
result = isInsideTriangle(A, B, C, P)
 
# Print the result
print(result)


C#




using System;
 
public class PointInsideTriangle
{
    // Function to check if the point is inside
    // the triangle or not
    public static string IsInsideTriangle(int[] A, int[] B, int[] C, int[] P)
    {
        // Calculate the barycentric coordinates
        // of point P with respect to triangle ABC
        double denominator = ((B[1] - C[1]) * (A[0] - C[0]) + (C[0] - B[0]) * (A[1] - C[1]));
        double a = ((B[1] - C[1]) * (P[0] - C[0]) + (C[0] - B[0]) * (P[1] - C[1])) / denominator;
        double b = ((C[1] - A[1]) * (P[0] - C[0]) + (A[0] - C[0]) * (P[1] - C[1])) / denominator;
        double c = 1 - a - b;
 
        // Check if all barycentric coordinates
        // are non-negative
        if (a >= 0 && b >= 0 && c >= 0)
        {
            return "Inside";
        }
        else
        {
            return "Outside";
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] A = { 0, 0 };
        int[] B = { 10, 30 };
        int[] C = { 20, 0 };
        int[] P = { 10, 15 };
 
        // Call the IsInsideTriangle function with
        // the given inputs
        string result = IsInsideTriangle(A, B, C, P);
 
        // Print the result
        Console.WriteLine(result);
    }
}


Javascript




// JavaScript program for the above approach
 
// Function to check if the point is inside
// the triangle or not
function isInsideTriangle(A, B, C, P) {
// Calculate the barycentric coordinates
// of point P with respect to triangle ABC
let denominator = ((B[1] - C[1]) * (A[0] - C[0]) +
               (C[0] - B[0]) * (A[1] - C[1]));
let a = ((B[1] - C[1]) * (P[0] - C[0]) +
     (C[0] - B[0]) * (P[1] - C[1])) / denominator;
let b = ((C[1] - A[1]) * (P[0] - C[0]) +
     (A[0] - C[0]) * (P[1] - C[1])) / denominator;
let c = 1 - a - b;
 
// Check if all barycentric coordinates
// are non-negative
if (a >= 0 && b >= 0 && c >= 0) {
    return "Inside";
} else {
    return "Outside";
}
}
 
// Driver Code
let A = [0, 0];
let B = [10, 30];
let C = [20, 0];
let P = [10, 15];
 
// Call the isInsideTriangle function with
// the given inputs
let result = isInsideTriangle(A, B, C, P);
 
// Print the result
console.log(result);


Output

Inside

Time Complexity: O(1)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

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 whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b < 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 if the given point lies inside given N points of a Convex Polygon
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
15 min read
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 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
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 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
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
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 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 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
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 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
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Triangle with no point inside
Given N points in 2-dimensional space, we need to find three points such that triangle made by choosing these points should not contain any other points inside. All given points will not lie on the same line so solution will always exist. Examples: In above diagram possible triangle with no point inside can be formed by choosing these triplets,[(0,
15+ min read
Check whether triangle is valid or not if sides are given
Given three sides, check whether triangle is valid or not. Examples: Input : a = 7, b = 10, c = 5 Output : Valid Input : a = 1 b = 10 c = 12 Output : Invalid Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. 1.a + b > c 2.a + c > b 3.b + c
4 min read
Check whether the triangle is valid or not if angles are given
Given three integers A, B and C which are the three angles of a possible triangle in degrees, the task is to check whether the triangle is valid or not.Examples: Input: A = 60, B = 40, C = 80 Output: ValidInput: A = 55, B = 45, C = 60 Output: Invalid Approach: A triangle is valid if the sum of the three angles is equal to 180 degrees. Below is the
3 min read
Check whether triangle is valid or not if three points are given
Given coordinates of three points in a plane P1, P2 and P3, the task is to check if the three points form a triangle or notExamples: Input: P1 = (1, 5), P2 = (2, 5), P3 = (4, 6) Output: YesInput: P1 = (1, 1), P2 = (1, 4), P3 = (1, 5) Output: No Approach: The key observation in the problem is three points form a triangle only when they don't lie on
9 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
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
Check whether right angled triangle is valid or not for large sides
Given three integers a, b and c as triplets. Check if it is possible to make right angled triangle or not. Print Yes if possible, else No. 10-18 <= a, b, c <= 1018 Examples: Input: 3 4 5 Output: Yes Explanation: Since 3*3 + 4*4 = 5*5 Hence print "Yes" Input: 8 5 13 Since 8 + 5 < 13 which violates the property of triangle. Hence print "No"
8 min read
Check whether a point exists in circle sector or not.
We have a circle centered at origin (0, 0). As input we are given with starting angle of the circle sector and the size of the circle sector in percentage. Examples: Input : Radius = 8 StartAngle = 0 Percentage = 12 x = 3 y = 4 Output : Point (3, 4) exists in the circle sector Input : Radius = 12 Startangle = 45 Percentage = 25 x = 3 y = 4 Output :
5 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
Check whether the given floating point number is a palindrome
Given a floating-point number N, the task is to check whether it is palindrome or not. Input: N = 123.321 Output: YesInput: N = 122.1 Output: No Approach: First, convert the given floating-point number into a character array.Initialize the low to first index and high to the last index.While low < high: If the character at low is not equal to the
4 min read
Check whether given floating point number is even or odd
Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number but its last digit is divisible by 2. Examples : I
6 min read
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides
Given an array arr[][] of dimension N * 2 representing the co-ordinates of N points and an array Q[] consisting of M integers, the task for every element in the Q[i] is to find the number of points lying inside or on the right-angled isosceles triangle formed on the positive co-ordinate axis with two equal sides of length Q[i]in each query. Example
7 min read
Check if vertex X lies in subgraph of vertex Y for the given Graph
Given an undirected graph and two vertices X and Y, our task is to check whether the vertex X lies in the subgraph of the vertex Y. Examples: Input: X = 2, Y = 3 Output: No Explanation: Subgraph of a vertex Y = 3 is set of all the vertex which lies below Y and are reachable by Y. Here subgraph of 3 contains {6} not 2. Input: X = 6, Y = 1 Output: Ye
10 min read
Practice Tags :