diff --git a/src/webgl/p5.Geometry.js b/src/webgl/p5.Geometry.js index b72c5b895e..ed48495b4d 100644 --- a/src/webgl/p5.Geometry.js +++ b/src/webgl/p5.Geometry.js @@ -24,6 +24,9 @@ p5.Geometry = class Geometry { //@type [p5.Vector] this.vertices = []; + this.boundingBoxCache = null; + + //an array containing every vertex for stroke drawing this.lineVertices = new p5.DataArray(); @@ -74,6 +77,112 @@ p5.Geometry = class Geometry { } } + /** + * Custom bounding box calculation based on the object's vertices. + * The bounding box is a rectangular prism that encompasses the entire object. + * It is defined by the minimum and maximum coordinates along each axis, as well + * as the size and offset of the box. + * + * It returns an object containing the bounding box properties: + * + * - `min`: The minimum coordinates of the bounding box as a p5.Vector. + * - `max`: The maximum coordinates of the bounding box as a p5.Vector. + * - `size`: The size of the bounding box as a p5.Vector. + * - `offset`: The offset of the bounding box as a p5.Vector. + * + * @method calculateBoundingBox + * @memberof p5.Geometry.prototype + * @returns {Object} + * + * @example + * + *
+ * let particles;
+ * let button;
+ * let resultParagraph;
+ *
+ * function setup() {
+ * createCanvas(100, 100, WEBGL);
+ * button = createButton('New');
+ * button.mousePressed(makeParticles);
+ *
+ * resultParagraph = createElement('pre').style('width', '200px' );
+ * resultParagraph.style('font-family', 'monospace');
+ * resultParagraph.style('font-size', '12px');
+ * makeParticles();
+ * }
+ *
+ * function makeParticles() {
+ * if (particles) freeGeometry(particles);
+ *
+ * particles = buildGeometry(() => {
+ * for (let i = 0; i < 60; i++) {
+ * push();
+ * translate(
+ * randomGaussian(0, 200),
+ * randomGaussian(0, 100),
+ * randomGaussian(0, 150)
+ * );
+ * sphere(10);
+ * pop();
+ * }
+ * });
+ *
+ * const boundingBox = particles.calculateBoundingBox();
+ * resultParagraph.html('Bounding Box: \n' + JSON.stringify(boundingBox, null, 2));
+ * }
+ *
+ * function draw() {
+ * background(255);
+ * noStroke();
+ * lights();
+ * orbitControl();
+ * model(particles);
+ * }
+ *
+ *
+ *