In this article, we will see how to get the bounding-box of different shapes. We will use P5.js which is a Javascript framework creative programming environment and is very much inspired by Processing.
Bounding-box: A bounding-box is basically just a rectangle that bounds a shape more technically it is the rectangle with the smallest possible surface area that bounds the shape. A bounding-box can have rotation (called global bounding-box) but in this article, we’ll be focusing around the axis-aligned bounding boxes (AABB shapes) which have zero rotation in them (also called local bounding-box)
Note: A Bounding-Box for a shape is the rectangle with the smallest possible surface area that bounds the shape.
Reason to calculate bounding-box: A bounding-box acts as a container of a shape and has several applications in graphical applications (most notably used by GUI libraries for widget-masks). Since a bounding-box contains the shape so if any other shape doesn’t intersect with the bounding-box then it also doesn’t intersect with the inner-shape thus bounding-boxes are used heavily in Physics engines (such as Box2D) for broad-phase collision detection.
Base for the p5.js: This is the base-code (normally for every p5.js code).
<!-- Our main HTML file! --><html> Â Â <head> Â Â Â Â <script src="sketch.js"></script> Â Â </head> Â Â <body> Â Â </body> </html> |
Note: We will only change script.js at every iteration, and the HTML file will necessarily remain intact!
- Finding bounding-box of an ellipse:
/* p5.js Sketch for finding and drawing  Âbounding-box of an ellipse*/functionsetup(){ ÂcreateCanvas(480, 360);}ÂÂ// Draws bounding-box around the// given ellipse!functiondrawBBox(x0, y0, r1, r2){ Â// Draw only the outline Â// of the rectangle ÂnoFill(); Â// Draw the outline in red Âstroke(255, 0, 0); Ârect(x0-r1, y0-r2, 2*r1, 2*r2);}functiondraw() { Âlet x0 = width/2, y0 = height/2; Âlet r1 = 180, r2 = 100; Â// Note that `ellipse` takes in Â// diameters not radii! Âellipse(x0, y0, 2*r1, 2*r2); ÂdrawBBox(x0, y0, r1, r2); Â// We don't want to draw this Â// over and over again ÂnoLoop();}Output:
- Finding bounding-box of a circle: It is same as an ellipse, since a circle is just a special case of an ellipse with same radii (same semi-major-axis and semi-minor axis).
- Finding bounding-box of a line-segment
/* p5.js Sketch for finding and drawing Âbounding-box of a line-segment*/functionsetup() { ÂcreateCanvas(480, 360);}ÂÂ// Draws bounding-box around the// given line-segment!functiondrawBBox(x1, y1, x2, y2) { Âstroke(255, 0, 0); ÂnoFill(); Âlet x = min(x1, x2), y = min(y1, y2); Âlet w = max(x1, x2) - x, h = max(y1, y2) - y; Ârect(x, y, w, h);}ÂÂfunctiondraw() { Âlet x1 = 280, y1 = 80, x2 = 180, y2 = 280; Âline(x1, y1, x2, y2); ÂdrawBBox(x1, y1, x2, y2); ÂnoLoop();}Output:
- Finding bounding-box of a triangle: Finding bounding-box of a triangle is very similar to finding bounding-box for line-segment.
/* p5.js Sketch for finding and drawing Âbounding-box of a triangle*/functionsetup() { ÂcreateCanvas(480, 360);}ÂÂ// Draws bounding-box around the// given triangle!functiondrawBBox(x1, y1, x2, y2, x3, y3) { Âstroke(255, 0, 0); ÂnoFill(); Âlet x = min(x1, x2, x3), y = min(y1, y2, y3); Âlet w = max(x1, x2, x3) - x, h = max(y1, y2, y3) - y; Ârect(x, y, w, h);}ÂÂfunctiondraw() { Âlet x1 = 240, y1 = 80, x2 = 140; Âlet y2 = 280, x3 = 340, y3 = 280; Âtriangle(x1, y1, x2, y2, x3, y3); ÂdrawBBox(x1, y1, x2, y2, x3, y3); ÂnoLoop();}Output:
- Finding bounding-box of a polygon: A triangle is a polygon, and if we find the bounding-box of a triangle then finding bounding-box for polygon shouldn’t be any difficulty. We just have to generalize so that we can have any number of vertices and we are done.
/* p5.js sketch for finding and drawing Âbounding-box of a polygon*/functionsetup() { ÂcreateCanvas(480, 360);}ÂÂ// Draws bounding-box around// the given polygon!functiondrawBBox(x, y) { Âstroke(255, 0, 0); ÂnoFill(); Âlet rx = min(x), ry = min(y); Âlet w = max(x) - rx, h = max(y) - ry; Ârect(rx, ry, w, h);}ÂÂfunctiondraw(){ Â/* Vertices for a star-polygon (decagon) */ Âlet x = [240, 268, 334, 286, 298,          Â240, 182, 194, 146, 212]; Âlet y = [80, 140, 150, 194, 260,          Â230, 260, 194, 150, 140]; ÂbeginShape(); Âfor(let i = 0; i < x.length; ++i)   Âvertex(x[i], y[i]); Âfill(255, 217, 0); Â// If you don't CLOSE it then it'd Â// draw a chained line-segment ÂendShape(CLOSE); ÂdrawBBox(x, y); ÂnoLoop();}Output:
Finding Bounding-Boxes is an important part of visualization applications. Also in dynamic applications such as games, one cannot compute capsule-collision detection at every frame without entailing a punishment in the performance. So before any complex collision-checking, a broad-phase check is made for early exit which returns false as soon as it is ascertained that the shape doesn’t collide with the other shape. If the broad-phase check is passed then comes the narrow-phase where the actual collision-detection (OOBB, SAT, capsule, ellipsoid, etc) happens! Hence finding the bounding-box is an important part of many graphics-rich applications for various reasons.

