Find if the point P lies Inside or Not

Last Updated : 16 Aug, 2026

Given the coordinates of the three vertices of a triangleĀ (x1, y1),Ā (x2, y2), andĀ (x3, y3), along with a pointĀ P(x, y).Ā Ā FindĀ if the point P lies inside the triangle or on its boundary.

Examples:

Input: x1 = 0, y1 = 0, x2 = 20, y2 = 0, x3 = 10, y3 = 30, x = 10, y = 15.
Output: true
Explanation: The point (x, y) lies within the Triangle.

1

Input: x1 = 0, y1 = 0, x2 = 20, y2 = 20, x3 = 20, y3 = 0, x = 30, y = 0.
Output: false
Explanation: The point (x, y) doesn't lie within the Triangle.

2
Try It Yourself
redirect icon

[Naive Approach] Using Area of Triangle - O(1) Time and O(1) Space

The idea is to calculate the area of the given triangle and compare it with the sum of the areas of the three triangles formed using the given point.

If both areas are equal, the point lies inside or on the boundary of the triangle.

Working of Approach:

  • Compute the area of triangle ABC.
  • Compute the areas of triangles PAB, PBC, and PCA.
  • Add the three smaller triangle areas.
  • If the sum equals the area of ABC, return true.
  • Otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to calculate twice the area of a triangle.
int area(int x1, int y1, int x2, int y2, int x3, int y3)
{
    return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
}

bool isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{

    // Calculate area of the original triangle.
    int A = area(x1, y1, x2, y2, x3, y3);

    // Calculate areas of the three sub-triangles.
    int A1 = area(x, y, x2, y2, x3, y3);
    int A2 = area(x1, y1, x, y, x3, y3);
    int A3 = area(x1, y1, x2, y2, x, y);

    // Point lies inside or on the boundary if areas match.
    return (A == A1 + A2 + A3);
}

int main()
{
    int x1 = 0, y1 = 0;
    int x2 = 20, y2 = 0;
    int x3 = 10, y3 = 30;
    int x = 10, y = 15;

    cout << (isInsideTri(x1, y1, x2, y2, x3, y3, x, y) ? "true" : "false");

    return 0;
}
Java
import java.util.Arrays;

// Function to calculate twice the area of a triangle.
public class GFG {
    public static int 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));
    }

    public static boolean isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) {
        // Calculate area of the original triangle.
        int A = area(x1, y1, x2, y2, x3, y3);

        // Calculate areas of the three sub-triangles.
        int A1 = area(x, y, x2, y2, x3, y3);
        int A2 = area(x1, y1, x, y, x3, y3);
        int A3 = area(x1, y1, x2, y2, x, y);

        // Point lies inside or on the boundary if areas match.
        return (A == A1 + A2 + A3);
    }

    public static void main(String[] args) {
        int x1 = 0, y1 = 0;
        int x2 = 20, y2 = 0;
        int x3 = 10, y3 = 30;
        int x = 10, y = 15;

        System.out.println(isInsideTri(x1, y1, x2, y2, x3, y3, x, y) ? "true" : "false");
    }
}
Python
from math import fabs

# Function to calculate twice the area of a triangle.


def area(x1, y1, x2, y2, x3, y3):
    return fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))


def isInsideTri(x1, y1, x2, y2, x3, y3, x, y):

    # Calculate area of the original triangle.
    A = area(x1, y1, x2, y2, x3, y3)

    # Calculate areas of the three sub-triangles.
    A1 = area(x, y, x2, y2, x3, y3)
    A2 = area(x1, y1, x, y, x3, y3)
    A3 = area(x1, y1, x2, y2, x, y)

    # Point lies inside or on the boundary if areas match.
    return (A == A1 + A2 + A3)


if __name__ == '__main__':
    x1 = 0
    y1 = 0
    x2 = 20
    y2 = 0
    x3 = 10
    y3 = 30
    x = 10
    y = 15

    print('true' if isInsideTri(x1, y1, x2, y2, x3, y3, x, y) else 'false')
C#
using System;

