Find a line to cut two squares in half

Problem: 

Given two squares on a two dimensional plane, find a line that would cut these two squares in half.

Solution:

Any Line passing the centers of the two squares will cut them in halves. So, we have to connect the lines between the center of the 2 squares. The straight line that connects the two centers of the two squares will cut both of them into half.

Code in java

public class Square {  
   public double left;  
   public double top;  
   public double bottom;  
   public double right;  
   public Square(double left, double top, double size) {  
          this.left = left;  
          this.top = top;  
          this.bottom = top + size;  
          this.right = left + size;  
   }  
  
   public Point middle() {  
          return new Point((this.left + this.right) / 2,  
                                       (this.top + this.bottom) / 2);  
   }  
  
   public Line cut(Square other) {  
          Point middle\_s = this.middle();  
          Point middle\_t = other.middle();  
          if (middle\_s == middle\_t) {  
                 return new Line(new Point(left, top),  
                 new Point(right, bottom));  
          } else {  
                 return new Line(middle\_s, middle\_t);  
          }  
   }  
}  

Note specific to java  - If == operator between the 2 point objects in cut method. I am not sure if it will work, as == operator in java compares references. So, better approach is Point should implement equals() method and compare the 2 point object like this:

if(middle\_s.equals(middle\_t)){  
    ....  
}  

Thanks.
References :
http://tianrunhe.wordpress.com/2012/04/02/find-a-line-to-cut-two-squares-in-half/
http://stackoverflow.com/questions/6886836/can-i-use-the-operator-to-compare-point-objects-in-java


See also