ActionScript 3 三角方程创建椭圆

发布于 2024-10-17 00:40:21 字数 656 浏览 5 评论 0原文

我正在使用此函数,该函数将对象围绕完美圆的中心点移动,并尝试修改它以将其移动为椭圆形,高度为宽度的 1/2?

基本上,我有一个设定的速度

var myVelocity:Number = 0.25;

,然后根据速度计算正弦和余弦

var myCos:Number = Math.cos(myVelocity);
var mySin:Number = Math.sin(myVelocity);

,然后计算物体沿每个轴距固定中心点的距离,

var x1:Number = myBall.x - centerX;
var y1:Number = myBall.y - centerY;
var x2:Number = myCos * x1 - mySin * y1;
var y2:Number = myCos * y1 + mySin * x1;
myBall.x = centerX + x2;
myBall.y = centerY + y2;

我还有另一个函数根据 myBall.x 计算 x 和 y = 中心X + cos(角度) * 半径;将半径修改为椭圆形很容易,但是有没有一种简单的方法可以将上面的半径修改为椭圆形呢?我想这样做是为了提高代码效率并减少数学函数调用的数量

I am working with this function that moves an object around a center point in a perfect circle and am trying to modify it to move it in an oval shape that is 1/2 as high as it is wide?

Basically, I have a set speed

var myVelocity:Number = 0.25;

then I calculate my Sine and Cosine based on the speed

var myCos:Number = Math.cos(myVelocity);
var mySin:Number = Math.sin(myVelocity);

then I figure the distance of the the object from a fixed center points along each axis and

var x1:Number = myBall.x - centerX;
var y1:Number = myBall.y - centerY;
var x2:Number = myCos * x1 - mySin * y1;
var y2:Number = myCos * y1 + mySin * x1;
myBall.x = centerX + x2;
myBall.y = centerY + y2;

I have another function that figures x and y based upon myBall.x = centerX + cos(angle) * radius; that is easy enough to modify the radius to become an ellipse, but is there an easy way to mod the one above to become an ellipse? I want to do this to be more efficient with the code and reduce the number of math function calls

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-10-24 00:40:21

这个问题几乎逐字逐句地反映了 Keith Peters基础 Actionscript 3.0:让事物动起来!< /a>

我从书中为您摘录了一个快速片段。

import flash.display.Shape;
import flash.events.Event;

var box:Shape = new Shape();
box.graphics.beginFill(0xFF0000);
box.graphics.drawRect(0, 0, 50, 50);
box.graphics.endFill();
addChild(box);
var angle:Number = 0;
var centerX:Number = stage.stageWidth/2 - (box.width/2);
var centerY:Number = stage.stageHeight/2 - (box.height/2)
var radiusX:Number = 200;
var radiusY:Number = 100;
var speed:Number = .1;
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(evt:Event):void
{
    box.x = centerX + Math.sin(angle) * radiusX;
    box.y = centerY + Math.cos(angle) * radiusY;
    angle += speed;
}

结果:http://www.swfupload.com/view/155573.htm

This question is nearly verbatim to what Keith Peters covers in Foundation Actionscript 3.0: Making Things Move!

I've whipped up a quick snippet for you from the book.

import flash.display.Shape;
import flash.events.Event;

var box:Shape = new Shape();
box.graphics.beginFill(0xFF0000);
box.graphics.drawRect(0, 0, 50, 50);
box.graphics.endFill();
addChild(box);
var angle:Number = 0;
var centerX:Number = stage.stageWidth/2 - (box.width/2);
var centerY:Number = stage.stageHeight/2 - (box.height/2)
var radiusX:Number = 200;
var radiusY:Number = 100;
var speed:Number = .1;
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(evt:Event):void
{
    box.x = centerX + Math.sin(angle) * radiusX;
    box.y = centerY + Math.cos(angle) * radiusY;
    angle += speed;
}

Result: http://www.swfupload.com/view/155573.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文