[英]Understanding How a Circle is Formed in Processing
import ddf.minim.*;
Minim minim;
AudioPlayer player;
PImage img;
void setup() {
size(728, 546);
minim = new Minim(this);
player = minim.loadFile("Bassnectar_-_Magical_World_feat.wav");
player.play();
img= loadImage("cat-in-shades-.jpg");
}
void draw() {
image(img, 0, 0);
tint(0, 100, 150);
stroke(255);
strokeWeight(4);
float a = 0;
float angle = (2*PI) / 200;
for(int i=0; i < player.bufferSize() - 1; i++) {
//player.mix.get(i) is a value between [-1,1]
float x = 250 + cos(a) * (20 * player.mix.get(i) + 100);
float x2 = 540 + cos(a) * (20 * player.mix.get(i) + 100);
float y = 230 + sin(a) * (20 * player.mix.get(i) + 100);
float y2 = 240 + sin(a) * (20 * player.mix.get(i) + 100);
float xFinal = 250 + cos(a+angle) * (20 * player.mix.get(i+1) + 100);
float x2Final = 540 + cos(a+angle) * (20 * player.mix.get(i+1) + 100);
float yFinal = 230 + sin(a+angle) * (20 * player.mix.get(i+1) + 100);
float y2Final = 240 + sin(a+angle) * (20 * player.mix.get(i+1) + 100);
line(x,y,xFinal,yFinal);
line(x2,y2,x2Final,y2Final);
a += angle;
}
}
void stop() {
player.close();
minim.stop();
super.stop();
}
上面的以下代码用于在使用Minim库进行处理中创建音频可视化器。 由于某种原因,我正在努力查看代码的for循环中如何形成一个圆。 总的来说,我也在尝试分解代码,对正在发生的事情有更深入的了解。 我对以下内容感到困惑:'float x = 250 + cos(a)*(20 * player.mix.get(i)+ 100);' 是20倍加100用来放大样本吗? 如果是这样的话,为什么我除掉20次并加上20000时,圆形可视化器不显示? 250是否用于在背景图像中将线的起点放置在x轴上? 最后,为什么需要变量“角度”? 当我将其取出时,我注意到可视化器并不像看起来象限之间的划分那样平滑。
我一直在玩这段代码,找不到太多带有详细说明的示例,因此将不胜感激。 谢谢。
您需要做的第一件事是更好地了解基本三角函数。 那里有大量资源:尝试搜索“ sin cos教程”或“用于游戏开发的sin and cos”或“ sohcahtoa”以获得大量结果。
但基本上,如果您有一个起点,一个旋转点和一个距离,则可以找出终点在哪里使用sin
和cos
。 计算终点的基本公式是:
endX = startX + cos(rotation)*distance;
endY = startY + sin(rotation)*distance;
您的代码使用此公式在圆周围找到点,以便可以在它们之间绘制直线以绘制圆。 圆的每个线段都是2个端点。
angle
变量用于指定这些点的距离。 制作的越小,看起来越“圆”。 制作的越大,越能看到构成圆圈的直线。
使用一个更简单的示例可能会更容易:
void setup(){
size(500, 500);
}
void draw(){
background(0);
//draw white
stroke(255);
//the start point- try changing this to mouseX and mouseY
float centerX = width/2;
float centerY = height/2;
//the distance from the start point
float radius = 100;
//how far apart the points are
float angleIncrement = 30;
//loop to go around the circle. Try changing it to 180 to see what happens.
for(float angleInDegrees = 0; angleInDegrees <= 360; angleInDegrees+=angleIncrement){
//the first "end point" is the start point of the line
float startX = centerX + cos(radians(angleInDegrees))*radius;
float startY = centerY + sin(radians(angleInDegrees))*radius;
//the second "end point" is the end point of the line
//notice that we're adding the angleIncrement to the angle to get the next point
float endX = centerX + cos(radians(angleInDegrees+angleIncrement))*radius;
float endY = centerY + sin(radians(angleInDegrees+angleIncrement))*radius;
//draw the line
line(startX, startY, endX, endY);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.