//*********************************************************************
// Example7Geometry.java
//
// Class Example7Geometry uses several simple classes related to
// geometry.
//*********************************************************************

import corejava.*;

class Example7Geometry
{
   public static void main (String[] args)
   {
      double a = 2.5, b = 1;
      TwoDimPoint P = new TwoDimPoint (a, b);
      double[] A = {4, 7};
      TwoDimPoint Q = new TwoDimPoint (A);
      System.out.println (P + "\n" + Q);
      TwoDimPoint P1 = new TwoDimPoint();
      TwoDimPoint P2 = new TwoDimPoint(P1);
      TwoDimPoint P3 = P2;
      System.out.println (P1 + "\n" + P2 + "\n" +
                          P1.wrongEquals(P2) + "\t" + P1.equals(P2) + "\t" +
                          P2.wrongEquals(P3) + "\t" + P2.equals(P3));
      P.setFirst(2);
      Q.reflectOrigin();
      System.out.println (P + "\n" + Q + "\n" + P.distance(Q));
      P2.setSecond(5);
      System.out.println (P1 + "\n" + P3);

      Interval I = new Interval (P, Q);
      System.out.println ("\n" + I);
      P3 = I.midPoint();
      I.translate(P3);
      System.out.println (P3 + "\n" + I + "\n" + P + "\n" + Q);
      Interval I1 = new Interval (P1, new TwoDimPoint (-1, -12));
      Interval I2 = new Interval (new TwoDimPoint (-5, -10), I1.midPoint());
      System.out.println (I1 + "\n" + I2 + "\n" + I.equals(I2));
   }// method main
}// class Example7Geometry


class TwoDimPoint
{
   //===========================================================
   // Supports several operations on points in the plane.
   //===========================================================
   double x, y;

   TwoDimPoint (double x1, double y1)
   {
      x = x1;
      y = y1;
   }// method TwoDimPoint

   TwoDimPoint (double[] A)
   {
      if (A.length == 2)
      {
         x = A[0];
         y = A[1];
      }
      else
         System.out.println ("Number of components should be 2");
   }// method TwoDimPoint

   TwoDimPoint()
   {
      x = Console.readDouble("Enter first coordinate: ");
      y = Console.readDouble("Enter second coordinate: ");
   }// method TwoDimPoint

   TwoDimPoint (TwoDimPoint P)
   {
      x = P.getFirst();
      y = P.getSecond();
   }// method TwoDimPoint

   double getFirst()
   {return x;}

   double getSecond()
   {return y;}

   void setFirst (double x1) 
   {x = x1;}

   void setSecond (double y1)
   {y = y1;}

   void reflectFirst()  // Reflect the point with respect to the y-axis
   {x = -x;}

   void reflectSecond() // Reflect the point with respect to the x-axis
   {y = -y;}

   void reflectOrigin() // Reflect the point with respect to the origin
   {
      reflectFirst();
      reflectSecond();
   }// method reflectOrigin

   boolean wrongEquals (TwoDimPoint P) // Probably not what you want
   {return this == P;}

   boolean equals (TwoDimPoint P)
   {return P.getFirst() == x & P.getSecond() == y;}

   void translate (TwoDimPoint P)
   {
      x += P.getFirst();
      y += P.getSecond();
   }// translate

   double distance (TwoDimPoint P)
   {
      double temp = Math.pow (P.getFirst() - x, 2) +
                    Math.pow (P.getSecond() - y, 2);
      return Math.sqrt (temp);
   }// method distance

   public String toString()
   {
      return "x coordinate is " + x + ", y coordinate is " + y;
   }// method toString
}// class TwoDimPoint


class Interval
{
   //===========================================================
   // Supports several operations on intervals.
   //===========================================================
   TwoDimPoint[] edges = new TwoDimPoint[2];
   
   Interval (TwoDimPoint[] endpoints)
   {
      if (endpoints.length == 2)
      {
         edges[0] = new TwoDimPoint (endpoints[0]);
         edges[1] = new TwoDimPoint (endpoints[1]);
      }
      else
         System.out.println ("Number of endpoints should be 2");
   }// method Interval

   Interval (TwoDimPoint P, TwoDimPoint Q)
   {
      edges[0] = new TwoDimPoint(P);
      edges[1] = new TwoDimPoint(Q);
   }// method Interval

   Interval()
   {
      for (int i=0; i <= 1; i++)
      {
         edges[i].setFirst (Console.readDouble("Enter first coordinate: "));
         edges[i].setSecond (Console.readDouble("Enter second coordinate: "));
      }
   }// method Interval
 
   Interval (Interval I)
   {
      edges[0] = new TwoDimPoint (I.getEndpoint(0));
      edges[1] = new TwoDimPoint (I.getEndpoint(1));
   }// method Interval

   TwoDimPoint getEndpoint (int i)
   {return edges[i];}

   double length()
   {return edges[0].distance (edges[1]);}

   TwoDimPoint midPoint()
   {
      double x = (edges[0].getFirst() + edges[1].getFirst()) / 2;
      double y = (edges[0].getSecond() + edges[1].getSecond()) / 2;
      return new TwoDimPoint (x, y);
   }// method midPoint

   boolean equals (Interval I)
   {
      boolean result1 = edges[0].equals(I.getEndpoint(0))
                        & edges[1].equals(I.getEndpoint(1));
      boolean result2 = edges[0].equals(I.getEndpoint(1))
                        & edges[1].equals(I.getEndpoint(0));
      return result1 | result2;
   }// method equals

   void translate (TwoDimPoint P)
   {
      for (int i=0; i <= 1; i++)
         edges[i].translate(P);
   }// method translate

   public String toString()
   {
      String result = "";
      for (int i=0; i <= 1; i++)
         result += "Endpoint " + (i + 1) + " is: " + edges[i] + "\n";
      return result;
   }// method toString
}// class Interval
