flash as3 相当于处理 popmatrix() 和 Pushmatrix() - 或者...存储变换矩阵
我正在尝试将处理草图转换为 flash as3 文件,并且我对两个处理命令感到困惑 - PushMatrix() 和 popMatrix() - 谁能告诉我如何在 Flash 中转换这些命令?
本质上,我只需要存储到目前为止绘制的线矩阵并绘制一条新线,这是递归完成的。这是我的代码:
var theta;
var xpos:Number = 0;
addEventListener(Event.ENTER_FRAME,draw)
function draw(e:Event) {
graphics.lineStyle(1, 0xf1eee5, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
var a = (mouseX / stage.stageWidth) * 90;
trace("a: " + a);
var theta = degreesToRadians(a);
graphics.moveTo(stage.stageWidth/2,stage.stageHeight);
graphics.lineTo(stage.stageWidth/2,stage.stageHeight-150);
branch(150);
if (a <= 30){
xpos+=3;
} else {
}
}
function degreesToRadians(degrees:Number):Number {
return degrees * Math.PI / 180;
}
function branch(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
/* HASN'T BEEN CONVERTED TO FLASH AS3 YET
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
println(h);
branch(h);
popMatrix();
*/
}
}
I'm trying to convert a processing sketch to a flash as3 file, and I'm getting hung up on two of processing's commands - pushMatrix() and popMatrix() - can anyone tell me how to convert these in flash?
Essentially I just need to store the matrix of lines I've drawn so far and draw a new line, and this gets done recursively. Here's my code:
var theta;
var xpos:Number = 0;
addEventListener(Event.ENTER_FRAME,draw)
function draw(e:Event) {
graphics.lineStyle(1, 0xf1eee5, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
var a = (mouseX / stage.stageWidth) * 90;
trace("a: " + a);
var theta = degreesToRadians(a);
graphics.moveTo(stage.stageWidth/2,stage.stageHeight);
graphics.lineTo(stage.stageWidth/2,stage.stageHeight-150);
branch(150);
if (a <= 30){
xpos+=3;
} else {
}
}
function degreesToRadians(degrees:Number):Number {
return degrees * Math.PI / 180;
}
function branch(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
/* HASN'T BEEN CONVERTED TO FLASH AS3 YET
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
println(h);
branch(h);
popMatrix();
*/
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于绘图 api 的差异有两个问题。
首先,Flash 不会跟踪变换矩阵,除非您在显示层次结构上绘制多个精灵。不建议这样做,因为每次成功的递归都会创建至少两个显示对象并使显示层次结构混乱。
第二个,或多或少,由于第一个,Flash 也不会自动将转换应用于绘制方法,除非使用如上所述的多个显示对象。
下面的代码大致可以满足您的需要。我已经玩过您的代码片段,但不确定我这边创建的渲染是否与您用于处理的算法相匹配。
在绘制方法中,您必须维护两个点:原点和目标。每个代表基于当前矩阵的变换点。
您可以对算法执行一些基本的优化。重用现有的积分无疑会有所帮助。
我没有安装处理程序,但如果您可以链接代表此特定算法的视频,它可以帮助其他人了解您的目标。
祝你好运!
There are two issues regarding the differences in drawing api's.
The first, flash does not keep track of transformation matrices unless you are drawing into multiple sprites on the display hierarchy. This would not be recommended as each successful recursion would create at least two display objects and clutter the display hierarchy.
The second, more or less, due to the first, is that flash also does not automatically apply a transformation to draw methods, unless using multiple display objects as described above.
The following code would roughly do what you need. I've played around with your snippet, but am unsure if the render created on my side matches the algorithm you were using with processing.
Within your draw method, you will have to maintain two points, origin and target. Each represents the transformed point based on the current matrix.
There are basic optimizations that you can perform to the algorithm. Reuse of existing points would defiantly help out.
I do not have processing installed, but if you could link a video that represents this particular algorithm, it could help others understand what your aiming for.
Best of luck!