In the previous article, we have discussed about Java Program to Calculate the Length of Hypotenuse
In this article we are going to see how to return -1 if point is on Left and 1 if point is on Right by using Java programming language.
Java Program to Return -1 if Point is on Left and 1 if Point is on Right
Before Jumping into the program directly let’s see how to Return -1 if Point is on Left and 1 if Point is on Right.
Suppose there is a line segment SE
, Where the starting point is S
having coordinates sx
, sy
,Ending point E
is having coordinates ex
, ey
and we draw a point p
having coordinates px
, py
So if the point P lies in between SE then returns 0
If the point P lies in left side of SE then returns -1
If the point P lies in right side of SE then returns 1
We can check the point using distance formula
SP = √(PX-SX)2 + (PY-SY)2
EP = √(PX-EX)2 + (PY-EY)2
SE = √(SX-EX)2 + (SY-EY)2
Now,
If SP + EP = SE, Then point lies in between SE (returns 0)
If SP + EP != SE, && SP < EP Then point lies in left side of SE (returns -1)
If SP + EP != SE, && SP > EP Then point lies in right side of SE (returns 1)
Example:
Sx = 0, sy = 0 , ex = 0 , ey = 5, px = 0, py = (-9)
SP = 9
EP = 14
SE = 5
Since SP < EP, hence point P lies in left side of SE & returns -1
Let’s see different ways to return -1 if point is on Left and 1 if point is on Right.
Method-1: Java Program to Return -1 if Point is on Left and 1 if Point is on Right By Using Static Input Value
Approach:
- Declare an double variable say ‘sx’ and assign the value to it, which holds the x coordinate of point S
- Declare an double variable say ‘sy’ and assign the value to it, which holds the y coordinate of point S
- Declare an double variable say ‘px’ and assign the value to it, which holds the x coordinate of point P
- Declare an double variable say ‘py’ and assign the value to it, which holds the y coordinate of point P
- Declare an double variable say ‘ex’ and assign the value to it, which holds the x coordinate of point E
- Declare an double variable say ‘ey’ and assign the value to it, which holds the y coordinate of point E
- Find the distance of SP, EP, SE using the distance formula
- SP = √(PX-SX)2 + (PY-SY)2
- EP = √(PX-EX)2 + (PY-EY)2
- SE = √(SX-EX)2 + (SY-EY)2
- Print the result.
Program:
public class Main { public static int check(float sx,float sy,float ex,float ey,float px,float py) { double sp = Math.sqrt(Math.pow(px-sx,2)+Math.pow(py-sy,2)); double ep = Math.sqrt(Math.pow(px-ex,2)+Math.pow(py-ey,2)); double se = Math.sqrt(Math.pow(ex-sx,2)+Math.pow(ey-sy,2)); if (sp+ep == se) return 0; else if (sp+ep != se && sp < ep) return -1; else return 1; } public static void main(String[] args) { float sx = 0; float sy = 0; float ex = 0; float ey = 5; float px = 0; float py = -9; int result = check(sx,sy,ex,ey,px,py); System.out.println(result); } }
Output: -1
Method-2: Java Program to Return -1 if Point is on Left and 1 if Point is on Right By Using User Input Value
Approach:
- Declare an double variable say ‘sx’ which holds the x coordinate of point S
- Declare an double variable say ‘sy’ which holds the y coordinate of point S
- Declare an double variable say ‘px’ which holds the x coordinate of point P
- Declare an double variable say ‘py’ which holds the y coordinate of point P
- Declare an double variable say ‘ex’ which holds the x coordinate of point E
- Declare an double variable say ‘ey’ which holds the y coordinate of point E
- Then we will take the value of “sx”, “sy”, “px”, “py”, “ex”, “ey” as user input using scanner class.
- Find the distance of SP, EP, SE using the distance formula
- SP = √(PX-SX)2 + (PY-SY)2
- EP = √(PX-EX)2 + (PY-EY)2
- SE = √(SX-EX)2 + (SY-EY)2
- Print the result.
Program:
import java.util.*; public class Main { public static int check(double sx,double sy,double ex,double ey,double px,double py) { // formula to find distance of SP, EP, SE double sp = Math.sqrt(Math.pow(px-sx,2)+Math.pow(py-sy,2)); double ep = Math.sqrt(Math.pow(px-ex,2)+Math.pow(py-ey,2)); double se = Math.sqrt(Math.pow(ex-sx,2)+Math.pow(ey-sy,2)); // Checking conditions if (sp+ep == se) return 0; else if (sp+ep != se && sp < ep) return -1; else return 1; } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter the x coordinate of starting point S"); double sx = s.nextDouble(); System.out.println("Enter the y coordinate of starting point S"); double sy = s.nextDouble(); System.out.println("Enter the x coordinate of ending point E"); double ex = s.nextDouble(); System.out.println("Enter the y coordinate of ending point E"); double ey = s.nextDouble(); System.out.println("Enter the x coordinate of point P"); double px = s.nextDouble(); System.out.println("Enter the y coordinate of point P"); double py = s.nextDouble(); int result = check(sx,sy,ex,ey,px,py); System.out.println("----------------------------------------------"); System.out.println(result); } }
Output: Enter the x coordinate of starting point S 0 Enter the y coordinate of starting point S 0 Enter the x coordinate of ending point E 0 Enter the y coordinate of ending point E 5 Enter the x coordinate of point P 0 Enter the y coordinate of point P 3 ---------------------------------------------- 0
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: