Determining the shortest distance between the coordinates of the rectangle quadrants

We should all know the coordinate system is divided into four quadrants (Quadrant)
In fact, this is a very simple rectangular distance algorithm, roughly divided into the following steps:
1. lower left point at which a rectangle to make a two-dimensional coordinate origin Department, further determines a point in the bottom left quadrant of the coordinate system which (may also be other points, but have to be compared are the same point);
2. take a point where different according to different quadrants in a rectangular as a comparison;
3. do get to the two points of the subtraction vector;
4. after the determination of the vector by subtracting the distance of two rectangular;
code as follows:

// taken from two intersecting rectangles -1 if otherwise from two rectangular 
int Distance (Rect rect1, Rect rect2) 
{ 
    // for inverting the X-axis is determined in the third quadrant for rect1 rect2's 
    BOOL isInversion ;
     // save the two comparison points 
    point Point1; 
    point Point2; 
    // Analyzing rect1 rect2 above or below that is to say the first three or four two-quadrant or quadrants 
    IF (rect1.y < rect2.y) 
    { 
    // Analyzing rect1 rect2 the left or right of that is to say a quadrant or two quadrants 
        isInversion = rect1.x < rect2.x;
         IF (is1Quadrant) 
        { 
            // get the upper right rect1 
            Point1 = new new point (rect1. x + rect1.width, rect1.y +rect1.height);
             // get a lower left point of rect2 
            Point2 = new new Point (rect2.x, rect2.y); 
        } the else 
        { 
            // left upper point of rect1 
            Point1 = new new Point (rect1.x, rect1.y + rect1. height);
             // get the lower right point rect2 
            Point2 = new new point (rect2.x + rect2.width, rect2.y); 
        }                
    } the else 
    { 
    // Analyzing rect1 rect2 right or left quadrant of that is to say three or quadrant 
        isInversion = rect1.x> rect2.x;
         IF (isInversion) 
        { 
            //Take rect2 upper right point 
            Point1 = new new Point (rect2.x + rect2.width, rect2.y + rect2.height);
             // get rect1 lower left point 
            Point2 = new new Point (rect1.x, rect1.y); 
        } the else 
        { 
            // get rect2 the left point 
            Point1 = new new point (rect2.x, rect2.y + rect2.height);
             // get a lower right point rect1 
            Point2 = new new point (rect1.x + rect1.width, rect1.y); 
        } 
    } 
    // do vector subtraction 
    Point Point2 = DPoint - Point1;
     // if the x-axis inverted
    ? dPoint.x = isInversion dPoint.x: - DPoint;
     // if this vector in the third quadrant of the intersection of two rectangles then returns -1 
    IF (dPoint.x < 0 && dPoint.y < 0 )
         return - . 1 ;
     // if x <0 return y 
    IF (dPoint.x < 0 )
         return dPoint.y;
     // returns x if y is less than 0 
    IF (dPoint.y < 0 )
         return dPoint.x;
     // returns the length of the vector 
    return dPoint.magnitude; 
}

 

By determining a quadrant of a method can quickly determine the rectangle intersect while knowing the distance between the two

Algorithmic process
the two intersecting rectangles FIG diagram, the two rectangular acquire verified, then the verification all other replicability.

In the lower left rect1 rect2 rect2 is in the third quadrant, the upper right we take rect1 point p1, p2 taking rect2 lower left point p1 and subtracting we get a third quadrant point p2 i.e. with x and y are both negative points.
Conversely, if the two rectangles do not intersect, then we have to get a point is a positive number.

Guess you like

Origin www.cnblogs.com/lx17746071609/p/11549755.html