【问题标题】:how do i display the coordinates of a random triangle in actionscript 2.0?如何在 actionscript 2.0 中显示随机三角形的坐标?
【发布时间】:2011-02-03 16:43:05
【问题描述】:

我想生成一个带有坐标的随机三角形,它显示了带有 flash 8.0 的 actionscript 2.0 中每个角的弧线。

【问题讨论】:

标签: flash geometry coordinates actionscript-2


【解决方案1】:

这看起来很像您的previous question(也很像this other question),但这里是:

import flash.geom.Point;

function randomPoint():Point {  //return a random point on the stage
    var p:Point = new Point(Math.floor(Math.random()*(Stage.width-300)), Math.floor(Math.random()*Stage.height));
    return p;
}

function drawTriangle(mc:MovieClip, q1:Point, q2:Point, q3:Point):Void {//draws a triangle through 3 points
    var stroke=2;//line weight of triangle
    mc.lineStyle(stroke, 0x000000, 100, true, "none", "round", "round");
    mc.moveTo(q1.x, q1.y);
    mc.lineTo(q2.x, q2.y);
    mc.lineTo(q3.x, q3.y);
    mc.lineTo(q1.x, q1.y);
}

function drawCircle(target_mc:MovieClip, x:Number, y:Number):Void {
    //draws a red circle, centred on (x,y)
    var radius:Number=18;
    var k1:Number=Math.tan(Math.PI / 8) * radius;
    var k2:Number=Math.sin(Math.PI / 4) * radius;
    with (target_mc) {
        lineStyle(2, 0xFF0000, 100, true, "none", "round", "round");
        moveTo(x + radius, y);
        curveTo(radius + x, k1 + y, k2 + x, k2 + y);
        curveTo(k1 + x, radius + y, x, radius + y);
        curveTo(-k1 + x, radius+ y, -k2 + x, k2 + y);
        curveTo(-radius + x, k1 + y, -radius + x, y);
        curveTo(-radius + x, -k1 + y, -k2 + x, -k2 + y);
        curveTo(-k1 + x, -radius + y, x, -radius + y);
        curveTo(k1 + x, -radius + y, k2 + x, -k2 + y);
        curveTo(radius + x, -k1 + y, radius + x, y);
        }
}

function arcTriangle():MovieClip {  //main function to draw a triangle with corner arcs
    //make a new movieclip t which will hold our triangle parts
    var depth=this.getNextHighestDepth();
    var t:MovieClip = this.createEmptyMovieClip("t"+depth, depth);

    //define 3 random points (stored as properties of t)
    t.p1=randomPoint();
    t.p2=randomPoint();
    t.p3=randomPoint();

    //draw a triangle
    t.createEmptyMovieClip("triangle", 0);
    drawTriangle(t.triangle, t.p1, t.p2, t.p3);

    //draw a filled triangle to use as a mask
    t.createEmptyMovieClip("mask", 1);
    t.mask.beginFill(0xF0F0F0);
    drawTriangle(t.mask, t.p1, t.p2, t.p3);
    t.mask.endFill();
    t.mask._alpha=0;

    //add a red circle to each corner
    t.createEmptyMovieClip("arcHolder", 2);
    drawCircle(t.arcHolder,t.p1.x,t.p1.y);
    drawCircle(t.arcHolder,t.p2.x,t.p2.y);
    drawCircle(t.arcHolder,t.p3.x,t.p3.y);

    //mask the circles so only the interior arcs are visible
    t.arcHolder.setMask(t.mask);

    //show the coordinates (from bottom-left corner of the stage) for each point
    t.createTextField("text1",3,t.p1.x,t.p1.y,300,100);
    t.text1.text="("+t.p1.x+", "+(Stage.height-t.p1.y)+")";
    t.createTextField("text2",4,t.p2.x,t.p2.y,300,100);
    t.text2.text="("+t.p2.x+", "+(Stage.height-t.p2.y)+")";
    t.createTextField("text3",5,t.p3.x,t.p3.y,300,100);
    t.text3.text="("+t.p3.x+", "+(Stage.height-t.p3.y)+")";

    return t;
}

var myTriangle:MovieClip = arcTriangle();

完成后应该看起来像这样:


(来源:webfactional.com

如果我的任何一个(或两个)答案对您有帮助,请点击大勾号 (接受 ) 在左手侧。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2013-09-10
    • 2013-02-07
    • 1970-01-01
    相关资源
    最近更新 更多