生成 .ppm 图像的牛顿分形和矩阵

问题描述 投票:0回答:0

所以任务是创建一个牛顿分形(然而,我们只考虑多项式 z³ -1) 我确实实现了代码并且一切正常并且我有我的分形。 最后一个任务是生成矩阵来创建图片,其原型如下所示:“int* newton_fractal(double x_start, double x_step, double x_end, 双y_start,双y_step,双y_end, int max_iterations,双精度);" 然后我尝试实现它但失败了,现在我只得到一张黑色图片。你能帮我调试吗? 问题肯定出在动态数组上。但我不知道如何修复它。你能帮忙吗?

`int newton_method(Complex a, double precision, int max_iterations, int *output,
                  int width, int height) {
  int i = 0;
  int index = 0;
  int r = 0, g = 0, b = 0;

  while (true) {
    Complex z = newton_poly(a);
    int dist = classify_root(z, precision);
    if (dist > 0) {
      if (dist == 1) {
        output[index++] = 255;
        output[index++] = 0;
        output[index++] = 0;
      } else if (dist == 2) {
        output[index++] = 0;
        output[index++] = 255;
        output[index++] = 0;
      } else if (dist == 3) {
        output[index++] = 0;
        output[index++] = 0;
        output[index++] = 255;

      } else {
        output[index++] = 0;
        output[index++] = 0;
        output[index++] = 0;
      }
    }

    if (i > max_iterations) {
      break;
    }
    a = z;
    i++;
  }

  return i;
}
int *newton_fractal(double x_start, double x_steps, double x_end,
                    double y_start, double y_steps, double y_end,
                    int max_iterations, double precision) {
  int width = static_cast<int>((x_end - x_start) / x_steps);
  int height = static_cast<int>((y_end - y_start) / y_steps);

  int *output = new int[width * height * 3];

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; ++x) {
      double re = x_start + (double)x * x_steps;
      double im = y_start + (double)y * y_steps;
      Complex z = {re, im};
      int index = (y * width + x) * 3;
      newton_method(z, precision, max_iterations, output + index, width,
                    height);
    }
  }
  return output;
}

 
`void print_default_image(int *matrix, int size_x, int size_y,
                         const char *filename) {
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    printf("Can not open file\n");
    exit(1);
    return;
  }
  fprintf(fp, "P3\n");
  fprintf(fp, "# file created by Maximilian Lewin\n");
  fprintf(fp, "%d %d`your text`\n", size_x, size_y);
  fprintf(fp, "255\n");

  for (int i = 0; i < size_x * size_y * 3; i += 3) {
    fprintf(fp, "%d %d %d ", matrix[i], matrix[i + 1], matrix[i + 2]);
    if (((i / 3) + 1) % size_x == 0) {
      fprintf(fp, "\n");
    }
  }
  fclose(fp);
}``your text`



int main() {
  double x_start = 1.0;
  double x_steps = 3.0 / 512;
  double x_end = 3.0;
  double y_start = 1.0;
  double y_steps = 3.0 / 512;
  double y_end = 3.0;
  int max_iterations = 100;
  double precision = 1e-6;

  int *fractal_matrix =
      newton_fractal(x_start, x_steps, x_end, y_start, y_steps, y_end,
                     max_iterations, precision);

  print_default_image(fractal_matrix, 512, 512, "fractal.ppm");

  delete[] fractal_matrix;

  return 0;
}`

我尝试使用 chatGPT 进行修复,这让一切变得更糟,因为我的代码现在是 chatGPT 和我自己的混合体,这让我一团糟

c++ openai-api fractals
© www.soinside.com 2019 - 2024. All rights reserved.