
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check if a point is on the left or right side of a line
A line consists of an infinite number of points. In two-dimensional coordinate system we can define every point with two values i.e. X and Y. A point basically situated on the left side or right side or it can be on the line itself and this can be defined only if we have the coordinates of it.
In this program we are going to use the cross-product method to find the direction of the point. Cross- Product method is used to find the third vector from two vectors by cross multiplying both the vectors. In our case if we cross product between the origin points of the line and the point which is given for finding the direction. Then either we get a positive value or a negative value or zero.
If we get a positive value then we can ensure that the point is situated on the right side of the line. If we get negative value then we can ensure that the point is situated on the left side of the line. If zero, then the point is laid somewhere on the line.
Formula to calculate the cross-product of two points A(x, y) and B(x, y) ?
Cross-Product = [A(x) X B(y)] - [B(x) X A(y)]
In this article we will see how we can find whether the point is on the left side or on the right side of a line using Java.
Problem Statement
Write a program in Java to check if a point is on the left or right side of a line ?
Input 1
The input point of line:
A(x, y) = (20, -20)
B(x, y) = (-30, -23)
The coordinate of the point:
P(x, y) = (4, 5)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (-50,-3)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (-16,-25)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] - [O2(x) X O1(y)]
=[-50 X -25] - [-16 X -3]
= 1250 - 48 = 1202
Output 1
As the output is positive, hence the point is situated on the right side of the line
Input 2
The input point of line:
A(x, y) = (-2, 4)
B(x, y) = (7, 8)
The coordinate of the point:
P(x, y) = (10,-20)
The coordinates of origin point and the observed points are:
O1(x, y) = [(B(x)-A(x)), (B(y)-A(y))] = (9, 4)
O2(x, y) = [(P(x)-A(x)), (P(y)-A(y))] = (12, -24)
The cross-product between the O1 and O2 = [O1(x) X O2(y)] - [O2(x) X O1(y)]
= [9 X -24] - [12 X 4]
= -216 - 48 = -264
Output 2
As the output is negative, hence the point is situated on the left side of the line.
Multiple Approaches
We have provided the solution in different approaches ?
Using static input value
In this approach, we declare the points of the line and the point co-ordinated by static input method and pass those values as an argument to our user-defined method. Then by using the algorithm inside the method we can find the direction of the point with respect to the line.
- Start by creating the Main class and define the main method.
- Create three point objects: first, second, and point.
- Assign static values to first and second for the line's endpoints.
- Assign a static value to point for the point to check.
- Call the dirOfPoint method with the three point objects to calculate direction.
- If the returned value is 1, print that the point is on the right side of the line, if it's -1, print that the point is on the left side of the line and if the returned value is 0, print that the point is on the line.
- Define a static inner class pnt with int x and int y.
- Implement the dirOfPoint method to calculate the cross-product and return the direction.
Example
Below is the example to check if a point is on the left or right side of a line ?
public class Main{ public static void main(String[] args) { pnt first = new pnt(); pnt second = new pnt(); pnt point = new pnt(); first.x = -20; first.y = 15; // first(-20, 15) second.x = 31; second.y = -18; // second(31, -18) point.x = 32; point.y = 45; // point(32, 45) int dir = dirOfPoint(first, second, point); if (dir == 1) System.out.println("The point is on Right Direction of the line."); else if (dir == -1) System.out.println("The point is on Left Direction of the line."); else System.out.println("Point is somewhere on the Line."); } static class pnt{ int x, y; }; static int R = 1, L = -1, Z = 0; static int dirOfPoint(pnt first,pnt second, pnt point) { second.x -= first.x; second.y -= first.y; point.x -= first.x; point.y -= first.y; int crs_prod = second.x * point.y - second.y * point.x; if (crs_prod > 0) return R; if (crs_prod < 0) return L; return Z; } }
Output
The point is on Right Direction of the line.
Using user input value
In this approach, we declare the points of the line and the point co-ordinated by user input method and pass those values as an argument to our user-defined method. Then by using the algorithm inside the method we can find the direction of the point with respect to the line.
- First, import the all the necessary classes from the java.util package and will create the Main class and define the main method.
- After that we will create a Scanner object for user input and create three point objects: first, second, and point.
- Ask the user to enter coordinates for the first point of the line and store them in first.x and first.y after that for the second point of the line and store them in second.x and second.y.
- Prompt the user to enter coordinates for the point to check and store them in point.x and point.y.
- Call the dirOfPoint method with the three point objects to calculate direction.
- We will use the if-else conditional statement. If the returned value is 1, print that the point is on the right side of the line if it's -1, print that the point is on the left side of the line and if the returned value is 0, print that the point is on the line.
- Define a static inner class pnt with int x and int y.
- Implement the dirOfPoint method to calculate the cross-product and return the direction.
Example
Below is the example to check if a point is on the left or right side of a line ?
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); pnt first = new pnt(); pnt second = new pnt(); pnt point = new pnt(); System.out.println("Enter the coordinates of first point of the line: "); System.out.println("--------------------------------------------------"); System.out.print("X- value: "); first.x = sc.nextInt(); System.out.print("Y- value: "); first.y = sc.nextInt(); System.out.println("Enter the coordinates of second point of the line:"); System.out.println("--------------------------------------------------"); System.out.print("X value: "); second.x = sc.nextInt(); System.out.print("Y value: "); second.y = -sc.nextInt(); System.out.println("Enter the coordinates of the point which you want to navigate: "); System.out.println("--------------------------------------------------"); System.out.print("X value: "); point.x = sc.nextInt(); System.out.print("Y value: "); point.y = sc.nextInt(); int dir = dirOfPoint(first, second, point); if (dir == 1) System.out.println("The point is on Right Direction of the line."); else if (dir == -1) System.out.println("The point is on Left Direction of the line."); else System.out.println("Point is somewhere on the Line."); } static class pnt{ int x, y; }; static int R = 1, L = -1, Z = 0; static int dirOfPoint(pnt first,pnt second, pnt point){ second.x -= first.x; second.y -= first.y; point.x -= first.x; point.y -= first.y; int crs_prod = second.x * point.y - second.y * point.x; if (crs_prod > 0) return R; if (crs_prod < 0) return L; return Z; } }
Output
Enter the coordinates of first point of the line: --------------------------------------------------- X- value: 10 Y- value: 20 Enter the coordinates of second point of the line: ---------------------------------------------------- X value: -11 Y value: 12 Enter the coordinates of the point which you want to navigate: ---------------------------------------------------------------- X value: 4 Y value: 6 The point is on Right Direction of the line.
In this article, we explored how to check if a point is on the left or right side of a line in Java by using different approaches.