如何在数学图中绘制三角形? [英] How to draw a triangle in a math graph?
问题描述
如果要使用ActionScript2绘制形状,您可以使用MovieClip对象的 moveTo()和 lineTo()方法。您可以使用 lineStyle()指定线条的颜色和粗细,或者使用 beginFill()和 endFill()设置实体的形状。 >
因此,绘制图形和三角形可以执行以下步骤:
- 定义图的大小(使用flash.geom.Rectangle对象)
- 使用图绘制灰色背景.beginFill(灰色),然后moveTo()和lineTo()
- 定期为网格绘制一些蓝线
- 绘制X和创建第二个名为shape的movieClip
- 选取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:
- Make a movieClip named "graph"
- Define how big your graph should be (using the flash.geom.Rectangle object)
- Draw a grey background using graph.beginFill(grey) and then moveTo() and lineTo()
- Draw some blue lines at regular intervals for a grid
- Draw the X and Y axes on the side and bottom of your grid
- Make a second movieClip named "shape"
- 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屋!