在Processing中平滑逐像素绘制

时间:2016-03-03 09:19:44

标签: graphics drawing processing

我今天选择了Processing,并编写了一个程序来生成双缝干涉图案。在稍微调整一下这些值之后,它会起作用,但生成的模式比其他程序中的模式更模糊。这是一个截图:

interference pattern

正如你所看到的,边缘并不像我认为的那样边缘光滑。我希望它们看起来像thisthis

这是我的代码:

// All quantities in mm

float slit_separation = 0.005;
float screen_dist = 50;
float wavelength = 5e-4f;

PVector slit1, slit2;

float scale = 1e+1f;

void setup() {
  size(500, 500);
  colorMode(HSB, 360, 100, 1);
  noLoop();
  background(255);

  slit_separation *= scale;
  screen_dist *= scale;
  wavelength *= scale;

  slit1 = new PVector(-slit_separation / 2, 0, -screen_dist);
  slit2 = new PVector(slit_separation / 2, 0, -screen_dist);
}

void draw() {
  translate(width / 2, height / 2);
  for (float x = -width / 2; x < width / 2; x++) {
    for (float y = -height / 2; y < height / 2; y++) {
      PVector pos = new PVector(x, y, 0);
      float path_diff = abs(PVector.sub(slit1, pos).mag() - PVector.sub(slit2, pos).mag());
      float parameter = map(path_diff % wavelength, 0, wavelength, 0, 2 * PI);
      stroke(100, 100, pow(cos(parameter), 2));
      point(x, y);
    }
  }
}

我的代码在数学上是正确的,所以我想知道我在将物理值转换为屏幕上的像素时是否存在错误。

1 个答案:

答案 0 :(得分:0)

我不完全确定你在问什么 - 你期望它到底是什么样的?是否有可能将此范围缩小到单行而不是嵌套的for循环?

但只是猜测你正在谈论的内容:请记住,Processing默认情况下会启用抗锯齿功能。要禁用它,您必须调用noSmooth()函数。您可以在setup()函数中调用它:

void setup() {
  size(500, 500);
  noSmooth();
  //rest of your code

如果你并排比较它们就很明显了:

如果这不是您所说的,请发布一条或两条线的MCVE而不是嵌套的for循环。包括一个你期望的样本和你得到的样本也会很有帮助。祝你好运!