计算公式和方法我是直接抄Matrix67 数学笔记里面的
神奇的分形艺术(四):Julia集和Mandelbrot集
本来昨天还可以访问,今天不知为何404了
因为这套公式是直接按理论的方法计算,没有优化,运行消耗不少,所以直接就改小了生成图像的尺寸。
按 4 6 8 2 左右上下移动, 按5放大 按0缩小
运行环境:ActivePerl, V5.16, for windows
依赖的库:OpenGL 没安装的可以再activeperl的环境下执行:ppm install OpenGL- use IO::Handle;
- use OpenGL qw/ :all /;
- use OpenGL::Config;
- use Time::HiRes 'sleep';
- use feature 'state';
- STDOUT->autoflush(1);
-
- my ($re, $im);
- #my @c = ( 0.3, 0 ); #云
- #my @c = ( 0.279, 0.0 );
- my @c = ( 0.316, -0.033 );
- my @z = ();
-
-
- our $dtx = 200;
- our $dty = 100;
-
- our $moveDT = 30;
- our $movex = 0;
- our $movey = 0;
- our $left;
- our $right;
- our $bottom;
- our $top;
- our $scale = 1;
- our $depth = 100;
-
- &Main();
-
- sub display
- {
- my ($i, $j, $k);
-
- $left = -$dtx/2 + $movex;
- $right = $dtx/2 + $movex;
- $bottom = -$dty/2 + $movey;
- $top = $dty/2 + $movey;
-
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_POINTS);
- for ($i = $left; $i < $right; $i += 1)
- {
- for ($j = $bottom; $j < $top; $j += 1)
- {
- ($re, $im) = (
- $i / ($dty *$scale),
- $j / ($dty *$scale),
- );
-
- for ($k=0; $k <= $depth; $k++)
- {
- $xre = $re * $re;
- $xim = $im * $im;
- if ( ($xre + $xim ) > 4 )
- {
- last;
- }
- else
- {
- ($re, $im) = (
- $xre - $xim + $c[0],
- $re * $im * 2 + $c[1],
- );
- }
- }
-
- glColor3f( colors($k, 33.3) );
- glVertex3f( $i - $movex, $j - $movey, 0.0);
- }
- }
-
- glEnd();
- glutSwapBuffers();
- }
-
- sub colors
- {
- my ($value, $limit) = (shift, shift);
- my ($R, $G, $B);
- if ($value > $limit) {
- $R = 0.0;
- $value -= $limit;
- } else {
- $R = $value / $limit;
- }
- if ($value > $limit ) {
- $G = 0.0;
- $value -= $limit;
- } else {
- $G = $value / $limit;
- }
-
- $B = $value % $limit /$limit;
-
- return ($R, $G, $B);
- }
-
- sub init
- {
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glPointSize(1.0);
- }
-
- sub idle
- {
- sleep 0.01;
- glutPostRedisplay();
- }
-
- sub Reshape
- {
- my $half = 250.0;
- glViewport(0.0,0.0,500.0,500.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-$half, $half,-$half, $half,0.0,200.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0,0.0,100.0,0.0,0.0,0.0, 0.0,1.0,100.0);
- }
-
- sub hitkey
- {
- my $keychar = lc(chr(shift));
- if ($keychar eq 'q') {
- glutDestroyWindow($WinID);
- }
- if ($keychar eq '4') {
- $movex -= 50;
- glutPostRedisplay();
- } elsif ($keychar eq '6') {
- $movex += $moveDT;
- glutPostRedisplay();
- } elsif ($keychar eq '8') {
- $movey += $moveDT;
- glutPostRedisplay();
- } elsif ($keychar eq '2') {
- $movey -= $moveDT;
- glutPostRedisplay();
- } elsif ($keychar eq '5') {
- $scale *= 1.1;
- $movex *= 1.1;
- $movey *= 1.1;
- glutPostRedisplay();
-
- } elsif ($keychar eq '0') {
- $scale /= 1.1;
- $movex /= 1.1;
- $movey /= 1.1;
- glutPostRedisplay();
-
- }
- }
-
- sub Main
- {
- glutInit();
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE );
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(1,1);
- our $WinID = glutCreateWindow("title");
- &init();
- glutDisplayFunc(\&display);
- glutReshapeFunc(\&Reshape);
- glutKeyboardFunc(\&hitkey);
- glutIdleFunc(\&idle);
- glutMainLoop();
- }
复制代码
|