@@ -519,43 +519,34 @@ var Util = PDFJS.Util = (function UtilClosure() {
519
519
};
520
520
521
521
// Returns a rectangle [x1, y1, x2, y2] corresponding to the
522
- // intersection of rect1 and rect2. If no intersection, returns 'false '
522
+ // intersection of rect1 and rect2. If no intersection, returns 'null '
523
523
// The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
524
524
Util .intersect = function Util_intersect (rect1 , rect2 ) {
525
- function compare (a , b ) {
526
- return a - b ;
527
- }
528
-
529
- // Order points along the axes
530
- var orderedX = [rect1 [0 ], rect1 [2 ], rect2 [0 ], rect2 [2 ]].sort (compare ),
531
- orderedY = [rect1 [1 ], rect1 [3 ], rect2 [1 ], rect2 [3 ]].sort (compare ),
532
- result = [];
533
-
534
- rect1 = Util .normalizeRect (rect1 );
535
- rect2 = Util .normalizeRect (rect2 );
536
-
537
- // X: first and second points belong to different rectangles?
538
- if ((orderedX [0 ] === rect1 [0 ] && orderedX [1 ] === rect2 [0 ]) ||
539
- (orderedX [0 ] === rect2 [0 ] && orderedX [1 ] === rect1 [0 ])) {
540
- // Intersection must be between second and third points
541
- result [0 ] = orderedX [1 ];
542
- result [2 ] = orderedX [2 ];
543
- } else {
544
- return false ;
525
+ const xLow = Math .max (
526
+ Math .min (rect1 [0 ], rect1 [2 ]),
527
+ Math .min (rect2 [0 ], rect2 [2 ])
528
+ );
529
+ const xHigh = Math .min (
530
+ Math .max (rect1 [0 ], rect1 [2 ]),
531
+ Math .max (rect2 [0 ], rect2 [2 ])
532
+ );
533
+ if (xLow > xHigh ) {
534
+ return null ;
545
535
}
546
-
547
- // Y: first and second points belong to different rectangles?
548
- if ((orderedY [0 ] === rect1 [1 ] && orderedY [1 ] === rect2 [1 ]) ||
549
- (orderedY [0 ] === rect2 [1 ] && orderedY [1 ] === rect1 [1 ])) {
550
- // Intersection must be between second and third points
551
- result [1 ] = orderedY [1 ];
552
- result [3 ] = orderedY [2 ];
553
- } else {
554
- return false ;
536
+ const yLow = Math .max (
537
+ Math .min (rect1 [1 ], rect1 [3 ]),
538
+ Math .min (rect2 [1 ], rect2 [3 ])
539
+ );
540
+ const yHigh = Math .min (
541
+ Math .max (rect1 [1 ], rect1 [3 ]),
542
+ Math .max (rect2 [1 ], rect2 [3 ])
543
+ );
544
+ if (yLow > yHigh ) {
545
+ return null ;
555
546
}
556
547
557
- return result ;
558
- };
548
+ return [ xLow , yLow , xHigh , yHigh ] ;
549
+ }
559
550
560
551
Util .sign = function Util_sign (num ) {
561
552
return num < 0 ? -1 : 1 ;
0 commit comments