Advertisement
CR7CR7

LongerLine

Oct 22nd, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. // Online Java Compiler
  2. // Use this editor to write, compile and run your Java code online
  3. import java.util.Scanner;
  4. import java.lang.*;
  5. class LongerLine {
  6.     private static double lineLength (double x1, double y1, double x2, double y2)
  7.         {
  8.             return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
  9.         }
  10.  
  11.        private  static double distanceToCenter(double x, double y)
  12.         {
  13.             return Math.sqrt(Math.pow((0 - x), 2) + Math.pow((0 - y), 2));
  14.         }
  15.  
  16.         private static void printClosestPointFirst(double x1, double y1, double x2, double y2)
  17.         {
  18.             if (distanceToCenter(x1, y1) > distanceToCenter(x2, y2))
  19.             {
  20.                 System.out.print("(" + x2 + ", " + y2 + ")");
  21.                 System.out.println("(" + x1 + ", " + y1 + ")");
  22.             }
  23.             else
  24.             {
  25.                 System.out.print("(" + x1 + ", " + y1+")");
  26.                 System.out.println("(" + x2 + ", " + y2 + ")");
  27.             }
  28.         }
  29.  
  30.         private static void printLongerLine(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  31.         {
  32.             if (lineLength(x1, y1, x2, y2) >= lineLength(x3, y3, x4, y4))
  33.             {
  34.                 printClosestPointFirst(x1, y1, x2, y2);
  35.             }
  36.             else if (lineLength(x1, y1, x2, y2) < lineLength(x3, y3, x4, y4))
  37.             {
  38.                 printClosestPointFirst(x3, y3, x4, y4);
  39.             }
  40.         }
  41.     public static void main(String[] args) {
  42.        
  43.             Scanner scan=new Scanner(System.in);
  44.             double point1X = Double.parseDouble(scan.nextLine());
  45.             double point1Y = Double.parseDouble(scan.nextLine());
  46.             double point2X = Double.parseDouble(scan.nextLine());
  47.             double point2Y = Double.parseDouble(scan.nextLine());
  48.             double point3x = Double.parseDouble(scan.nextLine());
  49.             double point3Y = Double.parseDouble(scan.nextLine());
  50.             double point4X = Double.parseDouble(scan.nextLine());
  51.             double point4Y = Double.parseDouble(scan.nextLine());
  52.  
  53.             printLongerLine(point1X, point1Y, point2X, point2Y, point3x, point3Y, point4X, point4Y);
  54.        
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement