00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00025
00027 typedef struct cpBB{
00028 cpFloat l, b, r ,t;
00029 } cpBB;
00030
00032 static inline cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
00033 {
00034 cpBB bb = {l, b, r, t};
00035 return bb;
00036 }
00037
00039 static inline cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
00040 {
00041 return cpBBNew(p.x - r, p.y - r, p.x + r, p.y + r);
00042 }
00043
00045 static inline cpBool cpBBIntersects(const cpBB a, const cpBB b)
00046 {
00047 return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
00048 }
00049
00051 static inline cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
00052 {
00053 return (bb.l <= other.l && bb.r >= other.r && bb.b <= other.b && bb.t >= other.t);
00054 }
00055
00057 static inline cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
00058 {
00059 return (bb.l <= v.x && bb.r >= v.x && bb.b <= v.y && bb.t >= v.y);
00060 }
00061
00063 static inline cpBB cpBBMerge(const cpBB a, const cpBB b){
00064 return cpBBNew(
00065 cpfmin(a.l, b.l),
00066 cpfmin(a.b, b.b),
00067 cpfmax(a.r, b.r),
00068 cpfmax(a.t, b.t)
00069 );
00070 }
00071
00073 static inline cpBB cpBBExpand(const cpBB bb, const cpVect v){
00074 return cpBBNew(
00075 cpfmin(bb.l, v.x),
00076 cpfmin(bb.b, v.y),
00077 cpfmax(bb.r, v.x),
00078 cpfmax(bb.t, v.y)
00079 );
00080 }
00081
00083 static inline cpFloat cpBBArea(cpBB bb)
00084 {
00085 return (bb.r - bb.l)*(bb.t - bb.b);
00086 }
00087
00089 static inline cpFloat cpBBMergedArea(cpBB a, cpBB b)
00090 {
00091 return (cpfmax(a.r, b.r) - cpfmin(a.l, b.l))*(cpfmax(a.t, b.t) - cpfmin(a.b, b.b));
00092 }
00093
00095 static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
00096 {
00097 cpFloat idx = 1.0f/(b.x - a.x);
00098 cpFloat tx1 = (bb.l == a.x ? -INFINITY : (bb.l - a.x)*idx);
00099 cpFloat tx2 = (bb.r == a.x ? INFINITY : (bb.r - a.x)*idx);
00100 cpFloat txmin = cpfmin(tx1, tx2);
00101 cpFloat txmax = cpfmax(tx1, tx2);
00102
00103 cpFloat idy = 1.0f/(b.y - a.y);
00104 cpFloat ty1 = (bb.b == a.y ? -INFINITY : (bb.b - a.y)*idy);
00105 cpFloat ty2 = (bb.t == a.y ? INFINITY : (bb.t - a.y)*idy);
00106 cpFloat tymin = cpfmin(ty1, ty2);
00107 cpFloat tymax = cpfmax(ty1, ty2);
00108
00109 if(tymin <= txmax && txmin <= tymax){
00110 cpFloat min = cpfmax(txmin, tymin);
00111 cpFloat max = cpfmin(txmax, tymax);
00112
00113 if(0.0 <= max && min <= 1.0) return cpfmax(min, 0.0);
00114 }
00115
00116 return INFINITY;
00117 }
00118
00120 static inline cpBool cpBBIntersectsSegment(cpBB bb, cpVect a, cpVect b)
00121 {
00122 return (cpBBSegmentQuery(bb, a, b) != INFINITY);
00123 }
00124
00126 cpVect cpBBClampVect(const cpBB bb, const cpVect v);
00127
00128
00130 cpVect cpBBWrapVect(const cpBB bb, const cpVect v);
00131