Check whether a given point lies inside a triangle or not - GeeksforGeeks

04 Oct, 2021
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.For example, consider the following program, the function should return true for P(10, 15) and false for P'(30, 15) 
              B(10,30)
                / \
               /   \
              /     \
             /   P   \      P'
            /         \
     A(0,0) ----------- C(20,0) 
Solution: Let the coordinates of three corners be (x1, y1), (x2, y2) and (x3, y3). And coordinates of the given point P be (x, y)1) Calculate area of the given triangle, i.e., area of the triangle ABC in the above diagram. Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 2) Calculate area of the triangle PAB. We can use the same formula for this. Let this area be A1. 3) Calculate area of the triangle PBC. Let this area be A2. 4) Calculate 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++
C
Java
Python
C#
PHP
Javascript
#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
#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 Code for Check whether a given point// lies inside a triangle or notimport 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.
# 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# Code to Check whether a given point// lies inside a triangle or notusing 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.
<?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.?>
<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 formedby 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>
Output: 
 Inside 
Time Complexity: O(1)
Auxiliary Space: O(1)
https://www.youtube.com/watch?v=H9qu9Xptf
-wExercise: 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
 
vt_m
nidhi_biet
mayanktyagi1709
surinderdawra388
shivanisinghss2110
subham348
triangle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Find if two rectangles overlap
Check whether triangle is valid or not if sides are given
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull using Divide and Conquer Algorithm
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples