- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段收到的处理代码,它似乎正在设置 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]
.
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/
试图找出在 Python 中返回直线斜率的函数。问题方向是用给定的斜率坐标求出 m 的斜率。通读其他几个堆栈溢出帖子,但似乎没有一个是可行的解决方案。以下是我尝试过的各种变体: def slope(x
我的代码中的所有内容终于看起来都是正确的。我只是遇到了一些棘手的问题。 如何编写代码,以便当我输入两个点且斜率为 -无穷大 时,它会被识别并且输出显示 Vertical 而不是 Negative坡度。
您可以通过此链接访问以下代码:jsfiddle.net/2NPxV 我的 CSS 代码: .custom_content { display: block; width:200px; height:
我需要计算一条线与水平线之间的角度。我的高中数学似乎不及格。 import matplotlib.pyplot as plt import numpy as np x = [8450.0, 8061.
我有一个 Python 程序,可以显示温度下降与时间的关系图。沿着下降过程,温度在一段时间内保持恒定,几乎为 0 斜率,然后继续下降。当温度恒定时,曲线中的这个区域我希望程序能够自动检测并显示 y 值
如何使用 R 将 RMSE、斜率、截距和 r^2 添加到绘图中?我附加了一个包含示例数据的脚本,它的格式与我的真实数据集类似 - 不幸的是,我处于停滞状态。有没有比从方程创建对象并将其插入到 text
我是一名优秀的程序员,十分优秀!