2010-07-25 134 views
2

我與代碼玩弄這樣的:如何獲得旋轉組件的視覺寬度和高度?

<s:Button id="test" label="test" transformX="{Math.floor(test.width/2)}" rotationY="20" x="20" y="20" /> 

按鈕旋轉Y軸和旋轉軸心點是在按鈕的中間。

這將創建一個按鈕,看起來是這樣的:

alt text http://www.jeffryhouser.com/images/StackOverflowQuestions/StackOverflow3DWidthHeight.png

的旋轉按鈕,視覺,填充比X,Y,高度不同的空間,和寬度值會怎麼相信。

我圖像中的「A」值是按鈕的高度。但是,我想用於計算和放置目的的是B值。

此外,我想執行與寬度類似的計算;獲得從右上角到左下角的寬度。

我該怎麼做?


我把一個sample放在一起來展示計算這種人們建議的各種方法。源代碼也是available。沒有任何事情像我所期望的那樣工作。例如,將rotationSlider旋轉到85.該按鈕實際上是不可見的,但所有的方法仍然給它的高度和寬度。

+0

可能重複的[從旋轉的矩形計算邊界框座標,圖片在裏面。](http://stackoverflow.com/questions/622140/calculate-bounding-box-coordinates-from-a-rotated-rectangle-picture -inside) – 2010-07-25 01:21:27

+0

它確實聽起來相似;但我不明白任何答案;這個問題早於Flex 4和我正在使用的「transformX」和「rotationY」屬性。 – JeffryHouser 2010-07-25 01:43:37

+0

額外的Flex 4功能聽起來是保持這個問題開放的好理由。 – 2010-07-25 01:46:30

回答

0

工作對我來說,答案是從詹姆斯·沃德在這個問題上的意見之一,位於該blog post

博客文章沒有說的一件事是,在許多情況下,相關類的t​​ransform屬性的perspectiveProjection屬性將爲null。鏈接到示例通過將maintainProjectionCenter屬性設置爲true來處理此問題。但是,你也可以創建一個新的PerspectiveProjection對象是這樣的:

object.transform.perspectiveProjection = new PerspectiveProjection(); 

我包裹起來,從evtimmy功能成一個類:

/** 
* DotComIt/Flextras 
* Utils3D.as 
* Utils3D 
* jhouser 
* Aug 5, 2010 
*/ 
package com.flextras.coverflow 
{ 
    import flash.geom.Matrix3D; 
    import flash.geom.PerspectiveProjection; 
    import flash.geom.Rectangle; 
    import flash.geom.Utils3D; 
    import flash.geom.Vector3D; 

    public class TransformUtilities 
    { 
     public function TransformUtilities() 
     { 
     } 


    //-------------------------------------------------------------------------- 
    // 
    // Methods 
    // 
    //-------------------------------------------------------------------------- 

    //---------------------------------- 
    // projectBounds 
    //---------------------------------- 
    // info from 
    // http://evtimmy.com/2009/12/calculating-the-projected-bounds-using-utils3dprojectvector/ 

    /** 
    * Method retrieved from 
    * http://evtimmy.com/2009/12/calculating-the-projected-bounds-using-utils3dprojectvector/ 
    * 
    * @param bounds: The rectangle that makes up the object 
    * @param matrix The 3D Matrix of the item 
    * @param the projection of the item's parent. 
    */ 
    public static function projectBounds(bounds:Rectangle, 
             matrix:Matrix3D, 
             projection:PerspectiveProjection):Rectangle 
    { 
     // Setup the matrix 
     var centerX:Number = projection.projectionCenter.x; 
     var centerY:Number = projection.projectionCenter.y; 
     matrix.appendTranslation(-centerX, -centerY, projection.focalLength); 
     matrix.append(projection.toMatrix3D()); 

     // Project the corner points 
     var pt1:Vector3D = new Vector3D(bounds.left, bounds.top, 0); 
     var pt2:Vector3D = new Vector3D(bounds.right, bounds.top, 0) 
     var pt3:Vector3D = new Vector3D(bounds.left, bounds.bottom, 0); 
     var pt4:Vector3D = new Vector3D(bounds.right, bounds.bottom, 0); 
     pt1 = Utils3D.projectVector(matrix, pt1); 
     pt2 = Utils3D.projectVector(matrix, pt2); 
     pt3 = Utils3D.projectVector(matrix, pt3); 
     pt4 = Utils3D.projectVector(matrix, pt4); 

     // Find the bounding box in 2D 
     var maxX:Number = Math.max(Math.max(pt1.x, pt2.x), Math.max(pt3.x, pt4.x)); 
     var minX:Number = Math.min(Math.min(pt1.x, pt2.x), Math.min(pt3.x, pt4.x)); 
     var maxY:Number = Math.max(Math.max(pt1.y, pt2.y), Math.max(pt3.y, pt4.y)); 
     var minY:Number = Math.min(Math.min(pt1.y, pt2.y), Math.min(pt3.y, pt4.y)); 

     // Add back the projection center 
     bounds.x = minX + centerX; 
     bounds.y = minY + centerY; 
     bounds.width = maxX - minX; 
     bounds.height = maxY - minY; 
     return bounds; 
    } 

    } 

} 

雖然這是回答我的問題,我不知道這是否是我的問題的解決方案。感謝大家!

1

我的數學可能有點生疏,但我這是怎麼會找到答案:

你會從按鈕的右邊緣延伸的直角三角形,以圖的最底部點你有(AB)。然後你可以使用正弦律得到三個角度:90',20'和70'(90將始終在那裏,然後是你的變量 - 第三個角度爲180)。

然後,您可以用下面的公式來找到答案:

B = ((button.width * sin(button.rotationY))/(sin(90 -button.rotationY)) + (button.height) 
+0

這並沒有給我所期望的。如果rotationY爲零,則工作正常;但除此之外,事情會變得混亂。我整理了一個樣本來展示人們提出的各種方法: http://www.flextras.com/labs/DisplaySize/DisplaySize.html – JeffryHouser 2010-07-27 12:49:46

1

的getBounds(..)和getRect(..)被認爲是用於獲取寬度和變換對象的高度的方法。

沒試過他們的Flex 4還沒有,但他們總是在Flex 3中

+0

在紙上;這聽起來正是我正在尋找的。但是,他們似乎沒有像我期望的那樣工作。 沒有轉換。寬度是70和高度21. var bounds1:Rectangle = test2.getRect(this.test2); bouinds1的寬度是72,高度是23.如果我開始旋轉項目;結果似乎更不正確。 – JeffryHouser 2010-07-27 11:50:57

+0

我把一個樣本放在一起來炫耀人們提出的各種方法: http://www.flextras.com/labs/DisplaySize/DisplaySize.html – JeffryHouser 2010-07-27 12:50:33

+0

James發佈的博客看起來很有前景,但是真的我認爲它有一個bug getBounds未返回正確的邊界... – 2010-07-27 12:56:18

相關問題