Whats the best way to calculate the bounding box around a group of DisplayObjects?

I have an array of Sprites on stage and want to now the rectangle that bounds them. I could do a loop and check each element's getBounds() but that could be computationally expensive. Is there a better way to do it?

-------------Problems Reply------------

You could put them all inside a Sprite, and get the bounding box of that sprite.

You could spread the load for this operation over time, by maintaining two more arrays, one for pixel rows, and one for pixel columns, so for an 800x600 stage, you'd have an xBounds array of 800 elements, and a yBounds array of 600 elements.

Each element of each array would be an array of references to all sprites that have a bound that falls on that pixel (or null, if none use that column/row). So, each sprite would have two entries in each array; one for min x bound, one for max.

Any time you move, resize, rotate, or otherwise change the bounds of a sprite, you'd then have to update those lists by removing the old values of min and max from each of your two lists, and adding them in at the new positions.

This way, your overall bounds for all sprites on screen are simply the lowest and highest used indexes in each of those two arrays.

For extra speed, you would most likely also want to store the min and max values for x and y, and only change them if the sprite action would change them. So, if a sprite is placed below the minimum or above the max, you could update the value with a simple assignment to the new min or max; and if the sprite was the only element in the array for the current min column, and was moved higher, then you'd loop from the old min, up to the sprite's new position, and set the new min to the first occupied array element (and the same but opposite for the max column).

Using arrays, this solution would use a couple of kb of memory, and occasionally requires looping over part of the array. More optimally, you could replace the arrays with a "sorted list" class: probably not worth developer/maintainer time for the additional complexity, but using some form of self-balancing binary tree might add a little speed and save a bit of memory, which could matter if you had many sprites, moving often, with a large screen, a slow CPU, and little memory.

Either way, be sure to handle the case where the arrays are empty because no sprites are displayed.

var child:DisplayObject = ...
var parent:DisplayObject = new Sprite(); // should be the parent object.
var bounds:Rectangle = child.getBounds(parent)

See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#getBounds()

Category:actionscript 3 Views:1 Time:2010-01-27

Related post

  • How to calculate the bounding box for a given lat/lng location? 2008-10-26

    I have given a location defined by latitude and longitude. Now i want to calculate a bounding box within e.g. 10 kilometers of that point. The bounding box should be defined as latmin, lngmin and latmax, lngmax. I need this stuff in order to use the

  • How to calculate the bounding box of a CATextLayer's string? 2012-03-02

    At first sight my question looks really simple, but it seems that I really can't find solution. Here is what it is: I want to calculate the bounding box of a CATextLayer's string. Here is what I do: CATextLayer *textLayer = [CATextLayer layer]; textL

  • how to calculate the bounding box enclosing a set of mysql geometries? 2012-04-22

    I have a mysql table containing a set of shapes. The table is defined like this: +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+

  • Calculate the bounding box of a GDI+ drawing 2009-11-22

    I am drawing an image from MetaFile (emf) and then apply some rotation transformations to it all within the OnPaint of a UserControl. After applying those transformation how can I calculate the normal untransformed rectangular bounding box of this in

  • PHP Library: Calculate a bounding box for a given lat/lng location 2010-04-13

    I'm looking for a PHP Library / PHP Script that allows me to calculate an accurate bounding box for a given center point (lat/lon). Using an ellipsoid formula (f.ex. WGS84) would be great. I know, there have to be a library but I'm not able to find o

  • How to calculate a bounding box for an Expander? 2010-07-05

    I have an Expander control and i need to calculate its bounds without invisible elements and margins. It commonly can be done by VisualTreeHelper.GetDescendantsBounds. But it seems that the rect is calculated by VisualTreeHelper doesn't depend on the

  • How i can calculate 2d bounding box with 3d transformation 2010-07-27

    I'm working on opengl project. I set up perspective projection and render a transformed rectangle (rotated, scaled) How i can calculate rectangle's bounding box (rectangle position,size) Thank you --------------Solutions------------- You'd run the re

  • Calculate the bounding box's X, Y, Height and Width of a rotated element via JavaScript 2012-05-01

    Basically I'm asking this question for JavaScript: Calculate Bounding box coordinates from a rotated rectangle, Picture inside In this case: iX = Width of rotated (blue) HTML element iY = Height of rotated (blue) HTML element bx = Width of Bounding B

  • How to calculate the bounding box around the touched map position? 2015-02-11

    In my Android application I want to request data for the location where the user touches the map. GoogleMap.OnMapClickListener provides the touched position as latitude and longitude coordinates. public abstract void onMapClick(LatLng point) In order

  • CoreText. How Do I Calculate the Bounding Box of an Attributed String? 2011-08-08

    In CoreText it is easy ask: "for a given rectangle how much of this attributed string will fit?". CTFrameGetVisibleStringRange(rect).length Will return where in the string the next run of text should begin. My question is: "given an attributed string

  • How do you calculate the axis-aligned bounding box of an ellipse? 2008-09-17

    If the major axis of the ellipse is vertical or horizontal, it's easy to calculate the bounding box, but what about when the ellipse is rotated? The only way I can think of so far is to calculate all the points around the perimeter and find the max/m

  • Given a latitude and longitude, and distance, I want to find a bounding box 2009-10-30

    Given a latitude and longitude, and distance, I want to find a bounding box where the distances are less than the given distance. This questions was asked here: http://stackoverflow.com/questions/238260/how-to-calculate-the-bounding-box-for-a-given-l

  • Geo Spacial Bounding Box Rectangle Calculation Error: Latitude Incorrect 2010-03-09

    Can any trig or GPS experts help me out here? I'm trying to create a geo-spacial bounding box (rectangle) calculation returning the maximum latitude and longitude using the following method that I've retrieved. I am calling the method once for each o

  • Calculating the bounding box using Javascript 2010-06-29

    I have a latitude/longitude value and distance value. I need to calculate a bounding box with the given location as the center. so if the distance was 200 meters then the rectangle box should be 200 meters in front, behind, to left and right. How do

  • googlemaps get bounding box around marker 2011-03-07

    In googlemaps api v2 I have one marker on map and i need to calculate a bounding box around this one. How would I get a bonding box of 5 by 5 kilometers of which this marker is the center? --------------Solutions------------- I'm not sure that such a

  • Calculating the bounding box for geographic coordinates 2012-01-09

    Given a list of coordinates, how do I calculate the minimum bounding rectangle (MBR), avoiding the global gotchas described in the Unlocking the Mysteries of the Bounding Box? Google Maps API method fitBounds() seems to be handling the gotchas well.

  • Getting the bounding box of a vector of points? 2012-01-30

    I have a vector of points stored in a std::vector instance. I want to calculate the bounding box of these points. I've tried with this code: bool _compare1(ofPoint const &p1, ofPoint const &p2) { return p1.x < p2.x && p1.y < p2.

  • How to correctly get the bounding box using LocationRect.fromLocations() when locations span 180th meridian? 2012-03-02

    I'm using the v7 Bing Maps Javascript "control" (I don't know why it's called a "control"...). I'm calling Microsoft.Maps.Map.setView({bounds: bounds}) and it isn't working as I would expect or desire. I have a set of Polygons with points that span t

  • Finding the bounding box of the difference between 2 images? 2012-03-10

    I have 2 bitmap images, where 1 is a slight variation of the other. Now I'd like to calculate the bounding box of the area-of-change, as fast as possible. Is there a smart algorithm to do that or is it just a case of brute-force processing ? Edit: th

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.098 (s). 11 q(s)