how do i display the coordinates of a random triangle in actionscript 2.0?
I wanna generate a random triang开发者_开发问答le with coordinates, which shows the arcs of each corner in actionscript 2.0 with flash 8.0.
This looks a lot like your previous question (also a lot like this other question), but here goes:
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();
It should look a bit like this when you're done:
(source: webfactional.com)
If either (or both) of my answers are helpful to you, please accept them by clicking the big tick-mark (
) on the left-hand side. Thank you.
精彩评论