Program to Find the Mid-point of a Line in Java
In geometry, a line segment is a part of a line that is defined by two distinct endpoints. The mid-point of a line segment is the point that lies exactly halfway between the two endpoints. It can be calculated by finding the average of the x-coordinates and y-coordinates of the two endpoints.
For example, let's say the user enters the coordinates (4, 6) for the first point and (6, 8) for the second point. The program will calculate the mid-point as follows:
Mid-point X-coordinate: (4 + 6) / 2 = 10 / 2 = 5
Mid-point Y-coordinate: (6 + 8) / 2 = 14 / 2 = 7
Therefore, the mid-point of the line segment with the given coordinates is (5, 7).
![Program to Find the Mid-point of a Line in Java](https://static.tutorialandexample.com/java/program-to-find-the-mid-point-of-a-line-in-java1.png)
Image: Mid-point of line
Approach: Using Scanner Class
Example 1
In this program, we use the Scanner class to read input from the user. The program prompts the user to enter the coordinates of two points that define the line. After reading the coordinates, it calculates the mid-point by averaging the x-coordinates and y-coordinates of the two points separately. Finally, it displays the mid-point on the console.
FileName: MidPointFinder.java
import java.util.Scanner; public class MidPointFinder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt the user to enter the coordinates of the first point (x1, y1) System.out.print("Enter the coordinates of the first point (x1 y1): "); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); // Prompt the user to enter the coordinates of the second point (x2, y2) System.out.print("Enter the coordinates of the second point (x2 y2): "); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); // Calculate the mid-point coordinates double midPointX = (x1 + x2) / 2; double midPointY = (y1 + y2) / 2; // Display the mid-point coordinates on the console System.out.println("The mid-point of the line is: (" + midPointX + ", " + midPointY + ")"); } }
Output
Enter the coordinates of the first point (x1 y1): 4 6 Enter the coordinates of the second point (x2 y2): 6 8 The mid-point of the line is: (5.0, 7.0)
Approach: Vector Addition
Example 2
The given approach provides another way to find the mid-point of a line segment by utilizing vector components and adding them to the coordinates of the first point. The program prompts the user to enter the coordinates of the first point (x1, y1) and the second point (x2, y2) that define the line segment. The vector components are calculated by subtracting the coordinates of the first point from the coordinates of the second point. At the last program displays the mid-point coordinates on the console.
FileName: MidPointFinder.java
import java.util.Scanner; public class MidPointFinder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the coordinates of the first point (x1 y1): "); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); System.out.print("Enter the coordinates of the second point (x2 y2): "); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); // Calculate the vector components double vectorX = x2 - x1; double vectorY = y2 - y1; // Calculate the mid-point coordinates double midPointX = x1 + (vectorX / 2); double midPointY = y1 + (vectorY / 2); System.out.println("The mid-point of the line is: (" + midPointX + ", " + midPointY + ")"); } }
Output
Enter the coordinates of the first point (x1 y1): 4 6 Enter the coordinates of the second point (x2 y2): 6 8 The mid-point of the line is: (5.0, 7.0)
Approach: linear Interpolation
Example 3
The given approach uses linear interpolation to find the mid-point by calculating an intermediate value between the start and end values based on a given ratio. In this case, the ratio of 0.5 is used to find the midpoint.
FileName: MidPointFinder.java
import java.util.Scanner; public class MidPointFinder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the coordinates of the first point (x1 y1): "); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); System.out.print("Enter the coordinates of the second point (x2 y2): "); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); // Calculate the mid-point coordinates double midPointX = interpolate(x1, x2, 0.5); double midPointY = interpolate(y1, y2, 0.5); System.out.println("The mid-point of the line is: (" + midPointX + ", " + midPointY + ")"); } // Linear interpolation method public static double interpolate(double start, double end, double ratio) { return start + (end - start) * ratio; } }
Output
Enter the coordinates of the first point (x1 y1): 4 6 Enter the coordinates of the second point (x2 y2): 6 8 The mid-point of the line is: (5.0, 7.0)