flash as3 相当于处理 popmatrix() 和 Pushmatrix() - 或者...存储变换矩阵

发布于 2024-12-14 13:09:54 字数 1649 浏览 0 评论 0原文

我正在尝试将处理草图转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-21 13:09:54

关于绘图 api 的差异有两个问题。

首先,Flash 不会跟踪变换矩阵,除非您在显示层次结构上绘制多个精灵。不建议这样做,因为每次成功的递归都会创建至少两个显示对象并使显示层次结构混乱。

第二个,或多或少,由于第一个,Flash 也不会自动将转换应用于绘制方法,除非使用如上所述的多个显示对象。

下面的代码大致可以满足您的需要。我已经玩过您的代码片段,但不确定我这边创建的渲染是否与您用于处理的算法相匹配。

private var matrices:Vector.<Matrix>;
private var matrix:Matrix;

public function Main()
{
    matrices = new Vector.<Matrix>();
    matrix = new Matrix();
    matrix.identity();
}

private function pushMatrix():void
{
    matrices.push(matrix.clone());
}

private function popMatrix():void
{
    matrix = matrices.pop();
}

在绘制方法中,您必须维护两个点:原点和目标。每个代表基于当前矩阵的变换点。

var origin:Point;
var target:Point;

pushMatrix();
matrix.rotate(theta);

origin = matrix.transformPoint(new Point(0, 0));
target = matrix.transformPoint(new Point(0, -h));

graphics.moveTo(origin.x, origin.y);
graphics.moveTo(target.x, target.y);

...

您可以对算法执行一些基本的优化。重用现有的积分无疑会有所帮助。

我没有安装处理程序,但如果您可以链接代表此特定算法的视频,它可以帮助其他人了解您的目标。

祝你好运!

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.

private var matrices:Vector.<Matrix>;
private var matrix:Matrix;

public function Main()
{
    matrices = new Vector.<Matrix>();
    matrix = new Matrix();
    matrix.identity();
}

private function pushMatrix():void
{
    matrices.push(matrix.clone());
}

private function popMatrix():void
{
    matrix = matrices.pop();
}

Within your draw method, you will have to maintain two points, origin and target. Each represents the transformed point based on the current matrix.

var origin:Point;
var target:Point;

pushMatrix();
matrix.rotate(theta);

origin = matrix.transformPoint(new Point(0, 0));
target = matrix.transformPoint(new Point(0, -h));

graphics.moveTo(origin.x, origin.y);
graphics.moveTo(target.x, target.y);

...

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文