1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "chipmunk_types.h"
28 /// @defgroup cpBBB cpBB
29 /// Chipmunk's axis-aligned 2D bounding box type along with a few handy routines.
32 /// Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
37 /// Convenience constructor for cpBB structs.
38 static inline cpBB cpBBNew(const cpFloat l, const cpFloat b, const cpFloat r, const cpFloat t)
40 cpBB bb = {l, b, r, t};
44 /// Constructs a cpBB centered on a point with the given extents (half sizes).
46 cpBBNewForExtents(const cpVect c, const cpFloat hw, const cpFloat hh)
48 return cpBBNew(c.x - hw, c.y - hh, c.x + hw, c.y + hh);
51 /// Constructs a cpBB for a circle with the given position and radius.
52 static inline cpBB cpBBNewForCircle(const cpVect p, const cpFloat r)
54 return cpBBNewForExtents(p, r, r);
57 /// Returns true if @c a and @c b intersect.
58 static inline cpBool cpBBIntersects(const cpBB a, const cpBB b)
60 return (a.l <= b.r && b.l <= a.r && a.b <= b.t && b.b <= a.t);
63 /// Returns true if @c other lies completely within @c bb.
64 static inline cpBool cpBBContainsBB(const cpBB bb, const cpBB other)
66 return (bb.l <= other.l && bb.r >= other.r && bb.b <= other.b && bb.t >= other.t);
69 /// Returns true if @c bb contains @c v.
70 static inline cpBool cpBBContainsVect(const cpBB bb, const cpVect v)
72 return (bb.l <= v.x && bb.r >= v.x && bb.b <= v.y && bb.t >= v.y);
75 /// Returns a bounding box that holds both bounding boxes.
76 static inline cpBB cpBBMerge(const cpBB a, const cpBB b){
85 /// Returns a bounding box that holds both @c bb and @c v.
86 static inline cpBB cpBBExpand(const cpBB bb, const cpVect v){
95 /// Returns the center of a bounding box.
99 return cpvlerp(cpv(bb.l, bb.b), cpv(bb.r, bb.t), 0.5f);
102 /// Returns the area of the bounding box.
103 static inline cpFloat cpBBArea(cpBB bb)
105 return (bb.r - bb.l)*(bb.t - bb.b);
108 /// Merges @c a and @c b and returns the area of the merged bounding box.
109 static inline cpFloat cpBBMergedArea(cpBB a, cpBB b)
111 return (cpfmax(a.r, b.r) - cpfmin(a.l, b.l))*(cpfmax(a.t, b.t) - cpfmin(a.b, b.b));
114 /// Returns the fraction along the segment query the cpBB is hit. Returns INFINITY if it doesn't hit.
115 static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
117 cpVect delta = cpvsub(b, a);
118 cpFloat tmin = -INFINITY, tmax = INFINITY;
121 if(a.x < bb.l || bb.r < a.x) return INFINITY;
123 cpFloat t1 = (bb.l - a.x)/delta.x;
124 cpFloat t2 = (bb.r - a.x)/delta.x;
125 tmin = cpfmax(tmin, cpfmin(t1, t2));
126 tmax = cpfmin(tmax, cpfmax(t1, t2));
130 if(a.y < bb.b || bb.t < a.y) return INFINITY;
132 cpFloat t1 = (bb.b - a.y)/delta.y;
133 cpFloat t2 = (bb.t - a.y)/delta.y;
134 tmin = cpfmax(tmin, cpfmin(t1, t2));
135 tmax = cpfmin(tmax, cpfmax(t1, t2));
138 if(tmin <= tmax && 0.0f <= tmax && tmin <= 1.0f){
139 return cpfmax(tmin, 0.0f);
145 /// Return true if the bounding box intersects the line segment with ends @c a and @c b.
146 static inline cpBool cpBBIntersectsSegment(cpBB bb, cpVect a, cpVect b)
148 return (cpBBSegmentQuery(bb, a, b) != INFINITY);
151 /// Clamp a vector to a bounding box.
153 cpBBClampVect(const cpBB bb, const cpVect v)
155 return cpv(cpfclamp(v.x, bb.l, bb.r), cpfclamp(v.y, bb.b, bb.t));
158 /// Wrap a vector to a bounding box.
160 cpBBWrapVect(const cpBB bb, const cpVect v)
162 cpFloat dx = cpfabs(bb.r - bb.l);
163 cpFloat modx = cpfmod(v.x - bb.l, dx);
164 cpFloat x = (modx > 0.0f) ? modx : modx + dx;
166 cpFloat dy = cpfabs(bb.t - bb.b);
167 cpFloat mody = cpfmod(v.y - bb.b, dy);
168 cpFloat y = (mody > 0.0f) ? mody : mody + dy;
170 return cpv(x + bb.l, y + bb.b);
173 /// Returns a bounding box offseted by @c v.
175 cpBBOffset(const cpBB bb, const cpVect v)