Java Program to Check Whether a Given Point Lies Inside a Triangle or not

In the previous article, we have seen Java Program to Find Direction of a Point from a Line Segment

In this article we will discuss about how to check whether a given point lies inside a triangle or not using Java programming language.

Java Program to Check Whether a Given Point Lies Inside a Triangle or not

Before jumping into the program directly, let’s first know how can we check whether a given point lies inside a triangle or not.

Explanation:

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

  • Use the point to form three new triangles with the three sides of the main triangle.
  • Find the area of the three new triangles and the main triangle.
  • If the areas of the three triangles add up to the main triangle then the point lies inside the triangle.

Let’s see different ways to check whether a given point lies inside a triangle or not.

Method-1: Java Program to Check whether a given point lies inside a triangle or not By Using Static Value

Approach:

  1. Declare the value for coordinates of the triangle and then the point to check.
  2. Then call the checkPointLoc() user defined method by passing the three coordinates and the point value as parameter.
  3. In this method the sum of areas of the three sub triangles is calculated and then it is compared with the area of the main triangle.
  4. Then print the result.

Program:

import java.awt.Point;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args)
    {       
        //Coordinates of the triangle
        Point a = new Point(0,0);
        Point b = new Point(20,0);
        Point c = new Point(10,30);
        //Coordinates of the Point
        Point p = new Point(10,15);

        // Checks if the point lies in
        if(checkPointLoc(a,b,c,p))
            System.out.println("The point lies inside the triangle");
        else
            System.out.println("The point does not lie inside the triangle");
    }
    
    // Checks if the areas of the three small triangles add upto the primary triangle
    static boolean checkPointLoc(Point a, Point b, Point c, Point p)
    {
        double primaryTriangleAr = AreaOfTriangle(a,b,c);
        double area1 = AreaOfTriangle(a,b,p);
        double area2 = AreaOfTriangle(a,p,c);
        double area3 = AreaOfTriangle(p,b,c);

        if(primaryTriangleAr==area1+area2+area3)
            return true;
        return false;

    }
    // Calculates the area of the triangle
    static double AreaOfTriangle(Point A, Point B, Point C)
    {
        return Math.abs((A.x*(B.y-C.y) + B.x*(C.y-A.y)+ C.x*(A.y-B.y))/2.0);
    }
}
Output:

The point lies inside the triangle

Method-2: Java Program to Check whether a given point lies inside a triangle or not By User Input Value

Approach:

  1. Take user input the value for coordinates of the triangle and then the point to check.
  2. Then call the checkPointLoc() user defined method by passing the three coordinates and the point value as parameter.
  3. In this method the sum of areas of the three sub triangles is calculated and then it is compared with the area of the main triangle.
  4. Then print the result.

Program:

import java.awt.Point; 
import java.util.Scanner;
import static java.lang.Math.*;

public class Main
{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);        
        // Taking user input of the coordinates of the triangle
        System.out.println("Enter First Point Of the triangle");
        Point a = new Point(scan.nextInt(),scan.nextInt()); 
        System.out.println("Enter Second Point Of the triangle");
        Point b = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter Third Point Of the triangle");
        Point c = new Point(scan.nextInt(),scan.nextInt());
        // Taking user input of the coordinates of the Point
        System.out.println("Enter Coordinates of The Point To Check");
        Point p = new Point(scan.nextInt(),scan.nextInt());
        // Checks if the point lies in
        if(checkPointLoc(a,b,c,p))
            System.out.println("The point lies inside the triangle");
        else
            System.out.println("The point does not lie inside the triangle");
    }
    
    // Checks if the areas of the three small triangles add upto the primary triangle
    static boolean checkPointLoc(Point a, Point b, Point c, Point p)
    {
        double primaryTriangleAr = AreaOfTriangle(a,b,c);
        double area1 = AreaOfTriangle(a,b,p);
        double area2 = AreaOfTriangle(a,p,c);
        double area3 = AreaOfTriangle(p,b,c);

        if(primaryTriangleAr==area1+area2+area3)
            return true;
        return false;

    }
    // Calculates the area of the triangle
    static double AreaOfTriangle(Point A, Point B, Point C)
    {
        return Math.abs((A.x*(B.y-C.y) + B.x*(C.y-A.y)+ C.x*(A.y-B.y))/2.0);
    }
}
Output:

Case-1

Enter First Point Of the triangle
4 12
Enter Second Point Of the triangle
15 8
Enter Third Point Of the triangle
10 30
Enter Coordinates of The Point To Check
9 15
The point lies inside the triangle


Case-2

Enter First Point Of the triangle
2 3
Enter Second Point Of the triangle
10 2
Enter Third Point Of the triangle
4 8
Enter Coordinates of The Point To Check
12 16
The point does not lie inside the triangle

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Articles: