gpt4 book ai didi

random - 在随机斜率上的一点处找到切线

转载 作者:行者123 更新时间:2023-12-04 04:53:47 26 4
gpt4 key购买 nike

我有一段收到的处理代码,它似乎正在设置 randomized Fourier series .不幸的是,尽管我努力提高我的数学技能,但我不知道它在做什么以及 articles我发现都没有太大帮助。

我正在尝试扩展此代码,以便我可以绘制一条与下面代码创建的斜率上的点相切的线。我能找到的最接近回答这个问题的是 mathematics forum .不幸的是,我真的不明白正在讨论什么,或者它是否真的与我的情况有关。

任何关于如何计算这条曲线上特定点的切线的帮助将不胜感激。

更新截至 13 年 6 月 17 日

我一直在尝试解决这个问题,但没有取得太大的成功。这是我能做的最好的事情,我怀疑我是否正确地应用了导数来找到切线(或者即使我正确地找到了该点的导数)。另外,我开始担心即使我的其他一切都正确,我也没有正确地画线。如果有人可以就此提供意见,我将不胜感激。

final int w = 800;
final int h = 480;
double[] skyline;
PImage img;
int numOfDeriv = 800;
int derivModBy = 1; //Determines how many points will be checked
int time;
int timeDelay = 1000;
int iter;
double[] derivatives;

void setup() {
noStroke();
size(w, h);
fill(0,128,255);
rect(0,0,w,h);
int t[] = terrain(w,h);
fill(77,0,0);
for(int i=0; i < w; i++){
rect(i, h, 1, -1*t[i]);
}
time = millis();
timeDelay = 100;
iter =0;
img = get();
}

void draw() {
int dnum = 0; //Current position of derivatives
if(iter == numOfDeriv) iter = 0;
if (millis() > time + timeDelay){
image(img, 0, 0, width, height);
strokeWeight(4);
stroke(255,0,0);
point((float)iter*derivModBy, height-(float)skyline[iter*derivModBy]);
strokeWeight(1);
stroke(255,255,0);
print("At x = ");
print(iter);
print(", y = ");
print(skyline[iter]);
print(", derivative = ");
print((float)derivatives[iter]);
print('\n');
lineAngle(iter, (int)(height-skyline[iter]), (float)derivatives[iter], 100);
lineAngle(iter, (int)(height-skyline[iter]), (float)derivatives[iter], -100);
stroke(126);
time = millis();
iter += 1;
}
}

void lineAngle(int x, int y, float angle, float length)
{
line(x, y, x+cos(angle)*length, y-sin(angle)*length);
}

int[] terrain(int w, int h){

width = w;
height = h;

//min and max bracket the freq's of the sin/cos series
//The higher the max the hillier the environment
int min = 1, max = 6;

//allocating horizon for screen width
int[] horizon = new int[width];
skyline = new double[width];
derivatives = new double[numOfDeriv];

//ratio of amplitude of screen height to landscape variation
double r = (int) 2.0/5.0;

//number of terms to be used in sine/cosine series
int n = 4;

int[] f = new int[n*2];

//calculating omegas for sine series
for(int i = 0; i < n*2 ; i ++){
f[i] = (int) random(max - min + 1) + min;
}

//amp is the amplitude of the series
int amp = (int) (r*height);
int dnum = 0; //Current number of derivatives
for(int i = 0 ; i < width; i ++){
skyline[i] = 0;
double derivative = 0.0;
for(int j = 0; j < n; j++){
if(i % derivModBy == 0){
derivative += ( cos( (f[j]*PI*i/height) * f[j]*PI/height) -
sin(f[j+n]*PI*i/height) * f[j+n]*PI/height);
}

skyline[i] += ( sin( (f[j]*PI*i/height) ) + cos(f[j+n]*PI*i/height) );
}
skyline[i] *= amp/(n*2);
skyline[i] += (height/2);
skyline[i] = (int)skyline[i];
horizon[i] = (int)skyline[i];
derivative *= amp/(n*2);
if(i % derivModBy == 0){
derivatives[dnum++] = derivative;
derivative = 0;
}
}

return horizon;
}

void reset() {
time = millis();
}

最佳答案

好吧,在这种特殊情况下,您似乎不需要对傅立叶级数了解太多,只需了解它的形式:

    A0 + A1*cos(x) + A2*cos(2*x) + A3*cos(3*x) +... + B1*sin(x) + B2*sin(x) +...

通常你会得到一个函数 f(x)你需要找到 An 的值和 Bn使得傅立叶级数收敛到您的函数(当您添加更多项时)某个区间 [a, b] .

然而,在这种情况下,他们想要一个看起来像不同的块和坑(或上下文可能暗示的山丘和山谷)的随机函数,因此他们从傅立叶级数中选择 min 和 max 之间的随机项并将它们的系数设置为 1(并且在概念上0 否则)。他们还用 4 个正弦项和 4 个余弦项的傅立叶级数来满足自己(这肯定比无穷多的项更容易管理)。这意味着它们的傅立叶级数最终看起来像是不同频率的不同正弦和余弦函数加在一起(并且都具有相同的幅度)。

如果你还记得的话,很容易找到它的导数:
    sin(n*x)' = n * cos(x)
cos(n*x)' = -n * sin(x)
(f(x) + g(x))' = f'(x) + g'(x)

所以计算导数的循环看起来像:
    for(int j = 0; j < n; j++){
derivative += ( cos( (f[j]*PI*i/height) * f[j]*PI/height) - \
sin(f[j+n]*PI*i/height) * f[j+n]*PI/height);
}

在某些时候 i (请注意,导数是关于 i 的,因为这是代表我们在这里的 x 位置的变量)。

希望有了这个,您应该能够计算点 i 处的切线方程。 .

更新
在你做的时候 skyline[i] *= amp/(n*2);您还必须相应地调整您的导数 derivative *= amp/(n*2);但是当您这样做时,您的导数不需要调整 skyline[i] += height/2;

关于random - 在随机斜率上的一点处找到切线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050916/

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