如何在 ActionScript 2.0 中显示图表中从 +10 到 -10 XY 轴的随机三角形的坐标?

2023-12-09

在actionscript 2.0中,我如何在图表中显示随机三角形的坐标,其范围在XY轴上从-10到+10,其中点如A(2,1)等,每个角都有圆弧


好的,到目前为止我们已经完成了带圆弧的三角形, 网格上的三角形, and 带坐标的三角形。现在是时候将所有这些组合成一段代码了。您要求网格大小为 -10 到 +10,但只需多做一点工作即可使其适用于任何大小的网格(好吧,在限制范围内)。

我们需要处理您选择的范围(-10 到 +10)和舞台大小(以像素为单位)之间的转换。这样做的技巧是创建一个 movieClip ('myGraph') 来保存 -10 到 +10 的网格,然后将其放大以适应舞台的大小(我们将在边缘周围留下 20 像素的边距);我们将计算一个矩形('displayRect')来表示我们希望 myGraph 的最大尺寸。我们还希望定位 myGraph,使其原点位于正确的位置,并垂直翻转它,以便 Y 轴随着您在屏幕上向上移动而增加。

完成所有这些后,我们可以使用所需的单位(-10 到 +10)在 myGraph 中进行绘制,而无需进一步转换为像素 - myGraph 的缩放会自动为我们处理这一问题。

接下来,我们在 myGraph 中创建一个“网格”剪辑,并使用 drawGrid() 函数在其中绘制网格。

然后我们在 myGraph 中创建一个“arcTriangle”剪辑,并在其中绘制三角形。我们使用与之前相同的技术,绘制三角形,在每个角绘制一个红色圆圈,然后使用三角形的副本作为蒙版以仅显示圆圈的内部弧。然后我们在每个角创建一个文本字段来显示每个点的坐标(请注意,这些需要缩放回正常大小,否则文本太小而无法阅读!)。

最后,我们向 myGraph 添加一个 onRelease 函数,该函数在单击时绘制一个新的随机 arcTriangle。

alt text
(source: webfactional.com)

这是代码...

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

var margin:Number = 20;//leave a margin around the graph...
//...calculate the largest extent (displayRect) that we want our graph to be displayed in:
var displayRect = new Rectangle(margin, margin, Stage.width-margin-60, Stage.height-2*margin);

//make the 'graph' clip:
var myGraph:MovieClip = this.createEmptyMovieClip("myGraph", this.getNextHighestDepth());

myGraph.extents = new Rectangle(-10, -10, 20, 20);//you can change this to set a different size of grid

//calculate the length of one local unit in pixels...
myGraph.unit = Math.min((displayRect.height)/myGraph.extents.height, (displayRect.width)/myGraph.extents.width);

//... and scale the graph clip so it fits inside the displayRect
myGraph._xscale = myGraph.unit*100;
myGraph._yscale = -myGraph.unit*100;// this one is negative, so Y increases upwards

//calculate the origin of myGraph...
myGraph.origin = new Point(displayRect.left-myGraph.unit*myGraph.extents.left, displayRect.top+myGraph.unit*myGraph.extents.bottom);

//... and move myGraph into the correct position
myGraph._x = myGraph.origin.x;
myGraph._y = myGraph.origin.y;

//draw a blank grid
drawGrid(myGraph.createEmptyMovieClip("grid", 0), myGraph.extents);

//draw a random triangle with arcs
arcTriangle(myGraph.createEmptyMovieClip("arcTriangle", 1), myGraph.extents);

//add a function to draw a new triangle when the graph is clicked:
myGraph.onRelease = function() {
arcTriangle(myGraph.createEmptyMovieClip("arcTriangle", 1), myGraph.extents);
}

//-------------------------------------
// All the functions are defined below:

function drawGrid(mc:MovieClip, rect:Rectangle):Void {
    //this is a function to draw the grid and axes

    //draw a light-grey 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 light-blue grid
    var unit:Number = 1;
    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 X-axis and Y-axis in dark grey
    mc.lineStyle(2, 0x808080, 100, true, "none", "round", "round");
    mc.moveTo(rect.left, 0);
    mc.lineTo(rect.right, 0);
    mc.moveTo(0, rect.bottom);
    mc.lineTo(0, rect.top);
}

function randomPoint(rect:Rectangle):Point {
    //this is a function which returns a random point within a given rectangle
    var p:Point = new Point(rect.x+Math.floor(Math.random()*rect.width), rect.y+Math.floor(Math.random()*rect.height));
    return p;
}

