计算Mandelbrot集点 [英] Calculate Mandelbrot set of points
问题描述
我需要完成该确定的该dertermine Mandelbrot集的点集的函数
我有一个结构
typedef结构
{
INT nb_lig,nb_col; / *尺寸* /
字符*像素; / *矩阵的计算linearisee德像素* /
} 图片;
和计算功能
静态无效calculer(图片* IM,INT nb_iter,双x_min,双x_max,双y_min,双Y_MAX){
双PASX =(x_max - x_min)/ IM - > nb_col;
双pasy =(Y_MAX - y_min)/ IM - > nb_lig; INT L,C; // 计算
为(L = 0; L&下;即时 - > nb_lig:L ++)
{
为(C = 0; C< IM - > nb_col; C ++)
{
// ....
}
}
}
在网上我发现这个code
为(CY = YMIN; CY和LT; YMAX; CY + = DXY){
对于(CX = XMIN; CX< XMAX; CX + = DXY){
ZX = 0.0;
ZY = 0.0;
N = 0;
而((ZX * ZX + ZY * ZY&所述; 4.0)及&放大器;!(N = UCHAR_MAX)){
new_zx = ZX ZX * - * ZY ZY + CX;
ZY = 2.0 * ZX * ZY + CY;
ZX = new_zx;
Ñ++;
}
写(1,&安培; N,的sizeof(N)); //结果写入到stdout
}
}
我将与我的与之相适应
会是什么样的ZX,ZY,CX,于我而言CY重新presents?
更新:
添加新功能后,我总是得到黑屏
这里是保存图像的fonction
静态无效sauvegarder(const的图像* IM,为const char *舍曼){ / * Enregistrement DE L'图像格式金ASCII.PPM'* /
无符号我;
FILE * F = FOPEN(舍曼,W);
fprintf中(F,P6 \\ n%D \\ N255 \\ n,即时通讯 - > nb_col,即时通讯 - > nb_lig);
对于(i = 0; I< IM - > nb_col * IM - > nb_lig;我++){
焦C = IM - >像素[I]
fprintf中(F,%C%C%C,C,C,C); / *单色相思(白)* /
}
FCLOSE(F);
}
您正在使用字符数组来存储迭代次数,然后你在 .ppm
使用每像素3字节,从而产生灰度图像文件。你认为256迭代足以产生Mandelbrot集的良好RA presentation,但是如果你开始放大,部分地区需要更高的极限。我会建议你使用无符号整型数组
,而不是再以这个数字转换成一些功能或一个简单的查找表的颜色。要做到这一点,我改变了你的code一点点,并增加了例如的main()
:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;typedef结构{
unsigned int类型nb_lig,nb_col; / *尺寸* /
无符号整型*像素; / *矩阵的计算linearisee德像素* /
无符号整型max_iter;
} 图片;静态无效calculer(图片* IM,双x_min,双x_max,双y_min,双Y_MAX){ 双PASX =晶圆厂(x_max - x_min)/ IM - > nb_col;
双pasy =晶圆厂(Y_MAX - y_min)/ IM - > nb_lig;
双CY =(y_min< Y_MAX)? Y_MAX:y_min; //保存时影像颠倒了...
双cx0 =(x_min< x_max)? x_min:x_max;
// 计算
对(无符号整数L = 0; L<&IM- GT; nb_lig,L ++){
双CX = cx0;
对(无符号整数C = 0; C<&IM- GT; nb_col; C ++){
双ZX = 0.0;
双ZY = 0.0;
双new_zx = 0.0;
unsigned int类型N = 0;
而((ZX * ZX + ZY * ZY&所述; 4.0)及及(N'下; IM-> max_iter)){
new_zx = ZX ZX * - * ZY ZY + CX;
ZY = 2.0 * ZX * ZY + CY;
ZX = new_zx;
++ N;
}
IM->像素[L * IM-> nb_col + C = N;
CX + = PASX;
}
CY - = pasy; //反转图片...
}
}静态无效sauvegarder(const的图像* IM,为const char *舍曼){ / * Enregistrement DE L'图像格式金ASCII.PPM'* /
无符号的I,J;
无符号整型瓦;
字符R,G,B; FILE * F = FOPEN(舍曼,WB);
如果(!F){
的printf(无法打开文件:%s \\ n,舍曼);
返回;
}
fprintf中(F,P6 \\ n%D \\ N255 \\ n,IM-> nb_col,IM-> nb_lig);
对于(i = 0; I<&IM- GT; nb_lig;我++){
为(J = 0; J<&IM- GT; nb_col; J ++){
W = IM->像素[我* IM-> nb_col + J]。
如果(W == IM-> max_iter){
R = 0; G = 0; B = 0;
}其他{
R = W%256;
G = 127 - W%128;
B = 127 + W 128%;
}
fprintf中(F,%C%C%C,R,G,B);
}
}
FCLOSE(F);
}INT主(INT ARGC,CHAR *的argv []){
图片曼德尔; mandel.nb_col = 768;
mandel.nb_lig = 512;
mandel.pixels =的malloc(mandel.nb_col * mandel.nb_lig * sizeof的(无符号整数));
mandel.max_iter = 2048; calculer(安培;曼德尔,-0.75,-0.69,0.235,0.275);
sauvegarder(安培;曼德尔mandel.ppm); 免费(mandel.pixels);
返回0;
}
输出图片(转换为巴纽
张贴在这里)为max_iter = 256:
设置max_iter = 2048:
I need to complete a function that determinate the set of points that dertermine Mandelbrot set I have a struct
typedef struct
{
int nb_lig, nb_col; /* Dimensions */
char * pixels; /* Matrice linearisee de pixels */
} Image;
And calculate function
static void calculer (Image * im, int nb_iter, double x_min, double x_max, double y_min, double y_max) {
double pasx = (x_max - x_min) / im -> nb_col;
double pasy = (y_max - y_min) / im -> nb_lig;
int l,c;
// Calculate
for (l = 0; l < im -> nb_lig; l ++)
{
for (c = 0; c < im -> nb_col; c ++)
{
//....
}
}
}
On the Internet I have found this code
for (cy = yMin; cy < yMax; cy += dxy) {
for (cx = xMin; cx < xMax; cx += dxy) {
zx = 0.0;
zy = 0.0;
n = 0;
while ((zx*zx + zy*zy < 4.0) && (n != UCHAR_MAX)) {
new_zx = zx*zx - zy*zy + cx;
zy = 2.0*zx*zy + cy;
zx = new_zx;
n++;
}
write (1, &n, sizeof(n)); // Write the result to stdout
}
}
I would adapt it with mine what would the zx, zy, cx , cy represents in my case ?
Update: After adding the new function I get always black screen
here is the fonction that saves the image
static void sauvegarder (const Image * im, const char * chemin) {
/* Enregistrement de l'image au format ASCII '.PPM' */
unsigned i;
FILE * f = fopen (chemin, "w");
fprintf (f, "P6\n%d %d\n255\n", im -> nb_col, im -> nb_lig);
for (i = 0; i < im -> nb_col * im -> nb_lig; i ++) {
char c = im -> pixels [i];
fprintf (f, "%c%c%c", c, c, c); /* Monochrome blanc (white) */
}
fclose (f);
}
You are using an array of char to store the number of iteration and then you save in a .ppm
file using 3 byte per pixel, resulting in a grey scale image. You assume that 256 iterations are enough to produce good rapresentation of the Mandelbrot set, but if you start zooming in, some areas require a much higher limit. I'll propose you to use an array of unsigned int
instead and then to convert this number into a color with some function or a simple look up table. To do that, I changed your code a little bit and added an example main()
:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
unsigned int nb_lig, nb_col; /* Dimensions */
unsigned int * pixels; /* Matrice linearisee de pixels */
unsigned int max_iter;
} Image;
static void calculer(Image * im, double x_min, double x_max, double y_min, double y_max ) {
double pasx = fabs(x_max - x_min) / im -> nb_col;
double pasy = fabs(y_max - y_min) / im -> nb_lig;
double cy = ( y_min < y_max ) ? y_max : y_min; // image is inverted when saved...
double cx0 = ( x_min < x_max ) ? x_min : x_max;
// Calculate
for (unsigned int l = 0; l < im->nb_lig; l++) {
double cx = cx0;
for (unsigned int c = 0; c < im->nb_col; c++) {
double zx = 0.0;
double zy = 0.0;
double new_zx = 0.0;
unsigned int n = 0;
while ( ( zx*zx + zy*zy < 4.0 ) && ( n < im->max_iter ) ) {
new_zx = zx*zx - zy*zy + cx;
zy = 2.0*zx*zy + cy;
zx = new_zx;
++n;
}
im->pixels[ l * im->nb_col + c ] = n;
cx += pasx;
}
cy -= pasy; // to invert picture...
}
}
static void sauvegarder(const Image * im, const char * chemin) {
/* Enregistrement de l'image au format ASCII '.PPM' */
unsigned i,j;
unsigned int w;
char r,g,b;
FILE * f = fopen (chemin, "wb");
if ( !f ) {
printf("Unable to open file: %s\n",chemin);
return;
}
fprintf (f, "P6\n%d %d\n255\n", im->nb_col, im->nb_lig);
for (i = 0; i < im->nb_lig; i++) {
for ( j = 0; j < im->nb_col; j++ ) {
w = im->pixels[ i*im->nb_col + j ];
if ( w == im->max_iter ) {
r = 0; g = 0; b = 0;
} else {
r = w % 256;
g = 127 - w % 128;
b = 127 + w % 128;
}
fprintf (f, "%c%c%c", r, g, b);
}
}
fclose (f);
}
int main(int argc, char *argv[]) {
Image mandel;
mandel.nb_col = 768;
mandel.nb_lig = 512;
mandel.pixels = malloc( mandel.nb_col * mandel.nb_lig * sizeof(unsigned int));
mandel.max_iter = 2048;
calculer(&mandel,-0.75,-0.69,0.235,0.275);
sauvegarder(&mandel,"mandel.ppm");
free(mandel.pixels);
return 0;
}
The output pictures (converted to .png
to post here) for max_iter = 256:
Setting max_iter = 2048:
这篇关于计算Mandelbrot集点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!