/' Copyright (c) 2007 Scott Lembcke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 '/

type cpBB
	as cpFloat	l, b, r, t
end type

function cpBBNew( byval l as const cpFloat, byval b as const cpFloat, byval r as const cpFloat, byval t as const cpFloat ) as cpBB
	return type<cpBB> ( l, b, r, t )
end function

function cpBBintersects( byval a as const cpBB, byval b as const cpBB ) as cpBool
	return ( a.l <= b.r ) and ( b.l <= a.r ) and ( a.b <= b.t ) and ( b.b <= a.t )
end function

function cpBBcontainsBB( byval bb as const cpBB, byval other as const cpBB ) as cpBool
	return ( bb.l < other.l ) and ( bb.r > other.r ) and ( bb.b < other.b ) and ( bb.t > other.t )
end function

function cpBBcontainsVect( byval bb as const cpBB, byval v as const cpVect ) as cpBool
	return ( bb.l < v.x ) and ( bb.r > v.x ) and ( bb.b < v.y ) and ( bb.t > v.y )
end function

function cpBBmerge( byval a as const cpBB, byval b as const cpBB ) as cpBB
	return cpBBNew( _
		cpfmin( a.l, b.l ), _
		cpfmin( a.b, b.b ), _
		cpfmax( a.r, b.r ), _
		cpfmax( a.t, b.t ) )
end function

function cpBBexpand( byval bb as const cpBB, byval v as const cpVect ) as cpBB
	return cpBBNew( _
		cpfmin(bb.l, v.x), _
		cpfmin(bb.b, v.y), _
		cpfmax(bb.r, v.x), _
		cpfmax(bb.t, v.y) )
end function

declare function cpBBClampVect cdecl alias "cpBBClampVect" ( byval as const cpBB, byval as const cpVect ) as cpVect	'' clamps the vector to lie within the bbox
'' TODO edge case issue
declare function cpBBWrapVect cdecl alias "cpBBWrapVect" ( byval as const cpBB, byval as const cpVect ) as cpVect	'' wrap a vector to a bbox


