gpt4 book ai didi

java - 生成的积分偏向

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:11 25 4
gpt4 key购买 nike

这更像是一个有趣的实验,而不是一个问题,我更好奇是什么导致了它。

我正在生成 2D 点,存储为 float ,以便绘制图案中的线条集合。生成新点时,我复制最后一个点,并根据标题(x += cos(heading), y += sin(heading))将其移动到某个方向。标题也存储为 float 。我以给定的速率(例如 90 度)更改航向,这意味着每个新点要么与最后一个点平行,要么与其成直角。

这是可行的,但经过多次(数千次)迭代后,图案的外边缘开始变得稍微变厚,然后新绘制的点开始移动到稍微倾斜(但一致)的位置。然后整个图案开始旋转。

我很好奇的是,一些不应该损坏的东西是如何损坏的。我最好的猜测是标题(不断减少的 float )在达到一定大小时会失去定义。

本质上,代码(用于生成下面的确切模式)看起来像这样

float distance = 25;
int theta = 90;
float heading = 0;

public static void main(String[] args) {
turnLeft();
func();
turnRight();
func();
turnRight();
turnRight();
func();
}

public void func() {
drawForward();
turnRight();
drawForward();
turnRight();
turnRight();
drawForward();
turnRight();
drawForward();
drawForward();
}

public void turnLeft() {
heading += degToRad(theta);
}

public void turnRight() {
heading -= degToRad(theta);
}

public float degToRad(float degrees) {
return (float) (degrees * (Math.PI / 180.0));
}

public void drawForward() {
FPoint newFp = new FPoint(array[array.length - 1]); //copy last point

movePoint(newFp, (float) (distance * Math.cos(heading)),
(float) (distance * Math.sin(heading)));

appendPoint(newFp); //add point to end of array

}

public void movePoint(FPoint fp, float x, float y) {
fp.x += x;
fp.y += y;
}

对此事的任何想法将不胜感激!

最佳答案

看看您向前和向后移动时是否处于完全相同的位置。如果您不在同一位置,则每次向前移动都会减去一半的差值。

这可能与计算的数值稳定性精度有关。 http://en.wikipedia.org/wiki/Numerical_stability .

由于您没有向后移动,因此您只需使用前进+右+前进+右....直到到达起始位置,然后如果与起始值不同则减去。然后用它作为偏移误差值来减去(但首先将误差除以移动次数)(当然会像“FRFRFRFECC”)

这称为BFECC来回纠错补偿。如果是不可避免的错误,则大大减少错误。

我看到有人在 Zalesak 的转动磁盘上测试 BFECC,看看磁盘在数千次旋转迭代后是否会损坏。

关于java - 生成的积分偏向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184659/

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