// Function to calculate twice the area of a triangle.
public class GFG {
    public static int 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));
    }

    public static bool isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) {
        // Calculate area of the original triangle.
        int A = area(x1, y1, x2, y2, x3, y3);

        // Calculate areas of the three sub-triangles.
        int A1 = area(x, y, x2, y2, x3, y3);
        int A2 = area(x1, y1, x, y, x3, y3);
        int A3 = area(x1, y1, x2, y2, x, y);

        // Point lies inside or on the boundary if areas match.
        return (A == A1 + A2 + A3);
    }

    public static void Main() {
        int x1 = 0, y1 = 0;
        int x2 = 20, y2 = 0;
        int x3 = 10, y3 = 30;
        int x = 10, y = 15;

        Console.WriteLine(isInsideTri(x1, y1, x2, y2, x3, y3, x, y)? "true" : "false");
    }
}
JavaScript
// Function to calculate twice the area of a triangle.
function area(x1, y1, x2, y2, x3, y3)
{
    return Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1)
                    + x3 * (y1 - y2));
}

function isInsideTri(x1, y1, x2, y2, x3, y3, x, y)
{

    // Calculate area of the original triangle.
    let A = area(x1, y1, x2, y2, x3, y3);

    // Calculate areas of the three sub-triangles.
    let A1 = area(x, y, x2, y2, x3, y3);
    let A2 = area(x1, y1, x, y, x3, y3);
    let A3 = area(x1, y1, x2, y2, x, y);

    // Point lies inside or on the boundary if areas match.
    return (A == A1 + A2 + A3);
}

// Driver Code
let x1 = 0, y1 = 0;
let x2 = 20, y2 = 0;
let x3 = 10, y3 = 30;
let x = 10, y = 15;

console.log(isInsideTri(x1, y1, x2, y2, x3, y3, x, y)
                ? "true"
                : "false");

Output
true

[Expected Approach] Using Cross Product - O(1) Time and O(1) Space

The idea is to use the cross product to determine the side of each triangle edge on which the given point lies.

If the point lies on the same side of all three edges (or on an edge), it is inside or on the boundary of the triangle.

Working of Approach:

  • Compute the cross product for the point with each triangle edge.
  • Check the sign of all three cross products.
  • If all are positive or all are negative (zeros are allowed), the point is inside.
  • If both positive and negative values are present, the point is outside.
  • Return the corresponding result.

Let us understand with an example:
Input: x1 = 0, y1 = 0, x2 = 20, y2 = 0, x3 = 10, y3 = 30, x = 10, y = 15.

  • Compute the cross product of point P(10, 15) with each triangle edge: AB, BC, and CA.
  • The obtained cross products are 300, 150, and 150, respectively.
  • Since all three cross products are positive, the point lies on the same side of every edge.
  • Therefore, the point lies inside the triangle.
  • Hence, the output is true.
1
C++
#include <bits/stdc++.h>
using namespace std;

// Cross product of vectors AB and AP to determine
// which side of edge AB the point P lies on.
int crossVal(int ax, int ay, int bx, int by, int px, int py)
{
    return (bx - ax) * (py - ay) - (by - ay) * (px - ax);
}

bool isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y)
{

    // Compute signed area (cross product) for P w.r.t each edge
    int c1 = crossVal(x1, y1, x2, y2, x, y);
    int c2 = crossVal(x2, y2, x3, y3, x, y);
    int c3 = crossVal(x3, y3, x1, y1, x, y);

    // If P is on the same side of all edges (all +ve, all -ve,
    // or zero), it lies inside or on the boundary of the triangle
    bool hasPos = (c1 > 0) || (c2 > 0) || (c3 > 0);
    bool hasNeg = (c1 < 0) || (c2 < 0) || (c3 < 0);

    return !(hasPos && hasNeg);
}

int main()
{
    int x1 = 0, y1 = 0;
    int x2 = 20, y2 = 0;
    int x3 = 10, y3 = 30;
    int x = 10, y = 15;

    cout << (isInsideTri(x1, y1, x2, y2, x3, y3, x, y) ? "true" : "false");

    return 0;
}
Java
public class GFG {
    // Cross product of vectors AB and AP to determine
    // which side of edge AB the point P lies on.
    public static int crossVal(int ax, int ay, int bx, int by, int px, int py) {
        return (bx - ax) * (py - ay) - (by - ay) * (px - ax);
    }

