view trunk/chipmunkd/cpBB.d @ 4:7ebbd4d05553

initial commit
author Extrawurst
date Thu, 02 Dec 2010 02:11:26 +0100
parents
children df4ebc8add66
line wrap: on
line source


// written in the D programming language

module chipmunkd.cpBB;

import chipmunkd.cpVect_h;
import chipmunkd.chipmunk_types_h;

struct cpBB{
	cpFloat l, b, r ,t;
}

static cpBB
cpBBNew(const cpFloat l, const cpFloat b,
		const cpFloat r, const cpFloat t)
{
	cpBB bb = {l, b, r, t};
	return bb;
}

static cpBool
cpBBintersects(const cpBB a, const cpBB b)
{
	return (a.l<=b.r && b.l<=a.r && a.b<=b.t && b.b<=a.t);
}

static cpBool
cpBBcontainsBB(const cpBB bb, const cpBB other)
{
	return (bb.l < other.l && bb.r > other.r && bb.b < other.b && bb.t > other.t);
}

static cpBool
cpBBcontainsVect(const cpBB bb, const cpVect v)
{
	return (bb.l < v.x && bb.r > v.x && bb.b < v.y && bb.t > v.y);
}

static cpBB
cpBBmerge(const cpBB a, const cpBB b){
	return cpBBNew(
		cpfmin(a.l, b.l),
		cpfmin(a.b, b.b),
		cpfmax(a.r, b.r),
		cpfmax(a.t, b.t)
	);
}

static cpBB
cpBBexpand(const cpBB bb, const cpVect v){
	return cpBBNew(
		cpfmin(bb.l, v.x),
		cpfmin(bb.b, v.y),
		cpfmax(bb.r, v.x),
		cpfmax(bb.t, v.y)
	);
}

cpVect
cpBBClampVect(const cpBB bb, const cpVect v)
{
	cpFloat x = cpfmin(cpfmax(bb.l, v.x), bb.r);
	cpFloat y = cpfmin(cpfmax(bb.b, v.y), bb.t);
	return cpv(x, y);
}

cpVect
cpBBWrapVect(const cpBB bb, const cpVect v)
{
	cpFloat ix = cpfabs(bb.r - bb.l);
	cpFloat modx = cpfmod(v.x - bb.l, ix);
	cpFloat x = (modx > 0.0f) ? modx : modx + ix;
	
	cpFloat iy = cpfabs(bb.t - bb.b);
	cpFloat mody = cpfmod(v.y - bb.b, iy);
	cpFloat y = (mody > 0.0f) ? mody : mody + iy;
	
	return cpv(x + bb.l, y + bb.b);
}