Calculating AABB of a Rotated 2D Rectangle

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

22:54
Wed
02
Jun 2010

Calculating AABB of a Rotated 2D Rectangle

Today I've solved an interesting geometrical problem of calculating an AABB rectangle (Axis-Aligned Bounding Box) around an oriented rectangle (rotated by an angle) in 2D.

It seems simple but it's easy to come up with suboptimal solution like calculating all 4 corners of the rotated rectangle or calling sin and cos functions multiple times. So I've coded a simple demonstration in ActionScript 3 (Flash). Here is my algorithm:

Input data:

```// Center position of the rectangle.
private const m_PosX : Number, m_PosY : Number;
// Half-width and half-height of the rectangle.
private const m_HalfSizeX : Number, m_HalfSizeY : Number;
private var m_Orientation : Number;```

Algorithm:

```// corner_1 is right-top corner of unrotated rectangle, relative to m_Pos.
// corner_2 is right-bottom corner of unrotated rectangle, relative to m_Pos.
var corner_1_x : Number = m_HalfSizeX;
var corner_2_x : Number = m_HalfSizeX;
var corner_1_y : Number = -m_HalfSizeY;
var corner_2_y : Number =  m_HalfSizeY;

var sin_o : Number = Math.sin(m_Orientation);
var cos_o : Number = Math.cos(m_Orientation);

// xformed_corner_1, xformed_corner_2 are points corner_1, corner_2 rotated by angle m_Orientation.
var xformed_corner_1_x : Number = corner_1_x * cos_o - corner_1_y * sin_o;
var xformed_corner_1_y : Number = corner_1_x * sin_o + corner_1_y * cos_o;
var xformed_corner_2_x : Number = corner_2_x * cos_o - corner_2_y * sin_o;
var xformed_corner_2_y : Number = corner_2_x * sin_o + corner_2_y * cos_o;

// ex, ey are extents (half-sizes) of the final AABB.
var ex : Number = Math.max(Math.abs(xformed_corner_1_x), Math.abs(xformed_corner_2_x));
var ey : Number = Math.max(Math.abs(xformed_corner_1_y), Math.abs(xformed_corner_2_y));

var aabb_min_x : Number = m_PosX - ex;
var aabb_max_x : Number = m_PosX + ex;
var aabb_min_y : Number = m_PosY - ey;
var aabb_max_y : Number = m_PosY + ey;```

AabbTest01.swf - online Flash demo
AabbTest01.zip - full project source code

I believe it can be optimized further (not only by rewriting it in C++ :) Anyone knows how?

 ewqr2015-04-14 11:08:03 chanel handbags, http://www.chanelhandbags.net.co/
