<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Calculating bounding rectangle of rotation</title>
		<link>http://www.allegro.cc/forums/view/589847</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 04 Feb 2007 06:39:50 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi there, I&#39;ve been scratching my head over this a little and I thought someone might know the answer to this, my trigs a little rusty...</p><p>I want to know what the measurements would be (ie. how to calculate) a bounding rectangle around a rotated rectangle.</p><p>A pictures worth 1000 words.... <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>http://www.angelfire.com/funky/feartheweasel/pics/Rotate.jpg</p><p>Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Fri, 02 Feb 2007 05:47:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>One way could be to rotate the 4 points of the rectangle, then get the min and max values of x and y, and then compute the bounding rectangle width and height.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (vbovio)</author>
		<pubDate>Fri, 02 Feb 2007 05:54:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I recommend the same approach. Find the corners of the rectangle to enclose and use the smallest XY values for the top-left corner of the bounding rectangle, and the largest XY values for the bottom-right corner.</p><p>However, I&#39;m a little perplexed as to why you want to use a bounding rectangle instead of a bounding circle. A bounding circle is much easier to calculate. All it takes is a little Pythagorean Theorem magic. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 02 Feb 2007 19:21:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Would this work?
</p><div class="source-code snippet"><div class="inner"><pre>size <span class="k3">=</span> MAX<span class="k2">(</span>w, h<span class="k2">)</span><span class="k2">;</span>
new_w <span class="k3">=</span> ABS<span class="k2">(</span>size <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>a<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
new_h <span class="k3">=</span> ABS<span class="k2">(</span>size <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>a<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Fri, 02 Feb 2007 19:24:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
One way could be to rotate the 4 points of the rectangle
</p></div></div><p>Better, for rectangles, is to take two points on one side, since you can mirror the others across the fulcrum. This should save you two sin/cos calls.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Fri, 02 Feb 2007 19:50:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
This should save you two sin/cos calls.
</p></div></div><p>
If the angles are whole numbers (integers), then these &#39;expensive&#39; sin/cos calls could be turned into a lookup table (array).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Fri, 02 Feb 2007 20:00:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, but using two out of four points still saves you 4 lookups <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Fri, 02 Feb 2007 20:06:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can also multiply the values of the sin/cos in the table by 128, then just &gt;&gt;7 the number after multiplication. This saves on use of floating point math. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>(note: I have been working with this stuff [rotation] for a couple months, so I have learned much <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Fri, 02 Feb 2007 23:53:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Cheers, I&#39;ve got it doing what I want now.  I was mainly trying to cut down on calculating all four points and just doing two.  In the meantime I also constructed some lookup tables.  I shouldn&#39;t be worrying about speed to much, it&#39;s only really for previewing things accurately. (see below)</p><p>Cheers for this pointer Kikaru:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Kikaru: You can also multiply the values of the sin/cos in the table by 128, then just &gt;&gt;7 the number after multiplication. This saves on use of floating point math. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div></div><p>

I was just thinking about doing something like this to help accuracy...</p><p>The reason I&#39;m not using a bounding circle is that this is for a quick routine I wrote to draw a transparent rotated sprite in standard Allegro (only has trans_sprite OR rotate_sprite....).  My game uses Openlayer (or Allegro if you want about half the framerate, wasn&#39;t hard to do both) but my Animation editor can only use Allegro because I use the Allegro GUI.</p><p>Here&#39;s a pic anyway if anyones interested.  It&#39;s all based on 2d skeletal animation with interpolation between frames (can&#39;t see animation in a screenshot though...).  Each bone supports it&#39;s own skin animations too (the flower bobs up and down in a happy way).</p><p>All the programming for the game is nearly done, just starting to draw graphics and make other game content now.</p><p>BTW I&#39;m not sure what&#39;s up with the guy being a cactus, I just drew the skins quickly for testing... <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>http://www.angelfire.com/funky/feartheweasel/pics/GRIM_ScreenShots_3_2_2007.jpg
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Sat, 03 Feb 2007 05:58:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>[off-topic]<br />Where can I download GRIM?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Sat, 03 Feb 2007 06:19:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I like the cactus.  You should make it a bonus special character.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 03 Feb 2007 06:23:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Haha yeah, &#39;Bonus Character!&#39; is what I thought too when I chucked the skin on the bones!</p><p>As for downloading I guess I could put the GRIM editor online.  I don&#39;t imagine it would be much use for anyone other than having a look and a play but I don&#39;t mind.  I&#39;ll start a thread in the proper place...</p><p>EDIT:  Download available in <a href="http://www.allegro.cc/forums/thread/589864">this thread</a> if anyones interested. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Sat, 03 Feb 2007 07:56:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s the kind of thing that I&#39;m working on too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Sat, 03 Feb 2007 14:43:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Cool!  I&#39;ve wanted to do this for a while and had a brief tech demo going mid last year but never did anything more for 6 months.  I completely rewrote the first attempt into a proper extendible structure and started the editor last week.  If you want to compare notes or anything I don&#39;t mind at all!  Something I&#39;ve learnt might help you out or vice versa.  How are you handling skin drawing order?</p><p>Matt.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Sat, 03 Feb 2007 16:43:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How are you handling skin drawing order?
</p></div></div><p>
Whatever the furthest is from the camera is drawn first. Use glDepthFunc(GL_LEQUAL) to draw things in order (like as if the bitmaps would do it).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Sat, 03 Feb 2007 18:11:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Cool.  I meant more in terms of structures, it was a bit of a problem I had originally because I use a linked list tree structure to hold all the bones with their lengths and relative angles to each other.  I originally just drew the skins as the bone position was calculated when iterating through the tree but of course the order wasn&#39;t perfect all the time.  In the end I made a seperate linear linked list of pointers to the bones which is the draw order, it makes it easy to change.  Under the &#39;Bones&#39; section in my editor is actually the list of all the bones where you can click and drag the order all around.</p><p>Screenshot:<br /><span class="remote-thumbnail"><span class="json">{"name":"591141","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/1\/c14d25c3004e231b1d8b637b779353ab.jpg","w":541,"h":422,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/1\/c14d25c3004e231b1d8b637b779353ab"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/c/1/c14d25c3004e231b1d8b637b779353ab-240.jpg" alt="591141" width="240" height="187" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Weir)</author>
		<pubDate>Sun, 04 Feb 2007 03:26:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Since you said &quot;bounding circle,&quot; I remembered a neat little trick for copying that:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//for collision at a known distance</span>
<span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>x_diff <span class="k3">&lt;</span> d1<span class="k2">)</span><span class="k3">&amp;</span><span class="k3">&amp;</span><span class="k2">(</span>y_diff <span class="k3">&lt;</span> d1<span class="k2">)</span><span class="k3">&amp;</span><span class="k3">&amp;</span><span class="k2">(</span>x_diff <span class="k3">+</span> y_diff <span class="k3">&lt;</span> d2<span class="k2">)</span><span class="k2">)</span>
     hit <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
</pre></div></div><p>
This creates a &quot;bounding octagon,&quot; which works a bit like a circle.</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//for estimating distances</span>
<span class="k1">if</span> <span class="k2">(</span>x_diff <span class="k3">&gt;</span> y_diff<span class="k2">)</span>
     dist <span class="k3">=</span> <span class="k2">(</span>x_diff<span class="k3">+</span>x_diff<span class="k3">+</span>y_diff<span class="k2">)</span><span class="k3">&gt;</span><span class="k3">&gt;</span><span class="n">2</span><span class="k2">;</span>
<span class="k1">else</span>
     dist <span class="k3">=</span> <span class="k2">(</span>x_diff<span class="k3">+</span>y_diff<span class="k3">+</span>y_diff<span class="k2">)</span><span class="k3">&gt;</span><span class="k3">&gt;</span><span class="n">2</span><span class="k2">;</span>
</pre></div></div><p>

this is a fairly accurate distance check, least accurate around 45<sup>o</sup> or so. Best for checking &quot;which is closer&quot; than actual distances. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Sun, 04 Feb 2007 06:39:50 +0000</pubDate>
	</item>
</rss>
