Java Program to Find Direction of a Point from a Line Segment

In the previous article, we have seen Java Program to Find Points that Divides a Line in Given Ratio (Section Formula)

In this article we will discuss about how to Find Direction of a Point from a Line Segment using Java programming language.

Java Program to Find Direction of a Point from a Line Segment

Before jumping into the program directly, let’s first know how can we find Direction of a Point from a Line Segment

Explanation:

Formula to Find Direction of a Point from a Line Segment: Ax*By – Ay*Bx

Then from the resultant we can obtain the direction using these three cases:

  1. If the result is positive then the point is on the right side of the line segment.
  2. If it is negative then the point is on the left of the line segment.
  3. If it is zero then the point is on the line segment itself.

Let’s see different ways to Find Direction of a Point from a Line Segment.

Method-1: Java Program to Find Direction of a Point from a Line Segment By Using Static Value

Approach:

  1. Declare the value for the coordinates of the start and end points of the line and the point to check direction.
  2. Then call the pointDirection() method by passing all the coordinates as parameter.
  3. In this method it sets one point to origin and then calculates the cross product and returns its sign. If it is positive then the point is towards the right, if it is negative then it is at left of the line else it is on the line itself.
  4. Then print the result.

Program:

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

public class Main
{

    // Static constant values
    static int RIGHT = 1, LEFT = -1, ZERO = 0;
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);        
        //Coordinates of the line
        Point a = new Point(-30,10);
        Point b = new Point(29,-15);
        //Coordinates of the line
        Point p = new Point(15,28);
        // Calls the function to find the direction of point w.r.t. the line segment
        int dir = pointDirection(a,b,p);
        if (dir == 1)
            System.out.println("Point is towards the right of the line");
        else if (dir == -1)
            System.out.println("Point is towards the left of the line");
        else
            System.out.println("Point is on the Line");
    }
    
    // Function to calculate direction of  the point from the line segment
    static int pointDirection(Point A, Point B, Point P)
    {
        // Subtracting the coordinates of the first point 
        // from the rest points to make A origin
        B.x -= A.x;
        B.y -= A.y;
        P.x -= A.x;
        P.y -= A.y;

        //Cross product formula
        int crossPro = B.x * P.y - B.y * P.x;
        // Returns zero,right or left based on the sign of the crossproduct
        if (crossPro > 0)
            return RIGHT; 
        if (crossPro < 0)
            return LEFT;
        return ZERO;
    }
}

Output:

Point is towards the right of the line

Method-2: Java Program to Find Direction of a Point from a Line Segment By Using Static Value

Approach:

  1. Take user input the value for the coordinates of the start and end points of the line and the point to check direction.
  2. Then call the pointDirection() method by passing all the coordinates as parameter.
  3. In this method it sets one point to origin and then calculates the cross product and returns its sign. If it is positive then the point is towards the right, if it is negative then it is at left of the line else it is on the line itself.
  4. Then print the result.

Program:

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

public class Main
{

    // Static constant values
    static int RIGHT = 1, LEFT = -1, ZERO = 0;
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);        
        //Asks the user for the Coordinates of the line
        System.out.println("Enter coordinates of the first point of the line");
        Point a = new Point(scan.nextInt(),scan.nextInt());
        System.out.println("Enter coordinates of the second point of the line");
        Point b = new Point(scan.nextInt(),scan.nextInt());
        //Asks the user for the Coordinates of the line
        System.out.println("Enter coordinates of the Point to check its direction");
        Point p = new Point(scan.nextInt(),scan.nextInt());
        // Calls the function to find the direction of point w.r.t. the line segment
        int dir = pointDirection(a,b,p);
        if (dir == 1)
            System.out.println("Point is towards the right of the line");
        else if (dir == -1)
            System.out.println("Point is towards the left of the line");
        else
            System.out.println("Point is on the Line");
    }
    
    // Function to calculate direction of  the point from the line segment
    static int pointDirection(Point A, Point B, Point P)
    {
        // Subtracting the coordinates of the first point 
        // from the rest points to make A origin
        B.x -= A.x;
        B.y -= A.y;
        P.x -= A.x;
        P.y -= A.y;

        //Cross product formula
        int crossPro = B.x * P.y - B.y * P.x;
        // Returns zero,right or left based on the sign of the crossproduct
        if (crossPro > 0)
            return RIGHT; 
        if (crossPro < 0)
            return LEFT;
        return ZERO;
    }
}
Output:

Case-1
Enter coordinates of the first point of the line
-25 8
Enter coordinates of the second point of the line
22 -12
Enter coordinates of the Point to check its direction
11 24
Point is towards the right of the line

Case-2
Enter coordinates of the first point of the line
18 -18
Enter coordinates of the second point of the line
-11 11
Enter coordinates of the Point to check its direction
6 7
Point is towards the left of the line

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Articles: