var type = require('mutypes');
var mumath = require('mumath');

/** @module  intersects */
module.exports = intersect;


/** Default settings */
var defaults = {
	/** Target container to observe within */
	within: undefined,

	/** Offsets from container to provide gaps */
	offsets: 0,

	/** The amount of square to detect intersection.
	 * 0 - touches
	 * 1 - fits
	 * .5 - 50% square intersection
	 * @type {(Function|value)}
	 */
	tolerance: .5
};


/**
 * Main intersection detector.
 *
 * @param {Rectangle} a Target
 * @param {Rectangle} b Container
 *
 * @return {bool} Whether target is within the container
 */
function intersects (a, b, opts){
	//ignore definite disintersection
	if (a.right < b.left || a.left > b.right) return false;
	if (a.bottom < b.top || a.top > b.bottom) return false;

	//intersection values
	var iX = Math.min(a.right - b.left, b.right - a.left);
	var iY = Math.min(a.bottom - b.top, b.bottom - a.top);
	var iSquare = iX * iY;

	var bSquare = (b.bottom - b.top) * (b.right - b.left);
	var aSquare = (a.bottom - a.top) * (a.right - a.left);

	//measure square overlap relative to the min square
	var targetSquare = Math.min(aSquare, bSquare);

	//minimal overlap ratio
	var minRatio = opts && opts.tolerance || defaults.tolerance;

	if (iSquare / targetSquare > minRatio) {
		return true;
	}

	return false;
}
@@ -57,16 +57,17 @@
 ath');%0A%0A
+%0A
 /** @mod
@@ -111,16 +111,55 @@
 ntersect
+s;%0A%0A%0Avar min = Math.min, max = Math.max
 ;%0A%0A%0A/** 
@@ -870,21 +870,16 @@
 ar iX = 
-Math.
 min(a.ri
@@ -888,15 +888,28 @@
 t - 
+max(
 b.left,
+ a.left),
  b.r
@@ -915,23 +915,36 @@
 right - 
-a
+max(a.left, b
 .left)
+)
 ;%0A%09var i
@@ -947,21 +947,16 @@
 ar iY = 
-Math.
 min(a.bo
@@ -966,14 +966,26 @@
 m - 
+max(
 b.top,
+ a.top),
  b.b
@@ -992,21 +992,33 @@
 ottom - 
+max(
 a.top
+, b.top)
 );%0A%09var 
@@ -1223,21 +1223,16 @@
 quare = 
-Math.
 min(aSqu
@@ -1243,24 +1243,25 @@
  bSquare);%0A%0A
+%0A
 %09//minimal o
@@ -1312,18 +1312,48 @@
 lerance 
-%7C%7C
+!== undefined ? opts.tolerance :
  default

Fix algorithm