
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
How To Find the Midpoint of a Line in Java?
Midpoint of a Line
The midpoint refers to a point that is exactly in the middle of the line segment, which is joined through two points. These are the endpoints of a line segment and the midpoint lying in between these two points.
The following diagram will provide a better understanding:
Midpoint Formula
The midpoint formula is defined for the points in the coordinate axes (i.e., x and y). Let's consider (x1, y1) and (x2, y2) are the endpoints of a line segment. The midpoint will be equal to half of the sum of the x-coordinates of the two points and half of the sum of the y-coordinates of the two points.
Use the following formula to find the midpoint of the a line:
Midpoint of line = ((x1 + x2)/2 , (y1 + y2)/2)
Here, the points x1 and x2 are located on the x-axis, while points y1 and y2 are located on the y-axis.
Input & Output Scenarios
Following are the few input and output scenario that helps you to understand how to find the midpoint of a line by doing mathematical calculations:
Scenario 1
Suppose the two points on the line are (2,3) and (3,5):
Input: pintA = (2, 3), pointB = (3, 5) Output:midPoint = (2.5, 4.0) Calculation: ma = (x1 + x2)/2 = (2 + 3)/2 = 2.5 mb = (y1 + y2)/2 = (3 + 5)/2 = 4.0
The midpoint of line is (2.5, 4.0).
Scenario 2
Suppose the two points on the line are (2, -3) and (-3, 5):
Input: pointA = (2, -3), pointB = (-3, 5) Output: midPoint = (-0.5, 1.0) Calculation: ma = (x1 + x2)/2 = (2 + (-3)/2 = -0.5 mb = (y1 + y2)/2 = ((-3) + 5)/2 = 1.0
The midpoint of line is (-0.5, 1.0).
Let's implement the above scenarios in the Java program to find the midpoint of the given points:
Example 1: Midpoint of the Negative Coordinates
In the example below, we use the above formula to find the midpoint on the drawn line by using the given points (-2, -2) and (4, 3):
Diagram:
The diagram below drawn a line according to the specified points, and also mentioned the midpoint:
Program:
public class Line{ public static void main(String[] args){ double x1 = -2; double y1 = -2; double x2 = 4; double y2 = 3; System.out.println("The give points are: "); System.err.println("(x1 = " + x1 + ", y1 = " + y1 + ") and " + "(x2 = " + x2 + ", y2 = " + y2 + ")"); //find the midpoint of given points double x = (x1 + x2) / 2; double y=(y1 + y2) / 2; System.out.println("The mid point = (" + x + " , " + y + ")"); } }
Output
The above program produces the following output:
The give points are: (x1 = -2.0, y1 = -2.0) and (x2 = 4.0, y2 = 3.0) The mid point = (1.0 , 0.5)
Example 2: Midpoint of the Positive Coordinates
In the example below, we define a method named findMidpoint(), which uses the ((x1+x2)/2, (y1+y2)/2) formula to calculate the midpoint on the line by using the given points (1, 1) and (4, 3):
Diagram:
In the diagram below, the line is drawn according to the specified points, and the midpoint is also mentioned:
Program:
public class Line { //user defined method public static void findMidpoint(double x1, double y1, double x2, double y2){ //fint the midpoint double x = (x1 + x2) / 2; double y = (y1 + y2) / 2; System.out.println("The mid point = (" + x + " , " + y + ")"); } public static void main(String[] args){ double x1 = 1; double y1 = 1; double x2 = 4; double y2 = 3; System.out.println("The give points are: "); System.err.println("(x1 = " + x1 + ", y1 = " + y1 + ") and " + "(x2 = " + x2 + ", y2 = " + y2 + ")"); //calling the findMidpoint() method to find the midpoint findMidpoint(x1, y1, x2, y2); } }
Output
Following is the output of the above program:
The give points are: (1.0, 1.0) and (4.0, 3.0) The mid point = (2.5 , 2.0)