/// <summary> /// Returns the distance from this to the other rect. 0 if there is an overlap. /// </summary> /// <param name="Other">Other rectangle.</param> public double Distance(C2DRect Other) { if (this.Overlaps(Other)) { return(0); } if (Other.GetLeft() > this.BottomRight.x) { // Other is to the right if (Other.GetBottom() > this.TopLeft.y) { // Other is to the top right C2DPoint ptTopRight = new C2DPoint(BottomRight.x, TopLeft.y); return(ptTopRight.Distance(new C2DPoint(Other.GetLeft(), Other.GetBottom()))); } else if (Other.GetTop() < this.BottomRight.y) { // Other to the bottom right return(BottomRight.Distance(Other.TopLeft)); } else { // to the right return(Other.GetLeft() - this.BottomRight.x); } } else if (Other.GetRight() < this.TopLeft.x) { // Other to the left if (Other.GetBottom() > this.TopLeft.y) { // Other is to the top left return(TopLeft.Distance(Other.BottomRight)); } else if (Other.GetTop() < this.BottomRight.y) { // Other to the bottom left C2DPoint ptBottomLeft = new C2DPoint(TopLeft.x, BottomRight.y); return(ptBottomLeft.Distance(new C2DPoint(Other.GetRight(), Other.GetTop()))); } else { //Just to the left return(this.TopLeft.x - Other.GetRight()); } } else { // There is horizontal overlap; if (Other.GetBottom() > TopLeft.y) { return(Other.GetBottom() - TopLeft.y); } else { return(BottomRight.y - Other.GetTop()); } } }