如何在数学图中绘制三角形?

时间:2010-03-31 12:27:42

标签: actionscript-2 flash

如何在显示X和Y轴的数学图表中绘制三角形。

2 个答案:

答案 0 :(得分:1)

要使用ActionScript2绘制形状,可以使用MovieClip对象的 moveTo() lineTo()方法。您可以使用 lineStyle()指定线条颜色和粗细,或使用 beginFill() endFill()制作实体形状。

因此,要绘制图形和三角形,您可以执行以下步骤:

  1. 制作名为“graph”的movieClip
  2. 定义图表的大小(使用flash.geom.Rectangle对象)
  3. 使用graph.beginFill(灰色)绘制灰色背景,然后使用moveTo()和lineTo()
  4. 定期为网格绘制一些蓝线
  5. 在网格的侧面和底部绘制X和Y轴
  6. 制作名为“shape”的第二个movieClip
  7. 选择3个随机点:moveTo(point1),lineTo(point2),lineTo(point3),lineTo(point1)
  8. 以下是代码的外观:

    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    function drawGraph(mc:MovieClip, rect:Rectangle):Void {
    //this is a function to draw the graph
    
        //draw the background
        mc.beginFill(0xF8F8F8);
        mc.moveTo(rect.left, rect.bottom);
        mc.lineTo(rect.left, rect.top);
        mc.lineTo(rect.right, rect.top);
        mc.lineTo(rect.right, rect.bottom);
        mc.lineTo(rect.left, rect.bottom);
        mc.endFill();
    
        //draw a grid
        var unit:Number = 20;
        mc.lineStyle(1, 0x0000FF, 20, true, "none", "round", "round");
        var i:Number=rect.x;
        do {
            i=i+unit;
            mc.moveTo(i, rect.bottom);
            mc.lineTo(i, rect.top);
        } while (i<rect.right);
        i=rect.bottom;
        do {
            i=i-unit;
            mc.moveTo(rect.left, i);
            mc.lineTo(rect.right,i);
        } while (i>rect.top);
    
        //draw the axes
        mc.lineStyle(2, 0x808080, 100, true, "none", "round", "round");
        mc.moveTo(rect.left, rect.bottom);
        mc.lineTo(rect.left, rect.top);
        mc.moveTo(rect.left, rect.bottom);
        mc.lineTo(rect.right, rect.bottom);
    }
    
    function randomPoint(rect:Rectangle):Point {
    //this is a function which returns a random point within rect
        var p:Point = new Point(rect.x+Math.random()*rect.width, rect.y+Math.random()*rect.height);
        return p;
    }
    
    function drawTriangle(mc:MovieClip, rect:Rectangle):Void {
    //this is a function to draw the triangle
    
        // pick 3 random points within rect
        var p1:Point = randomPoint(rect);
        var p2:Point = randomPoint(rect);
        var p3:Point = randomPoint(rect);
    
        //connect the points to make a triangle
        mc.lineStyle(3, 0xFF0000, 100, true, "none", "round", "round");
        mc.moveTo(p1.x, p1.y);
        mc.lineTo(p2.x, p2.y);
        mc.lineTo(p3.x, p3.y);
        mc.lineTo(p1.x, p1.y);
    
    }
    
    //make the 'graph' clip:
    var myGraph:MovieClip = this.createEmptyMovieClip("myGraph", this.getNextHighestDepth());
    //define the graph size:
    var myRect:Rectangle = new Rectangle(50,50,300,300);
    drawGraph(myGraph,myRect);//draw the graph
    var myShape:MovieClip = this.createEmptyMovieClip("myShape", this.getNextHighestDepth());//make the 'shape' clip
    drawTriangle(myShape,myRect);//draw a random triangle
    
    //add a function to draw a new triangle when the graph is clicked:
    myGraph.onRelease = function() {
    myShape.clear();//erase the old triangle
    drawTriangle(myShape,myRect);//draw a new one
    }
    

    您可以点击图表以生成新的随机三角形。

    graph + triangle http://roi.webfactional.com/img/so/graph.jpg

答案 1 :(得分:0)

听起来您需要了解绘图API在Flash中的工作原理。您的问题有点模糊,但这可能有助于您入门 - 谷歌“Flash绘图API”以获取更多信息。

var point1:Point = new Point (0, 0);
var point2:Point = new Point (25, -50);
var point3:Point = new Point (50, 0);

var s:Sprite = new Sprite();
s.graphics.lineStyle(1, 0xff0000);
s.graphics.beginFill(0x000000);

s.graphics.moveTo(point1.x, point1.y);
s.graphics.lineTo(point2.x, point2.y);
s.graphics.lineTo(point3.x, point3.y);
s.graphics.lineTo(point1.x, point1.y);

s.graphics.endFill();
s.graphics.lineStyle(0);

s.x = (stage.stageWidth - s.width) / 2;
s.y = ((stage.stageHeight - s.height) / 2) + 50;

addChild(s);

我希望有帮助,如果您有任何问题,请与我联系。

注意:此解决方案使用的是ActionScript 3.如果您需要AS2,这将无法正常工作,我不知道如何提供帮助,但您可能只是尝试使用Google搜索“AS2绘图API”或其他内容为此。