The Java Program: B_euclid/euclid_chinmay.java

  1 import java.util.*;
  2 import java.io.*;
  3 public class euclid_chinmay {
  4         
  5         /* Euclidean distance computing function */
  6         static double dist(double x1, double y1, double x2, double y2)
  7         {
  8                 return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  9         }
 10         
 11         
 12         
 13         public static void main(String[] args) throws Exception{
 14 
 15                 Scanner in = new Scanner(new File("euclid.in"));
 16                 PrintStream out = new PrintStream(new File("euclid.out"));
 17                 
 18                 double eps = 1.0e-9;
 19                 
 20                 
 21                 while(true)
 22                 {
 23                         double ax = in.nextDouble();
 24                         double ay = in.nextDouble();
 25                         double bx = in.nextDouble();
 26                         double by = in.nextDouble();
 27                         double cx = in.nextDouble();
 28                         double cy = in.nextDouble();
 29                         double dx = in.nextDouble();
 30                         double dy = in.nextDouble();
 31                         double ex = in.nextDouble();
 32                         double ey = in.nextDouble();
 33                         double fx = in.nextDouble();
 34                         double fy = in.nextDouble();
 35                 
 36                         /* Computing the area of DEF by the semi-perimeter method */
 37                         
 38                         double A = dist(dx, dy, ex, ey);
 39                         double B = dist(ex,ey,fx,fy);
 40                         double C = dist(fx,fy,dx,dy);
 41                         
 42                         double S = (A+B+C)/2;
 43                         double area = Math.sqrt(S*(S-A)*(S-B)*(S-C));
 44                         
 45                         
 46                         /* The ending condition */
 47                         if(area < eps) break;
 48                         
 49                         
 50                         /*
 51                          We use the fact that Area of Parallelogram AHGB
 52                          
 53                          = Absolute value of the cross product of AH and AB
 54                          = |AHxAB|
 55                          = Area of triangle DEF
 56                          
 57                          But AH = alpha times AC where we know all the other quantities except alpha
 58                          
 59                          So we compute alpha
 60                          */
 61                         double alpha = area/Math.abs((cx-ax)*(by-ay) - (cy-ay)*(bx-ax));
 62                         
 63                         
 64                         /* Now AH = alpha x AC  and  AG = AH + AB in vector terms */
 65                         double hx = alpha*(cx-ax) + ax;
 66                         double hy = alpha*(cy-ay) + ay;
 67                         
 68                         double gx = hx+bx-ax;
 69                         double gy = hy+by-ay;
 70                         
 71                         out.printf("%.3f %.3f %.3f %.3f\n", gx, gy, hx, hy);
 72                         
 73                 }
 74         }
 75 }
 76