FlasCC例子研究之Drawing

这个例子主要是向大家展示 voronoi 图的绘制方法。

Voronoi图,又叫泰森多边形Dirichlet图,其具体介绍可以参见这里http://baike.baidu.com/view/501103.htm,这不是本例子的重点。

这个例子并没有向大家展示太多的东西,AS3相关的调用和C API的使用,也和先前没有太多区别。 唯 一不同的是,这个例子的voronoi图的生成,使用了C++ class. 也就是说,这个例子,让大家看到FlasCC对C++的支持。

 

下面的代码,是例子原生代码,中间并没有注释。 这是因为,已经不需要注释了。所用到的,都是前面 Interop中已经介绍了的内容。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "VoronoiDiagramGenerator.h"
#include "AS3/AS3.h"

int main(int argc,char **argv)
{
    int stagewidth, stageheight;
    inline_as3(
        "import flash.display.Stage;\n"
        "import flash.display.Graphics;\n"
        "import com.adobe.flascc.CModule;\n"
        "var gfx = CModule.rootSprite.graphics;\n"
        "gfx.lineStyle(1, 0);\n"
        "gfx.beginFill(0, 0.0);\n"
        "%0 = CModule.rootSprite.stage.stageWidth;\n"
        "%1 = CModule.rootSprite.stage.stageHeight;\n"
        : "=r"(stagewidth),"=r"(stageheight) :
    );
    const int cellcount = 512;
    float xvals[cellcount], yvals[cellcount];
    for(int i=0; i<cellcount; i++) {
        xvals[i] = stagewidth * ((float)rand()/(float)RAND_MAX);
        yvals[i] = stageheight * ((float)rand()/(float)RAND_MAX);
    }

    VoronoiDiagramGenerator vdg;
    vdg.generateVoronoi(xvals, yvals, cellcount, 0, stagewidth, 0, stageheight, 3);
    vdg.resetIterator();

    float x1,y1,x2,y2;
    while(vdg.getNext(x1,y1,x2,y2))
    {
        inline_as3("gfx.moveTo(%0,%1);\n" : : "r"(x1), "r"(y1));
        inline_as3("gfx.lineTo(%0,%1);\n" : : "r"(x2), "r"(y2));
    }
    inline_as3("gfx.endFill();\n");
}

 

来个图!

image

posted @ 2013-05-16 00:03  麒麟子MrKylin  阅读(375)  评论(0编辑  收藏  举报