Created
December 1, 2011 03:42
-
-
Save Tomyail/1413357 to your computer and use it in GitHub Desktop.
以可视化的方式表现矩阵变幻
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import com.bit101.components.InputText; | |
import com.bit101.components.PushButton; | |
import flash.display.Shape; | |
import flash.display.Sprite; | |
import flash.display.Stage; | |
import flash.events.MouseEvent; | |
import flash.geom.Matrix3D; | |
import flash.geom.Vector3D; | |
/** | |
* 测试Matrix3D的一些方法 | |
* @author Tomyail | |
* | |
*/ | |
public class Matrix3DTest extends Sprite | |
{ | |
private var sprite:Shape = new Shape(); | |
private var prePoint:Shape = new Shape; | |
/**按钮*/ | |
private var btns:Vector.<PushButton> = new Vector.<PushButton>; | |
/**存放控制容器*/ | |
private var group:Sprite = new Sprite; | |
private var matrix:Matrix3D; | |
/**输入框*/ | |
private var ti:InputText; | |
private var myStage:Sprite = new Sprite; | |
private var datas:Vector.<Object> = Vector.<Object>( | |
[ | |
{label:"appendTranslation(lhs * thisMatrix) X",fun:appendTranslationX}, | |
{label:"prependTranslation(thisMatrix * rhs) X",fun:prependTranslationX}, | |
{label:"appendRotation Z",fun:appendRotation}, | |
{label:"prependRotation Z",fun:prependRotation}, | |
{label:"appendScale X",fun:appendScale}, | |
{label:"prependScale X",fun:prependScale}, | |
{label:"reset",fun:reset}, | |
] | |
); | |
public function Matrix3DTest() | |
{ | |
myStage.graphics.lineStyle(1); | |
myStage.graphics.drawRect(0,0,stage.stageWidth>>1,stage.stageHeight>>1); | |
myStage.x = stage.stageWidth>>1; | |
myStage.y = stage.stageHeight>>1; | |
ti = new InputText(group,0,0,"10") | |
sprite.graphics.beginFill(0); | |
sprite.graphics.drawRect(0,0,50,50); | |
sprite.graphics.beginFill(0xff0000); | |
sprite.graphics.drawRect(0,0,2,2); | |
//当显式设置一个显示对象的 z 属性或任何旋转或缩放属性时,将自动创建相应的 Matrix3D 对象 | |
sprite.z = 0; | |
myStage.addChild(sprite); | |
prePoint.graphics.beginFill(0); | |
addChild(group); | |
myStage.addChild(prePoint); | |
addChild(myStage); | |
matrix = sprite.transform.matrix3D; | |
for(var i:int = 0;i<datas.length;i++) | |
{ | |
btns[i] = new PushButton(group,0,(i+1)*20,datas[i].label,datas[i].fun); | |
} | |
group.x = stage.stageWidth - group.width; | |
} | |
private function appendRotation(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.appendRotation(parseFloat(ti.text),Vector3D.Z_AXIS); | |
} | |
private function appendScale(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.appendScale(parseFloat(ti.text),1,1); | |
} | |
private function prependScale(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.prependScale(parseFloat(ti.text),1,1); | |
} | |
private function prependRotation(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.prependRotation(parseFloat(ti.text),Vector3D.Z_AXIS); | |
} | |
private function appendTranslationX(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.appendTranslation(parseFloat(ti.text),0,0); | |
} | |
private function prependTranslationX(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.prependTranslation(parseFloat(ti.text),0,0); | |
} | |
private function reset(e:MouseEvent):void | |
{ | |
drawPre(); | |
matrix.identity(); | |
prePoint.graphics.clear(); | |
prePoint.graphics.beginFill(0); | |
} | |
private function drawPre():void | |
{ | |
prePoint.graphics.drawCircle(sprite.x,sprite.y,2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment