Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MoreExercise;
- import java.util.Scanner;
- public class LongerLine {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read coordinates of four points
- int x1 = scanner.nextInt();
- int y1 = scanner.nextInt();
- int x2 = scanner.nextInt();
- int y2 = scanner.nextInt();
- int x3 = scanner.nextInt();
- int y3 = scanner.nextInt();
- int x4 = scanner.nextInt();
- int y4 = scanner.nextInt();
- // Calculate lengths of both lines
- double length1 = calculateLength(x1, y1, x2, y2);
- double length2 = calculateLength(x3, y3, x4, y4);
- // Determine the point closer to the center for each line
- boolean firstLineFirstPointClosest = isCloserToCenter(x1, y1, x2, y2);
- boolean secondLineFirstPointClosest = isCloserToCenter(x3, y3, x4, y4);
- // Determine which line is longer
- if (length1 > length2) {
- // Print the longer line starting from the point closer to the center
- if (firstLineFirstPointClosest)
- printLine(x1, y1, x2, y2);
- else
- printLine(x2, y2, x1, y1);
- } else if (length2 > length1) {
- // Print the longer line starting from the point closer to the center
- if (secondLineFirstPointClosest)
- printLine(x3, y3, x4, y4);
- else
- printLine(x4, y4, x3, y3);
- } else {
- // Lines are of equal length, print only the first one
- printLine(x1, y1, x2, y2);
- }
- }
- // Method to calculate the length of a line given two points
- private static double calculateLength(int x1, int y1, int x2, int y2) {
- return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
- }
- // Method to check which point is closer to the center
- private static boolean isCloserToCenter(int x1, int y1, int x2, int y2) {
- double distance1 = Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2));
- double distance2 = Math.sqrt(Math.pow(x2, 2) + Math.pow(y2, 2));
- return distance1 <= distance2;
- }
- // Method to print a line in the required format
- private static void printLine(int x1, int y1, int x2, int y2) {
- System.out.printf("(%d, %d)(%d, %d)\n", x1, y1, x2, y2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement