如何在数学图中绘制三角形? [英] How to draw a triangle in a math graph?

查看:164
本文介绍了如何在数学图中绘制三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

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

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


  1. 定义图的大小(使用flash.geom.Rectangle对象)
  2. 使用图绘制灰色背景.beginFill(灰色),然后moveTo()和lineTo()
  3. 定期为网格绘制一些蓝线
  4. 绘制X和创建第二个名为shape的movieClip
  5. 选取3个随机点:moveTo(point1), lineTo(point2),lineTo(point3),lineTo(point1)


$ b 下面是代码的样子:

  import flash.geom.Point; 
import flash.geom.Rectangle;

函数drawGraph(mc:MovieClip,rect:Rectangle):Void {
//这是一个绘制图形的函数

//绘制背景
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();

//绘制网格
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);

绘制坐标轴
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);
}

函数randomPoint(rect:Rectangle):Point {
//这是一个返回rect
中的随机点的函数var p:Point = new Point(rect.x + Math.random()* rect.width,rect.y + Math.random()* rect.height);
return p;


函数drawTriangle(mc:MovieClip,rect:Rectangle):Void {
//这是绘制三角形的函数

/ / rect
var p1:Point = randomPoint(rect);
var p2:Point = randomPoint(rect);
var p3:Point = randomPoint(rect);

//将点连接成一个三角形
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);


$ b //创建图片剪辑
var myGraph:MovieClip = this.createEmptyMovieClip(myGraph,this.getNextHighestDepth());
//定义图形大小:
var myRect:Rectangle = new Rectangle(50,50,300,300);
drawGraph(myGraph,myRect); //绘制图形
var myShape:MovieClip = this.createEmptyMovieClip(myShape,this.getNextHighestDepth()); //使'形状'剪辑
drawTriangle(myShape,myRect); //绘制一个随机三角形

//添加一个函数在点击图形时绘制一个新的三角形:
myGraph.onRelease = function() {
myShape.clear(); //擦除旧的三角形
drawTriangle(myShape,myRect); //绘制一个新的
}



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

图+三角形http://roi.webfactional.com/img/so/graph .jpg


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

解决方案

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

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

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

这篇关于如何在数学图中绘制三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