Tôi có mã đặt Mandelbrot sau đây trong C. Tôi đang tính toán và tạo tệp .ppm cho hình ảnh fractal cuối cùng. Vấn đề là hình ảnh fractal của tôi bị lộn ngược, có nghĩa là nó được xoay 90 độ. Bạn có thể kiểm tra bằng cách thực thi mã của tôi: ./mandel> test.ppmCải thiện mã bộ Mandelbrot của tôi
Mặt khác, tôi cũng muốn thay đổi màu sắc. Tôi muốn đạt được hình ảnh fractal này:
vấn đề cuối cùng của tôi là mã của tôi không kiểm tra thời gian chạy của mã của tôi. Tôi có mã cho phần này quá, nhưng khi thực thi mã kết thúc nó không in thời gian chạy. Nếu ai đó có thể thực hiện các thay đổi thích hợp cho mã của tôi và giúp tôi đạt được hình ảnh fractal này và làm cho thời gian trôi qua được hiển thị, tôi sẽ rất vui.
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
void color(int red, int green, int blue)
{
fputc((char)red, stdout);
fputc((char)green, stdout);
fputc((char)blue, stdout);
}
int main(int argc, char *argv[])
{
int w = 600, h = 400, x, y;
//each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin
double pr, pi; //real and imaginary part of the pixel p
double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z
double zoom = 1, moveX = -0.5, moveY = 0; //you can change these to zoom and change position
int maxIterations = 1000;//after how much iterations the function should stop
clock_t begin, end;
double time_spent;
printf("P6\n# CREATOR: E.T/mandel program\n");
printf("%d %d\n255\n",w,h);
begin = clock();
//loop through every pixel
for(x = 0; x < w; x++)
for(y = 0; y < h; y++)
{
//calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
pr = 1.5 * (x - w/2)/(0.5 * zoom * w) + moveX;
pi = (y - h/2)/(0.5 * zoom * h) + moveY;
newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0
//"i" will represent the number of iterations
int i;
//start the iteration process
for(i = 0; i < maxIterations; i++)
{
//remember value of previous iteration
oldRe = newRe;
oldIm = newIm;
//the actual iteration, the real and imaginary part are calculated
newRe = oldRe * oldRe - oldIm * oldIm + pr;
newIm = 2 * oldRe * oldIm + pi;
//if the point is outside the circle with radius 2: stop
if((newRe * newRe + newIm * newIm) > 4) break;
}
color(i % 256, 255, 255 * (i < maxIterations));
}
end = clock();
time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
printf("Elapsed time: %.2lf seconds.\n", time_spent);
return 0;
}
Cảm ơn! Nó giải quyết vấn đề của tôi. Nhưng có một lỗi truyền nhỏ liên quan đến dấu ngoặc đơn ở phần cuối của mã của bạn. Nếu bạn có thể sửa nó vì lợi ích của người khác, nó có thể là tuyệt vời. –
@erkant, Ý bạn là double (maxIterations)? Nó hoạt động tốt với tôi với gcc. Có cái gì khác? –
Vâng, tôi đã nói về nó. Nó không làm việc cho tôi, tôi đã phải sử dụng dấu ngoặc đơn như ((đôi) maxIterations). –