How to draw a triangle in a math graph?

前端 未结 2 489
长情又很酷
长情又很酷 2021-01-22 03:39

How to draw a triangle in a math graph which displays X and Y axis.

2条回答
  •  后悔当初
    2021-01-22 03:48

    To draw shapes using ActionScript2, you can use the moveTo() and lineTo() methods of the MovieClip object. You can specify line colour and thickness with lineStyle(), or make a solid shape using beginFill() and endFill().

    So to draw your graph and triangle you could do the following steps:

    1. Make a movieClip named "graph"
    2. Define how big your graph should be (using the flash.geom.Rectangle object)
    3. Draw a grey background using graph.beginFill(grey) and then moveTo() and lineTo()
    4. Draw some blue lines at regular intervals for a grid
    5. Draw the X and Y axes on the side and bottom of your grid
    6. Make a second movieClip named "shape"
    7. Pick 3 random points: moveTo(point1), lineTo(point2), lineTo(point3), lineTo(point1)

    Here's how the code might look:

    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 (irect.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
    }
    

    You can click the graph to generate a new random triangle.


    (source: webfactional.com)

提交回复
热议问题