ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-找不到为Mandelbrot设置颜色的方法

2019-10-24 18:03:34  阅读:305  来源: 互联网

标签:fractals mandelbrot java math processing


尽管在将Mandelbrot设置为“模糊”模式并停止之前无法放大很远,但我还是成功为其着色.我通过增加max_iteration来解决此问题,这可以工作,但是在* 1放大倍率下我得到的颜色很少,并且只有当我放大时才会出现很多颜色.我理解为什么会发生这种情况,因为在“真” Mandelbrot集中没有颜色并且增加max_iterations使其更接近于此.但是我的问题是,在YouTube上进行缩放时,如何在整个缩放过程​​中呈现出美丽的色彩,同时又能永久缩放呢?

我尝试过在线查找任何地方,但找不到解决方案,当我查看这些youtube缩放的说明时,它们似乎几乎没有提供有关缩放方式的任何信息.

这只是绘制Mandelbrot集的代码部分.下面的代码是在处理过程中编写的,它是带有附加可视库的java.您可以在这里找到有关该程序的更多信息:https://processing.org/

//m is max_iterations
//mb is the function which calculates how many iterations each point took to escape to infinity. I won't be including the function since I know it works fine and it's quite messy.
//i'm using a HSB/HSV system to draw the mandelbrot

hue=(mb(x, y, m)*360)/m;
sat=255;
if (mb(x, y, m)<m) {
  val=255;
} 
else {
  val=0;
}

stroke(hue,sat,val);
point(x, y);

我知道为什么会发生我的问题,但我不知道如何解决.

这是一张max_iterations较低且已缩小的图像,如您所见,它非常彩色:
low max zoomed out

这是一张具有低max_iterations并略微放大的图像,如您所见,它很无聊且不是很彩色:
low max zoomed in slightly

这是一张具有高max_iterations并缩小的图像,如您所见,它并不是很彩色:
high max zoomed out

这是一张具有高max_iterations并放大的图像,如您所见,它非常彩色:
high max zoomed in

解决方法:

首先看一下相关的质量检查:

> Mandelbrot Set – Color Spectrum Suggestions?

主要思想是使用直方图将颜色梯度更有效地分配给使用的索引,而不是将大量颜色均匀浪费在未使用的索引上.它还使用了一个特殊的视觉上令人愉悦的渐变函数:

> RGB values of visible spectrum

其他人建议的最大动态迭代次数只会影响整体性能和缩放细节.但是,如果您想要没有缩放的漂亮颜色,则需要计算浮点迭代次数,这也称为Mandelbrot Escape.有一种数学方法可以根据等式的最后一个子结果计算迭代计数的小数部分.有关更多信息,请参见:

> Renormalizing the Mandelbrot Escape

但是,我从未尝试过,因此请以偏见阅读此书:如果我没看错,您想要的是计算此方程式:

mu = m + frac = n + 1 - log (log  |Z(n)|) / log 2

其中n是您的迭代计数,Z(n)是您要迭代的方程的复数域子结果.所以现在从现在是浮点的mu而不是n中计算颜色…

[Edit2]基于上面链接的带有小数转义的GLSL mandelbrot

我添加了分数转义并修改了直方图多遍重新着色以匹配新的输出…

顶点:

// Vertex
#version 420 core
layout(location=0) in vec2 pos;     // glVertex2f <-1,+1>
out smooth vec2 p;                  // texture end point <0,1>
void main()
    {
    p=pos;
    gl_Position=vec4(pos,0.0,1.0);
    }

分段:

// Fragment
#version 420 core
uniform vec2 p0=vec2(0.0,0.0);      // mouse position <-1,+1>
uniform float zoom=1.000;           // zoom [-]
uniform int  n=100;                 // iterations [-]
uniform int  sh=7;                  // fixed point accuracy [bits]
uniform int  multipass=0;           // multi pass?
in smooth vec2 p;
out vec4 col;

const int n0=1;                     // forced iterations after escape to improve precision

vec3 spectral_color(float l)        // RGB <0,1> <- lambda l <400,700> [nm]
    {
    float t;  vec3 c=vec3(0.0,0.0,0.0);
         if ((l>=400.0)&&(l<410.0)) { t=(l-400.0)/(410.0-400.0); c.r=    +(0.33*t)-(0.20*t*t); }
    else if ((l>=410.0)&&(l<475.0)) { t=(l-410.0)/(475.0-410.0); c.r=0.14         -(0.13*t*t); }
    else if ((l>=545.0)&&(l<595.0)) { t=(l-545.0)/(595.0-545.0); c.r=    +(1.98*t)-(     t*t); }
    else if ((l>=595.0)&&(l<650.0)) { t=(l-595.0)/(650.0-595.0); c.r=0.98+(0.06*t)-(0.40*t*t); }
    else if ((l>=650.0)&&(l<700.0)) { t=(l-650.0)/(700.0-650.0); c.r=0.65-(0.84*t)+(0.20*t*t); }
         if ((l>=415.0)&&(l<475.0)) { t=(l-415.0)/(475.0-415.0); c.g=             +(0.80*t*t); }
    else if ((l>=475.0)&&(l<590.0)) { t=(l-475.0)/(590.0-475.0); c.g=0.8 +(0.76*t)-(0.80*t*t); }
    else if ((l>=585.0)&&(l<639.0)) { t=(l-585.0)/(639.0-585.0); c.g=0.84-(0.84*t)           ; }
         if ((l>=400.0)&&(l<475.0)) { t=(l-400.0)/(475.0-400.0); c.b=    +(2.20*t)-(1.50*t*t); }
    else if ((l>=475.0)&&(l<560.0)) { t=(l-475.0)/(560.0-475.0); c.b=0.7 -(     t)+(0.30*t*t); }
    return c;
    }

void main()
    {
    int i,j,N;
    vec2 pp;
    float x,y,q,xx,yy,mu;
    pp=(p/zoom)-p0;         // y (-1.0, 1.0)
    pp.x-=0.5;              // x (-1.5, 0.5)
    for (x=0.0,y=0.0,xx=0.0,yy=0.0,i=0;(i<n-n0)&&(xx+yy<4.0);i++)
        {
        q=xx-yy+pp.x;
        y=(2.0*x*y)+pp.y;
        x=q;
        xx=x*x;
        yy=y*y;     
        }
    for (j=0;j<n0;j++,i++)  // 2 more iterations to diminish fraction escape error
        {
        q=xx-yy+pp.x;
        y=(2.0*x*y)+pp.y;
        x=q;
        xx=x*x;
        yy=y*y;
        }
    mu=float(i)-log(log(sqrt(xx+yy))/log(2.0));
    mu*=float(1<<sh); i=int(mu);
    N=n<<sh;
    if (i>N) i=N;
    if (i<0) i=0;

    if (multipass!=0)
        {
        // i
        float r,g,b;
        r= i     &255; r/=255.0;
        g=(i>> 8)&255; g/=255.0;
        b=(i>>16)&255; b/=255.0;
        col=vec4(r,g,b,255);
        }
    else{
        // RGB
        q=float(i)/float(N);
        q=pow(q,0.2);
        col=vec4(spectral_color(400.0+(300.0*q)),1.0);
        }
    }

CPU端C / VCL代码:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "gl\\OpenGL3D_double.cpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
OpenGLscreen scr;
GLSLprogram shd;
float mx=0.0,my=0.0,mx0=0.0,my0=0.0,mx1=0.0,my1=0.0;
TShiftState sh0,sh1;
int xs=1,ys=1;
float zoom=1.000;
int sh=7;
int N=256;
int _multi=0;
unsigned int queryID[2];
#define multi_pass
OpenGLtexture txr;
//---------------------------------------------------------------------------
DWORD spectral_color(float l)        // RGB <0,1> <- lambda l <400,700> [nm]
    {
    float t;  float r,g,b; DWORD c,x; r=0.0; g=0.0; b=0.0;
         if ((l>=400.0)&&(l<410.0)) { t=(l-400.0)/(410.0-400.0); r=    +(0.33*t)-(0.20*t*t); }
    else if ((l>=410.0)&&(l<475.0)) { t=(l-410.0)/(475.0-410.0); r=0.14         -(0.13*t*t); }
    else if ((l>=545.0)&&(l<595.0)) { t=(l-545.0)/(595.0-545.0); r=    +(1.98*t)-(     t*t); }
    else if ((l>=595.0)&&(l<650.0)) { t=(l-595.0)/(650.0-595.0); r=0.98+(0.06*t)-(0.40*t*t); }
    else if ((l>=650.0)&&(l<700.0)) { t=(l-650.0)/(700.0-650.0); r=0.65-(0.84*t)+(0.20*t*t); }
         if ((l>=415.0)&&(l<475.0)) { t=(l-415.0)/(475.0-415.0); g=             +(0.80*t*t); }
    else if ((l>=475.0)&&(l<590.0)) { t=(l-475.0)/(590.0-475.0); g=0.8 +(0.76*t)-(0.80*t*t); }
    else if ((l>=585.0)&&(l<639.0)) { t=(l-585.0)/(639.0-585.0); g=0.84-(0.84*t)           ; }
         if ((l>=400.0)&&(l<475.0)) { t=(l-400.0)/(475.0-400.0); b=    +(2.20*t)-(1.50*t*t); }
    else if ((l>=475.0)&&(l<560.0)) { t=(l-475.0)/(560.0-475.0); b=0.7 -(     t)+(0.30*t*t); }
    r*=255.0; g*=255.0; b*=255.0;
    x=r; c =x;
    x=g; c|=x<<8;
    x=b; c|=x<<16;
    return c;
    }
//---------------------------------------------------------------------------
void gl_draw()
    {
    scr.cls();

    // matrix for old GL rendering
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();


    // GLSL uniforms
    shd.bind();
    shd.set2f("p0",mx,my);          // pan position
    shd.set1f("zoom",zoom);         // zoom
    shd.set1i("n",N);               // iterations
    shd.set1i("sh",sh);             // fixed point accuracy (shift)
    shd.set1i("multipass",_multi);  // single/multi pass

    // issue the first query
    // Records the time only after all previous
    // commands have been completed
    glQueryCounter(queryID[0], GL_TIMESTAMP);

    // QUAD covering screen
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_QUADS);
    glVertex2f(-1.0,+1.0);
    glVertex2f(-1.0,-1.0);
    glVertex2f(+1.0,-1.0);
    glVertex2f(+1.0,+1.0);
    glEnd();
    shd.unbind();

    // [multipas]
    if (_multi)
        {
        float t,m,n=N<<sh;
        DWORD *hist=new DWORD[n+1];
        int sz=txr.xs*txr.ys,i,j;
        // get rendered image
        glReadPixels(0,0,txr.xs,txr.ys,GL_RGBA,GL_UNSIGNED_BYTE,txr.txr);
        // compute histogram
        for (i=0;i<=n;i++) hist[i]=0;
        for (i=0;i<sz;i++) hist[txr.txr[i]&0x00FFFFFF]++;
        // histogram -> used color index (skip holes)
        for (i=1,j=1;i<=n;i++)
         if (hist[i]){ hist[i]=j; j++; }
        // used color index -> color
        m=1.0/float(j); hist[0]=0x00000000;
        for (i=1;i<=n;i++)
         if (hist[i]){ t=hist[i]; t*=m; hist[i]=spectral_color(400.0+(300.0*t)); }
          else hist[i]=0x00000000;
        // recolor image
        for (i=0;i<sz;i++) txr.txr[i]=hist[txr.txr[i]&0x00FFFFFF];
        // render it back
        scr.cls();
        txr.bind();
        glColor3f(1.0,1.0,1.0);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0,1.0); glVertex2f(-1.0,+1.0);
        glTexCoord2f(0.0,0.0); glVertex2f(-1.0,-1.0);
        glTexCoord2f(1.0,0.0); glVertex2f(+1.0,-1.0);
        glTexCoord2f(1.0,1.0); glVertex2f(+1.0,+1.0);
        glEnd();
        txr.unbind();
        glDisable(GL_TEXTURE_2D);
        delete[] hist;
        }

    // issue the second query
    // records the time when the sequence of OpenGL
    // commands has been fully executed
    glQueryCounter(queryID[1], GL_TIMESTAMP);


    // GL driver info and GLSL log
    scr.text_init_pix(0.75);
    glColor4f(1.0,1.0,1.0,0.9);
    scr.text(glGetAnsiString(GL_VENDOR));
    scr.text(glGetAnsiString(GL_RENDERER));
    scr.text("OpenGL ver: "+glGetAnsiString(GL_VERSION));
    if (_multi) scr.text("Multi pass");
     else       scr.text("Single pass");
    if (shd.log.Length()!=41)
     for (int i=1;i<=shd.log.Length();) scr.text(str_load_lin(shd.log,i,true));
    scr.text_exit();

    scr.exe();
    scr.rfs();

    // wait until the results are available
    int e;
    unsigned __int64 t0,t1;
    for (e=0;!e;) glGetQueryObjectiv(queryID[0],GL_QUERY_RESULT_AVAILABLE,&e);
    for (e=0;!e;) glGetQueryObjectiv(queryID[1],GL_QUERY_RESULT_AVAILABLE,&e);
    glGetQueryObjectui64v(queryID[0], GL_QUERY_RESULT, &t0);
    glGetQueryObjectui64v(queryID[1], GL_QUERY_RESULT, &t1);
    Form1->Caption=AnsiString().sprintf("dt: %f ms p0:%.3fx%.3f zoom: %.1lf N:%i<<%i\n",(t1-t0)/1000000.0,mx,my,zoom,N,sh);
    }
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
    {
    scr.init(this);
    shd.set_source_file("","","","Mandelbrot_set.glsl_vert","Mandelbrot_set.glsl_frag");
    glGenQueries(2, queryID);
    // nice spirals
    _multi=1;
    zoom=300.0;
    mx  = 0.268;
    my  =-0.102;
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
    scr.exit();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
    {
    scr.resize();
    xs=ClientWidth;
    ys=ClientHeight;
    txr.resize(xs,ys);
    gl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
    {
    gl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X,int Y)
    {
    bool q0,q1;
    mx1=1.0-divide(X+X,xs-1);
    my1=divide(Y+Y,ys-1)-1.0;
    sh1=Shift;
    q0=sh0.Contains(ssLeft);
    q1=sh1.Contains(ssLeft);
    if (q1)
        {
        mx-=(mx1-mx0)/zoom;
        my-=(my1-my0)/zoom;
        }
    mx0=mx1; my0=my1; sh0=sh1;
    gl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,TShiftState Shift, int X, int Y)
    {
    FormMouseMove(Sender,Shift,X,Y);
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,TShiftState Shift, int X, int Y)
    {
    FormMouseMove(Sender,Shift,X,Y);
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled)
    {
    if (WheelDelta>0) zoom*=1.2;
    if (WheelDelta<0) zoom/=1.2;
    Handled=true;
    gl_draw();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
    {
    Caption=Key;
    if (Key==32){ _multi=!_multi; gl_draw(); }      // [Space]
    if (Key==33){ if (N<8192) N<<=1; gl_draw(); }   // [PgUp]
    if (Key==34){ if (N> 128) N>>=1; gl_draw(); }   // [PgDown]
    }
//---------------------------------------------------------------------------

这是单程分数转义n = 100 * 32:

fractional escape

这是单遍整数转义n = 100:

integer escape

如您所见,对于相同数量的迭代(100),分数转义要好得多.

最后,只有256次迭代和约300倍变焦,才是不错的多次通过(作为展示):

multi pass

与单次通过:

single pass

有关修改的一些说明:

我将sh小数部分添加到计数器(定点).因此,最大计数现在是n<< sh而不是n.我还添加了n0常数,以降低转义小数部分的错误.该链接建议使用2次迭代,但我认为1次迭代看起来更好(它也从对数方程中删除了i 1增量).迭代循环保持不变,我只是在其后添加相同的n0次迭代,然后计算小数转义码mu并将其转换为固定点(因为我的着色器输出整数). 多遍仅在CPU端代码上更改.它只是对使用过的索引重新索引,以使它们中没有孔,并使用可见光谱颜色重新着色. 这里演示:
> Win32 C++/GL/GLSL Mandelbrot set demo 32 bit float (old)
> Win32 C++/GL/GLSL Mandelbrot set demo 64 bit float (new)

标签:fractals,mandelbrot,java,math,processing
来源: https://codeday.me/bug/20191024/1922356.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有