English 中文(简体)
How do you find a tight-fitting, axis-aligned, bounding box of a rotated ellipse in AS3?
原标题:

The AS3 getBounds function returns a rectangle that is not fitting tightly to a rotated ellipse. Instead it returns an axis-aligned rectangle based on the bounds of a rectangle whose width/height corresponds to the max/min diameter of the ellipse and follows its rotation.

The answer to a similar question on Stack Overflow elegantly outlines the math part of my question:

Stack Overflow Q/A about the math of ellipses and bounding boxes

Based on this I took a stab at coding a solution in AS3. So far I have been able to produce a rectangle that fits perfectly along the x-axis, but as I rotate my ellipse it acts very weird along the y-axis. Rather than alternating between 2*r_min and 2*r_max while rotating, it alternates between 2*r_min and 0. My best guess is that I have done something wrong when solving the differentiated t for gradient -> infinity...

Here is an example from my code:

var r_max:uint = 45;
var r_min:uint = 20;
var rot:Number = ellipse.rotation * (Math.PI / 180);
var t_nil:Number = Math.atan( -r_min * Math.tan(rot) / r_max);
var t_inf:Number = Math.atan(r_min * Math.atan(rot) / r_max) + (Math.PI / 2);
var x:Number = r_max * Math.cos(t_nil) * Math.cos(rot) - r_min * Math.sin(t_nil) * Math.sin(rot);
var y:Number = r_min * Math.sin(t_inf) * Math.cos(rot) + r_max * Math.cos(t_inf) * Math.sin(rot);
最佳回答

I assumed atan(x) and cot(x) were interchangeable - turns out they re not. Who would ve thought ;)

My working AS3 code ended up looking like this:

var r_max:uint = 45;
var r_min:uint = 20;
var rot:Number = ellipse.rotation * (Math.PI / 180);
var t_nil:Number = Math.atan( -r_min * Math.tan(rot) / r_max);
var t_inf:Number = Math.atan(r_min * (Math.cos(rot) / Math.sin(rot)) / r_max);
var rect_width:Number = 2 * (r_max * Math.cos(t_nil) * Math.cos(rot) - r_min * Math.sin(t_nil) * Math.sin(rot));
var rect_height:Number = 2* (r_min * Math.sin(t_inf) * Math.cos(rot) + r_max * Math.cos(t_inf) * Math.sin(rot));
问题回答

暂无回答




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签