gpt4 book ai didi

flash - 如何绘制正弦线图?

转载 作者:行者123 更新时间:2023-12-01 02:47:45 24 4
gpt4 key购买 nike

我正在尝试基于数字数组绘制静态图。这个图应该是很好的光滑窦状。底部值应始终为零,较高值在数字数组中指定。

sample

我一直在尝试使用 curveTo() 来实现这种效果,但没有任何运气。

编辑:值类似于:10、15、40、28、5、2、27 等。

有人可以帮忙吗?

最佳答案

curveTo() 只绘制二次贝塞尔曲线,它本质上是抛物线段。因此,如果您想绘制具有可变幅度的正弦,您必须构思一些棘手的算法。

我建议你应该用直线段绘制曲线。它需要在循环中进行大量迭代,但这是一种非常简单的方法,只需几分钟即可编写代码。你真的需要更复杂的东西吗?

或者,看看 http://code.google.com/p/bezier/ : 他们的演示很酷。

如果您想创建无法用单个方程轻松定义的内容,您可能需要查看拉格朗日近似:它将给定的点插入到平滑函数中。

编辑 拉格朗日近似示例

下面的代码绘制如下:

enter image description here

package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;

import org.noregret.math.LagrangeApproximator;

[SWF(fps="31",width="600",height="600",backgroundColor="0xFFFFFF")]
public class LagrangeTest extends Sprite
{
private const plot:Sprite = new Sprite();
private const approx:LagrangeApproximator = new LagrangeApproximator();

public function LagrangeTest()
{
super();
initialize();
}

private function initialize():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onResize);
onResize();

addChild(plot);

// ADDING CONTROL POINTS (just took them at random)
approx.addValue(0, 0);
approx.addValue(50, -10);
approx.addValue(100, 10);
approx.addValue(150, -20);
approx.addValue(200, -10);
approx.addValue(250, -20);
approx.addValue(300, -10);
approx.addValue(350, 10);
approx.addValue(400, -30);
approx.addValue(450, 50);
approx.addValue(500, 100);
approx.addValue(550, 30);
approx.addValue(600, 0);

drawCurve(plot, 5, 0, 600);
}

private function drawCurve(target:Sprite, step:uint, fromArg:int, toArg:int):void
{
var gfx:Graphics = target.graphics;
gfx.clear();

gfx.lineStyle(0, 0xCCCCCC, 1);
gfx.moveTo(-50, 0);
gfx.lineTo(50, 0);
gfx.moveTo(0, -50);
gfx.lineTo(0, 50);

gfx.lineStyle(2, 0, 1);

var minArg:int = Math.min(fromArg, toArg);
var maxArg:int = Math.max(fromArg, toArg);

if (step == 0) {
step = 1;
}

var value:Number;
for (var i:Number = minArg; i<=maxArg; i+=step) {
value = approx.getApproximationValue(i);
if (i) {
gfx.lineTo(i, value);
} else {
gfx.moveTo(i, value);
}
}
}

private function onResize(event:Event = null):void
{
plot.x = 10;
plot.y = stage.stageHeight/2;
}
}
}

逼近器类
package org.noregret.math 
{
import flash.geom.Point;
import flash.utils.Dictionary;

/**
* @author Michael "Nox Noctis" Antipin (http://noregret.org)
*/
public class LagrangeApproximator {

private const points:Vector.<Point> = new Vector.<Point>();
private const pointByArg:Dictionary = new Dictionary();

private var isSorted:Boolean;

public function LagrangeApproximator()
{
}

public function addValue(argument:Number, value:Number):void
{
var point:Point;
if (pointByArg[argument] != null) {
trace("LagrangeApproximator.addValue("+arguments+"): ERROR duplicate function argument!");
point = pointByArg[argument];
} else {
point = new Point();
points.push(point);
pointByArg[argument] = point;
}
point.x = argument;
point.y = value;
isSorted = false;
}

public function getApproximationValue(argument:Number):Number
{
if (!isSorted) {
isSorted = true;
points.sort(sortByArgument);
}
var listLength:uint = points.length;
var point1:Point, point2:Point;
var result:Number = 0;
var coefficient:Number;
for(var i:uint =0; i<listLength; i++) {
coefficient = 1;
point1 = points[i];
for(var j:uint = 0; j<listLength; j++) {
if (i != j) {
point2 = points[j];
coefficient *= (argument-point2.x) / (point1.x-point2.x);
}
}
result += point1.y * coefficient;
}
return result;
}

private function sortByArgument(a:Point, b:Point):int
{
if (a.x < b.x) {
return -1;
}
if (a.x > b.x) {
return 1;
}
return 0;
}

public function get length():int
{
return points.length;
}

public function clear():void
{
points.length = 0;
var key:*;
for (key in pointByArg) {
delete pointByArg[key];
}
}
}
}

关于flash - 如何绘制正弦线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com