flat iron, http://www.chiflatiron.net.co/
converse, http://www.converse.net.co/
salvatore ferragamo, http://www.ferragamo.com.co/
gucci shoes, http://www.guccihandbags.com.co/
hollister clothing, http://www.hollister.us.org/
insanity workout, http://www.insanityworkout.net.co/
jimmy choo, http://www.jimmychoo.net.co/
jordan retro, http://www.jordan-shoes.com.co/
juicy couture outlet, http://www.juicycouture.com.co/
juicy couture handbags, http://www.juicycoutureoutlet.net.co/
louis vuitton outlet online, http://www.louisvuitton-outlet.com.co/
michael kors, http://www.michael-kors.us.org/
michael kors outlet online, http://www.michael-kors-handbags.us.com/
michael kors purses, http://www.michael-kors-outlet.us.org/
michael kors outlet online sale, http://www.michael-kors-outlet-online.us.org/
michael kors outlet store, http://www.michaelkorsoutletonline-sale.us.com/
north face backpacks, http://www.northface.us.org/
oakley sunglasses, http://www.oakley-sunglasses.us.org/
pandora jewelry, http://www.pandorajewelry.com.co/
ray ban outlet, http://www.ray-ban-outlet.us.com/
ray-ban sunglasses, http://www.rayban-sunglasses.us.org/
rolex watches for sale, http://www.replicawatches.us.com/
swarovski crystal, http://www.swarovskicrystal.com.co/
north face outlet, http://www.thenorthface.us.org/
tiffany and co, http://www.tiffanyandco.net.co/
tiffany jewelry, http://www.tiffanyjewelry.us.org/
true religion jeans, http://www.true-religion.com.co/
true religion, http://www.truereligionjeans.net.co/
ugg, http://www.uggaustralia.net.co/
ugg australia, http://www.uggboots.net.co/
uggs, http://www.uggbootsclearance.com.co/
ugg boots clearance, http://www.uggsonsale.com.co/
uggs on sale, http://www.uggsoutlet.com.co/
oakley vault, http://www.cheap-oakleysunglasses.in.net/
oakley outlet, http://www.oakleysunglassescheap.in.net/
ralph lauren outlet, http://www.ralphlaurenoutletonline.in.net/
polo ralph lauren outlet, http://www.ralphlauren--outlet.in.net/
burberry handbags, http://www.burberryoutlet--online.in.net/
tom's shoes, http://www.tomshoes.in.net/
toms shoes outlet, http://www.toms-outlet.in.net/
christian louboutin, http://www.christianlouboutin.in.net/
tory burch shoes, http://www.tory-burch-outlet.in.net/
gucci shoes, http://www.gucci-outlet.in.net/
long champ, http://www.longchamphandbagsoutlet.in.net/
abercrombie fitch, http://www.abercrombie-kids.in.net/
hollisterco, http://www.hollisterclothing-store.in.net/
hermes bags, http://www.hermes.in.net/
nba jerseys, http://www.cheapjerseys.in.net/
beats by dr dre, http://www.beats-by-dre.in.net/
straighteners, http://www.ghdhairstraightener.in.net/
ray bans, http://www.rayban-sunglasses.org.uk/
michael kors outlet, http://www.michaelkors-uk.org.uk/
michael kors bags, http://www.michaelkorshandbags.org.uk/
louis vuitton uk, http://www.louis--vuitton.org.uk/
ralph lauren uk, http://www.ralphlauren-outlet.org.uk/
nike free run, http://www.nikefree-run.org.uk/
replica watches, http://www.rolex-watches.me.uk/
wedding dress, http://www.weddingdressesuk.org.uk/
pandora rings, http://www.pandora-charms.org.uk/
thomas sabo uk, http://www.thomassabo-uk.org.uk/
swarovski uk, http://www.swarovski-uk.org.uk/
louboutin, http://www.christianlouboutin.org.uk/
air huarache, http://www.air-huarache.co.uk/
nike roshe, http://www.rosherun.org.uk/
the north face, http://www.thenorth-face.org.uk/
louis vuitton, http://www.louis--vuitton.ca/
air jordan retro, http://www.airjordans.us/
air jordan retro, http://www.jordanretro.org/
air jordan, http://www.cheap-jordans.net/
nike shoes, http://www.nikefactory.org/
nike outlet, http://www.nikestore.us/
nike outlet store, http://www.cheap-nikeshoes.com/
air max 2014, http://www.nike-air-max.us/
nike air max, http://www.airmax-2015.org/
nike air max 2014, http://www.airmax-90.org/
nike running shoes, http://www.nikefree-run.net/
free running, http://www.nikefree5.net/
supra footwear, http://www.suprashoe.net/
new balance store, http://www.newbalance-outlet.org/
mont blanc pens, http://www.montblanc--pens.net/
ferragamo shoes, http://www.ferragamoshoes.net/
mcm backpack, http://www.mcm-bags.net/
bottega veneta, http://www.bottega.us/
soccer shoes, http://www.nikemercurial.net/
celine bags, http://www.celinebags.org/
nike roshe, http://www.rosheruns.us/
nike roshe run, http://www.nikerosherun.us/
vans outlet, http://www.vansshoes.us/
timberland shoes, http://www.timberland-shoes.com/
iphone 5s cases, http://www.iphone-cases.us/
baseball bats, http://www.softball-bats.us/
babyliss, http://www.babyliss-pro.net/
louis vuitton outlet stores, http://www.louisvuitton-outlets.us/
burberry, http://www.burberry-outlet.jp.net/
red bottom shoes, http://www.louboutin.jp.net/
louboutin shoes, http://www.christianlouboutinshoes.jp.net/
louis vuitton outlet online, http://www.louisvuitton.jp.net/
michael kors outlet online, http://www.michaelkorsclance.com/
michael kors clearance, http://www.official-michaelkorsonline.com/
tommy hilfiger bags, http://www.tommyhilfiger.in.net/
mcm bags, http://www.mcmworldwide.ca/
moncler sale, http://www.mmoncler-outlet.com/
barbour outlet, http://www.barbour-factory.com/
ferragamo outlet, http://www.ferragamos.in.net/
louis vuitton outlet stores, http://www.louisvuittonas.com/
burberry sale, http://www.burberry-outlet2015.com/
calvin klein clothes, http://www.fashion-clothing.us.com/
north face outlet, http://www.thenorthface.so/
barbour jacket outlet, http://www.barbour-jacketsoutlet.com/
north face outlet, http://www.northfaceoutlet.in.net/
north clearance, http://www.northclearance.com/
burberry outlet online, http://www.burberrysfactory.com/
polo ralph lauren, http://www.ralph-laurenuk.com/
ralph lauren outlet online, http://www.polo-outlets.com/
, http://www.ralphlaurenr.com/
monster beats outlet, http://www.monsterbeatsbydres.net/
louis vuitton outlet, http://www.louis-vuittonblackfriday.com/
gucci shoes uk, http://www.lv-guccishoesfactory.com/
longchamp handbags, http://www.longchampsoutlet.com/
moncler coats, http://www.moncler-clearance.com/
polo ralph lauren outlet, http://www.ralphlaurentshirts.com/
gucci handbags, http://www.usahanbags.com/
beats by dre sale, http://www.beatsbydreoutlet.net/
coach outlet store online, http://www.coach-blackfriday2014.com/
coach outlet store, http://www.coachccoachoutlet.com/
coach clearance, http://www.coach-clearance.com/
michael kors handbags, http://www.michaelkors.in.net/
coach factory online, http://www.coach-factories.net/
oakley, http://www.oakley.so/
hermes birkin, http://www.hermes-outletonline.com/
marc by marc jacobs, http://www.marcjacobsonsale.com/
air jordan shoes, http://www.nike-jordanshoes.com/
woolrich jackets, http://www.woolrich-clearance.com/
coach factory outlet, http://www.coachoutletew.com/
coach outlet store, http://www.coachoutlet.so/
coach usa, http://www.coachoutletstates.com/
gucci shoes, http://www.guccishoes-uk.com/
coach outlet store, http://www.official-coachoutlets.com/
michael kors sale, http://www.michaelkors.so/
gucci outlet, http://www.guccifactorys.com/
north face jackets, http://www.thenorthface.in.net/
uggs on sale, http://www.cheapuggboots.eu.com/
tommy hilfiger bags, http://www.tommy-hilfigeroutlet.com/
ugg boots clearance, http://www.uggboots.so/
louis vuitton purses, http://www.louisvuitton.so/
cheap calvin kleins, http://www.calvin-kleins.com/
louboutin shoes, http://www.cheap-christianlouboutinshoes.com/
retro jordans, http://www.airjordanshoes2015.com/
burberry, http://www.burberry.eu.com/