function drawTriangle(mc:MovieClip, q1:Point, q2:Point, q3:Point):Void {
    //this function draws a triangle through 3 points
    var stroke = 2; //(line weight of triangle)
    mc.lineStyle(stroke, 0x000000, 100, true, "none", "round", "round");
    mc.moveTo(q1.x, q1.y);
    mc.lineTo(q2.x, q2.y);
    mc.lineTo(q3.x, q3.y);
    mc.lineTo(q1.x, q1.y);
}

function drawCircle(mc:MovieClip, x:Number, y:Number):Void {
    //this draws a red circle, centred on (x,y)

    //we want the circle to always appear the same size,
    //independently of our scaling of myGraph,
    //so we need to set the radius accordingly:
    var radius:Number = 18/mc._parent._parent.unit;

    //AS2 has no direct way of drawing a circle, 
    //so we need to construct one out of 8 bezier curves:
    var k1:Number = Math.tan(Math.PI/8)*radius;
    var k2:Number = Math.sin(Math.PI/4)*radius;
    with (mc) {
        lineStyle(2, 0xFF0000, 100, true, "none", "round", "round");
        moveTo(x+radius, y);
        curveTo(radius+x, k1+y, k2+x, k2+y);
        curveTo(k1+x, radius+y, x, radius+y);
        curveTo(-k1+x, radius+y, -k2+x, k2+y);
        curveTo(-radius+x, k1+y, -radius+x, y);
        curveTo(-radius+x, -k1+y, -k2+x, -k2+y);
        curveTo(-k1+x, -radius+y, x, -radius+y);
        curveTo(k1+x, -radius+y, k2+x, -k2+y);
        curveTo(radius+x, -k1+y, radius+x, y);
    }
}

function arcTriangle(t:MovieClip, rect:Rectangle):MovieClip {
    //main function to draw a triangle with corner arcs

    //define 3 random points (stored as properties of t)
    t.p=new Array(3);
    t.p[0] = randomPoint(rect);
    t.p[1] = randomPoint(rect);
    t.p[2] = randomPoint(rect);

    //draw a triangle
    t.createEmptyMovieClip("triangle", 0);
    drawTriangle(t.triangle, t.p[0], t.p[1], t.p[2]);

    //draw a filled triangle to use as a mask
    t.createEmptyMovieClip("mask", 1);
    t.mask.beginFill(0xF0F0F0);
    drawTriangle(t.mask, t.p[0], t.p[1], t.p[2]);
    t.mask.endFill();
    t.mask._alpha = 0;

    //add a red circle to each corner
    t.createEmptyMovieClip("arcHolder", 2);
    drawCircle(t.arcHolder, t.p[0].x, t.p[0].y);
    drawCircle(t.arcHolder, t.p[1].x, t.p[1].y);
    drawCircle(t.arcHolder, t.p[2].x, t.p[2].y);

    //mask the circles so only the interior arcs are visible
    t.arcHolder.setMask(t.mask);

    //show the coordinates for each point
    var tf:TextField;
    for (var i = 0; i<3; i++) {
        tf = t.createTextField("text"+i, i+3, t.p[i].x, t.p[i].y, 1, 1);
        tf.autoSize = true;
        tf._xscale = 10000/t._parent._xscale;
        tf._yscale = 10000/t._parent._yscale;
            //use unicode values to get "A", "B" and "C":
        tf.text = String.fromCharCode(65+i)+"("+t.p[i].x+", "+t.p[i].y+")";
    }

    return t;
}

