我通过在函数中放入 x 数量的像素,在处理中绘制了正弦波。乍一看就像一条连续的线:
但是当我增加波的振幅时,我注意到绘制的点散开并且看起来不再连续:
Sine wave with increased amplitude where the points are spread
我认为绘制更多的点或点之间的插值可能会解决问题。我想知道对于任何幅度或频率的增量,使函数看起来连续的最有效方法是什么。
如果有任何帮助,这里是处理中的代码:
// sine variables
float x, y, y2, offset,r = 0;
float ex = 0, ey = 0;
void setup() {
size(800, 800);
frameRate(60);
}
void draw() {
// sine
checkMouse();
amp = amp+ex;
background(0);
y2 = y;
stroke(255);
for (int i = 5; i < 6;i++) {
updateWave(i);
}
}
void updateWave(float i) {
for (int x = 1; x < width; x+=1) {
y = sin(radians(-x*abs(ex)+r))*ey*i;
circle(x, y+height/2,2);
}
r = millis()/8;
}
void checkMouse() {
if (mousePressed){
ex = (mouseX - height/2)*0.01;
ey = pow((mouseY- height/2), 2)/1000;
} else {
if (ey < 1) {
ey = 0;
} else {
ey -= 1;
}
}
}
谢谢!!
最佳答案
在两个连续点之间绘制一条线 ( line()
),而不是单个点 ( circle()
)。线条的粗细可以通过 strokeWeight()
设置:
void updateWave(float i) {
strokeWeight(2);
float prevY = 0.0;
for (int x = 0; x < width; x ++) {
float newY = sin(radians(-x*abs(ex)+r))*ey*i + height/2;
if (x > 0)
line(x-1, prevY, x, newY);
prevY = newY;
}
r = millis()/8;
}
关于java - 处理(Java): How to make a plotted sine function look continuous instead of separate dots when the amplitude of the function increases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64928675/