next up previous
Next: 練習問題 Up: 計算機言語 I 第8回 Fractals Previous: マンデルブロ(Mandelbrot)図形

Source code

/* File name 8-2.c */
#include <stdio.h>

#define BASE 5
#define SIZE 512
#define BOUNDS 1.0

int hantei(double real, double imaginary);

main()
{
    double interval=2*BOUNDS/SIZE;
    int i, j, result, red, green, blue;
    FILE *fp;

    fp=fopen("mandelbrot.ppm", "w");
    fprintf(fp, "P6\n%d %d\n%d\n", SIZE, SIZE, BASE-1);

    for(i=0; i < SIZE; i++){
        for(j=0; j < SIZE; j++){
            result = hantei(-1.5*BOUNDS+j*interval, BOUNDS-i*interval);
            red=result%BASE;
            green=(result/BASE)%BASE;
            blue=(result/BASE)/BASE;
            fprintf(fp, "%c%c%c", red,green,blue); 
        }
    }
}

int hantei(double real, double imaginary)
{
    int answer=0;
    double x=0.0;        
    double y=0.0;        
    double nextx;

    while(answer<(BASE*BASE*BASE-1)&& x*x+y*y < 25.0){ 
            nextx = x*x-y*y+real;
            y = 2*x*y +imaginary;
            x=nextx;
            answer++;
    }
    return answer;
}



Next: 練習問題 Up: 計算機言語 I 第8回 Fractals Previous: マンデルブロ(Mandelbrot)図形