Remember: you can accept a favourite answer to each of your questions by clicking the big tick-mark (tick ) on the left-hand side. Thank you.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 ActionScript 2.0 中显示图表中从 +10 到 -10 XY 轴的随机三角形的坐标? 的相关文章

  • 在 Excel 中生成随机 -1 和 +1 值

    The Rand 函数会生成一个 0 到 1 之间的实数 这Randbetween 1 1 将生成 1 0 或 1 我想要的只是 1或1 那么 1 到 1 之间的实数呢 Easy IF RAND lt 0 5 1 1 要获得实数 请使用 R
  • 获取N个随机数,其总和为M

    我想得到N个随机数 其总和是一个值 例如 假设我想要 5 个总和为 1 的随机数 那么 一个有效的可能性是 0 2 0 2 0 2 0 2 0 2 另一种可能性是 0 8 0 1 0 03 0 03 0 04 等等 我需要这个来创建模糊 C
  • 从排列生成器中随机选择?

    如何随机挑选所有结果 一一 不重复 itertools permutations k 或者这个 如何构建随机排列生成器 就像是shuffle permutations k 我正在使用Python 2 6 Yeah shuffle r 可以使
  • ActionScript Workers 可以用于在单独的线程中播放/生成声音吗?

    我很新ActionScript 工作者 但我想知道这是否可能 从我读到的来看 ActionScript 工作者 ASW 就像单独的线程 可以执行更多 CPU 密集型计算 而无需中断主线程 正在执行主 SWF 文件 我真正看到的唯一一个例子是
  • 如何在 MATLAB 中绘制纹理映射三角形?

    我有一个三角形 u v 图像中的坐标 我想在 3D 坐标处绘制这个三角形 X Y Z 与图像中的三角形进行纹理映射 Here u v X Y Z都是具有三个元素的向量 代表三角形的三个角 我有一个非常丑陋 缓慢且令人不满意的解决方案 其中我
  • 对象克隆库的 Flash 复制?

    这可能是一个非常简单的问题 我只是不知道如何解决 我有一个想要复制的对象 但不知道如何进行 这是我的尝试 var myObj new ObjectClass var duplicate myObj duplicate null myObj
  • 射线与三角形相交

    我看到了快速最小存储射线 三角形交集 http www cs virginia edu gfx Courses 2003 ImageSynthesis papers Acceleration Fast 20MinimumStorage 20
  • C#/Java 数字随机化

    NET 是否可以模仿 Java 使用的精确随机化 我有一个种子 我希望在创建随机数时能够在 C 和 Java 中收到相同的结果 您不需要阅读源代码 该公式是一个单行公式 在以下公式中给出的文档java util Random http ja
  • 在 python 中保存 3D NetworkX 图以便稍后使用 paraview 查看

    我编写了这个脚本 它使用 python 中的 NetworkX 绘制随机 3D 图形 该脚本的输出是一个 3D 图形 我可以在其中围绕图形结构旋转相机 import networkx as nx from mpl toolkits mplo
  • Matlab:从一定范围内不重复的随机整数

    我想获得一个包含在范围内且不重复的随机整数的数组 我使用了 randperm 15 3 输出是 8 10 12 这个函数不使用范围 我只想从 10 中随机取值 例如 如果您有统计工具箱 则可以使用randsample https www m
  • Math.random() 与 Random.nextInt(int)

    有什么区别Math random n and Random nextInt n where n是一个整数 Here is 详细解释 https community oracle com message 6596485 thread mess
  • 电影剪辑结束的 Flash 事件侦听器?

    任何人都可以建议影片剪辑动画结束时触发功能的最佳方式吗 我认为事件监听器可以处理这个问题 但不确定最好的方法 谢谢 保罗 有几种方法可以解决这个问题 只需从动画的最后一帧调用该函数即可 在函数的最后一帧上调度一个事件并在其他地方监听它 长而
  • 使用 OpenGL 或 D3D 绘制椭圆的有效方法

    有一种快速画圆的方法 void DrawCircle float cx float cy float r int num segments float theta 2 3 1415926 float num segments float c
  • 如何从 Java 中的 Random 中获取种子?

    我正在为某个对象创建深度克隆 该对象包含一个Random 从种子中取回种子是个好习惯吗 Random 如果是这样 怎么办 没有一个Random getSeed 获取种子的一种更简单的方法是生成一个种子并将其存储为种子 我正在游戏中使用这种方
  • 这段代码会导致内存泄漏吗?

    这会导致内存泄漏吗 var mc MovieClip lt lt lt lt lt lt lt OUTSIDE LOOP for var i 0 i lt 1000 i mc new MovieClip mc addEventListene
  • 在时间序列线图上绘制点

    我有这个数据框 我想绘制它的线图 正如我所绘制的 Graph is 生成的代码是 fig ax plt subplots figsize 15 5 date time pd to datetime df Date df df set ind
  • php洗一副牌

    我想使用 php 创建随机桥手的集合 我认为我可以将有序的卡片包编码为字符串 deal下面 我喜欢它有 52 个字母 同时考虑大小写 我发现了 php 函数str shuffle 所以我想我可以做以下事情 pack abcdefghijkl
  • 如何随机打乱一个比 PRNG 周期更多排列的列表?

    我有一个包含大约 3900 个元素的列表 我需要对其进行随机排列以产生统计分布 我环顾四周 发现了这个使用 Python random shuffle 进行随机播放的列表的最大长度 https stackoverflow com quest
  • html5 vs flash - 完整的比较图表在哪里? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 因此 自从史蒂夫 乔布斯说 Flash 很糟糕并暗示 HTML5 可以完成 Flash 可以做的所有事情
  • 如何在数据框中绘制包含三列的无向图,形成 3 种不同类型的节点(三方)?

    我正在尝试使用三个不同的列表绘制网络的可视化 这三个列表形成 3 种类型的节点 下面的代码正在运行 如图所示 需要两个列表 用户 ID 评分 但是 我希望我的图表是三部分的 即 user userId review ratings prod

随机推荐