【问题标题】:How to draw a triangle in a math graph?如何在数学图中绘制三角形?
【发布时间】:2010-03-31 12:27:41
【问题描述】:

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

【问题讨论】:

  • 你能展示一张你正在尝试做什么的图片吗?

标签: actionscript-2 flash


【解决方案1】:

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

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

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

代码如下所示:

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
}

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


(来源:webfactional.com

【讨论】:

    【解决方案2】:

    听起来您需要了解绘图 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,这将不起作用,而且我不知道如何提供帮助,但您可以尝试使用谷歌搜索“AS2 绘图 API”或其他内容达到这个效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      相关资源
      最近更新 更多