    public static boolean isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) {
        // Compute signed area (cross product) for P w.r.t each edge
        int c1 = crossVal(x1, y1, x2, y2, x, y);
        int c2 = crossVal(x2, y2, x3, y3, x, y);
        int c3 = crossVal(x3, y3, x1, y1, x, y);

        // If P is on the same side of all edges (all +ve, all -ve,
        // or zero), it lies inside or on the boundary of the triangle
        boolean hasPos = (c1 > 0) || (c2 > 0) || (c3 > 0);
        boolean hasNeg = (c1 < 0) || (c2 < 0) || (c3 < 0);

        return!(hasPos && hasNeg);
    }

    public static void main(String[] args) {
        int x1 = 0, y1 = 0;
        int x2 = 20, y2 = 0;
        int x3 = 10, y3 = 30;
        int x = 10, y = 15;

        System.out.println(isInsideTri(x1, y1, x2, y2, x3, y3, x, y)? "true" : "false");
    }
}
Python
def crossVal(ax, ay, bx, by, px, py):
    # Cross product of vectors AB and AP to determine
    # which side of edge AB the point P lies on.
    return (bx - ax) * (py - ay) - (by - ay) * (px - ax)

def isInsideTri(x1, y1, x2, y2, x3, y3, x, y):
    # Compute signed area (cross product) for P w.r.t each edge
    c1 = crossVal(x1, y1, x2, y2, x, y)
    c2 = crossVal(x2, y2, x3, y3, x, y)
    c3 = crossVal(x3, y3, x1, y1, x, y)

    # If P is on the same side of all edges (all +ve, all -ve,
    # or zero), it lies inside or on the boundary of the triangle
    hasPos = (c1 > 0) or (c2 > 0) or (c3 > 0)
    hasNeg = (c1 < 0) or (c2 < 0) or (c3 < 0)

    return not (hasPos and hasNeg)

if __name__ == "__main__":
    x1, y1 = 0, 0
    x2, y2 = 20, 0
    x3, y3 = 10, 30
    x, y = 10, 15

    print("true" if isInsideTri(x1, y1, x2, y2, x3, y3, x, y) else "false")
C#
using System;

public class GFG {
    // Cross product of vectors AB and AP to determine
    // which side of edge AB the point P lies on.
    public static int crossVal(int ax, int ay, int bx, int by, int px, int py) {
        return (bx - ax) * (py - ay) - (by - ay) * (px - ax);
    }

    public static bool isInsideTri(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) {
        // Compute signed area (cross product) for P w.r.t each edge
        int c1 = crossVal(x1, y1, x2, y2, x, y);
        int c2 = crossVal(x2, y2, x3, y3, x, y);
        int c3 = crossVal(x3, y3, x1, y1, x, y);

        // If P is on the same side of all edges (all +ve, all -ve,
        // or zero), it lies inside or on the boundary of the triangle
        bool hasPos = (c1 > 0) || (c2 > 0) || (c3 > 0);
        bool hasNeg = (c1 < 0) || (c2 < 0) || (c3 < 0);

        return!(hasPos && hasNeg);
    }

    public static void Main() {
        int x1 = 0, y1 = 0;
        int x2 = 20, y2 = 0;
        int x3 = 10, y3 = 30;
        int x = 10, y = 15;

        Console.WriteLine(isInsideTri(x1, y1, x2, y2, x3, y3, x, y)? "true" : "false");
    }
}
JavaScript
function crossVal(ax, ay, bx, by, px, py)
{
    // Cross product of vectors AB and AP to determine
    // which side of edge AB the point P lies on.
    return (bx - ax) * (py - ay) - (by - ay) * (px - ax);
}

function isInsideTri(x1, y1, x2, y2, x3, y3, x, y)
{
    // Compute signed area (cross product) for P w.r.t each
    // edge
    let c1 = crossVal(x1, y1, x2, y2, x, y);
    let c2 = crossVal(x2, y2, x3, y3, x, y);
    let c3 = crossVal(x3, y3, x1, y1, x, y);

    // If P is on the same side of all edges (all +ve, all
    // -ve, or zero), it lies inside or on the boundary of
    // the triangle
    let hasPos = (c1 > 0) || (c2 > 0) || (c3 > 0);
    let hasNeg = (c1 < 0) || (c2 < 0) || (c3 < 0);

    return !(hasPos && hasNeg);
}

// Driver Code
let x1 = 0, y1 = 0;
let x2 = 20, y2 = 0;
let x3 = 10, y3 = 30;
let x = 10, y = 15;

console.log(isInsideTri(x1, y1, x2, y2, x3, y3, x, y)
                ? "true"
                : "false");

Output
true
Comment