当前位置: 首页 > 知识库问答 >
问题:

理解圆在加工过程中是如何形成的

弓明亮
2023-03-14
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轴上的线的起始点?最后,为什么需要“角度”这个变量?当我把它拿出来时,我注意到可视化工具并不像象限之间看起来有一个划分那样流畅。
我一直在玩这段代码,但找不到太多详细解释的示例,因此希望得到任何帮助。谢谢你。

共有1个答案

经福
2023-03-14

你需要做的第一件事是更好地理解基本三角学。这里有大量的资源:尝试谷歌搜索“Sin cos教程”或“Sin and cos for game Development”或“Sohcahtoa”以获得一系列结果。

但是基本上,如果有一个起点、一个旋转和一个距离,就可以使用sincos计算出终点在哪里。计算终点的基本公式是:

endX = startX + cos(rotation)*distance;
endY = startY + sin(rotation)*distance;

您的代码使用这个公式来查找圆周周围的点,这样它就可以在它们之间画线来绘制圆周。圆的每一条线截面是endpoint的2。

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);

  }
}
 类似资料:
  • 我正在上一门使用处理的课。 我在理解map()函数时遇到了问题。 根据文件记载(http://www.processing.org/reference/map_.html): 将数字从一个范围重新映射到另一个范围。 在上面的第一个示例中,数字25从0到100范围内的值转换为从窗口的左边缘(0)到右边缘(宽度)的值。 如第二个示例所示,范围之外的数字不会被钳制到最小和最大参数值,因为范围之外的值通常

  • 正如您所看到的,我已经有了一个iscollding方法,控制台中的输出似乎是正确的,但是它在show()方法中不起作用,圆圈不会停止相互交叉。 那么我怎样才能使它起作用,当它碰撞时,位置被重新计算呢?

  • 问题内容: 请提供有关如何在椭圆形或圆形上进行裁剪的想法。请分享您的想法。 问题答案: 通过增加半径,它将变得更圆滑。 只要图像是正方形,就可以通过将宽度的一半作为拐角半径来获得一个完美的圆: 您还需要添加 斯威夫特4.2

  • 主要活动。xml 这是一张圆形的照片。xml: 我尝试了一些阴影效果的代码,但它不起作用。

  • 因此,目标是每次锤子或水桶落下时添加一个黑色圆圈而不击中人,然后每次人进门时添加4个圆圈,当第一行圆圈被填满时,它将进入第二行,直到整个屏幕被填满,然后重新设置。而且,随着人被锤子或水桶击中,会出现一整排的红圈。我不知道该怎么做,谢谢。

  • 问题内容: 如何给这个按钮背景像jbutton 我想给这个按钮金属色或灰色那样怎么做呢? RoundButton-产生轻量级按钮的类。 轻量级组件可以具有“透明”区域,这意味着您可以 在这些区域后面看到容器的背景。 问题答案: java.awt.Component不支持不透明度/透明度,仅支持Swing组件。 首先要解决的问题是…… 现在,完成所有这些操作之后,您可能想在 基础组件时认真看一下 j