How To Find the Midpoint of a Line in Java?


Let's say (x1,y1) is the starting point of line and (x2,y2) is the end point of line.

To get the midpoint of line we have to use the midpoint formula of line.

Midpoint = ((x1+x2)/2 , (y1+y2)/2)

In this article we will see how to find the midpoint of line when two points of the line are given by using Java Programming language.

To show you some instances

Instance-1

Let say the two points are (2,3) and (3,5)

By using the midpoint formula of line,

a = (x1+x2)/2 = (2+3)/2 = 2.5
b = (y1+y2)/2 = (3+5)/2 = 4.0

Hence, the midpoint of line is (2.5, 4.0)

Instance-2

Let say the two points are (2,-3) and (-3,5)

By using the midpoint formula of line,

a = (x1+x2)/2 = (2+(-3)/2 = -0.5
b = (y1+y2)/2 = ((-3) +5)/2 = 1.0

Hence, the midpoint of line is (-0.5, 1.0)

Instance-3

Let say the two points are (2,2) and (5,5)

By using the midpoint formula of line,

a = (x1+x2)/2 = (2+5)/2 = 3.5
b = (y1+y2)/2 = (2+5)/2 = 3.5

Hence, the midpoint of line is (3.5, 3.5)

Algorithm

  • Step 1 − Get the starting point and end point of the line either by static input or by user input.

  • Step 2 − Then by using the midpoint formula of the line find the midpoint.

  • Step 3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, the starting point and end point of the line will be initialized in the program. Then by using the algorithm find the mind point.

Example

public class Main{ //main method public static void main(String[] args){ //declared start point of line double x1 = -3; double y1 = 4; System.out.println("Start point of the line: "+x1+", "+y1); //Declared end point of line double x2 = -2; double y2 = 5; System.out.println("End point of the line: "+x2+", "+y2); //Find midpoint double x=(x1+x2)/2; double y=(y1+y2)/2; System.out.println("Mid Point = "+x+" , "+y); } }

Output

Start point of the line: -3.0, 4.0
End point of the line: -2.0, 5.0
Mid Point = -2.5 , 4.5

Approach-2: By Using User Defined

In this approach, the starting point and end point of the line will be initialized in the program. Then call a user defined method by passing these points as parameters and inside the method find the midpoint by using the algorithm.

Example

public class Main{ //main method public static void main(String[] args){ //declared start point of line double x1 = 2; double y1 = 2; System.out.println("Start point of the line: "+x1+", "+y1); //Declared end point of line double x2 = 7; double y2 = 9; System.out.println("End point of the line: "+x2+", "+y2); //call user defined method to find midpoint findMidpoint(x1,y1,x2,y2); } //user defined method public static void findMidpoint(double x1,double y1,double x2,double y2){ //Find midpoint double x=(x1+x2)/2; double y=(y1+y2)/2; System.out.println("Mid Point = "+x+" , "+y); } }

Output

Start point of the line: 2.0, 2.0
End point of the line: 7.0, 9.0
Mid Point = 4.5 , 5.5

In this article, we explored how to find the midpoint of a line in Java by using different approaches.

Updated on: 28-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements