gpt4 book ai didi

javascript - 生成的 Julia 集与维基百科图像之间的差异

转载 作者:行者123 更新时间:2023-11-29 19:09:18 25 4
gpt4 key购买 nike

我写了一个小的 javascript 程序来查看 julia 集图像 [link] .我一直在实现多个迭代算法(不仅仅是 z^2 + c)并且最近实现了 exp(z)。但是,我的输出与维基百科页面图像不同。这是 c 值 -0.65 的比较:

我的形象: My julia set

维基百科图片 enter image description here

显然,他们的输出更加丰富多彩,显示了更多细节!我在想这可能是我的实现错误,或者我的着色算法?或者维基百科的结果是通过一些不同的方式生成的吗?下面是一个仅包含 e^z + c 算法和相关函数的 MVE。如果您想免费试用整个程序,请访问 on my site .

//globals
var MAXITERATION = 2500;
var BOUNDARY = 4;
var CANVASID = "juliaDraw";
var CANVAS = document.getElementById("juliaDraw");
var CONTEXT = document.getElementById("juliaDraw").getContext('2d');
var HEIGHT = 750;
var WIDTH = 750;
var CONVERGENCEITERCOUNT = 2500;

/** Complex number functions **/

function complexNum(real, imaginary) {
this.real = real;
this.imaginary = imaginary;
return this;
}

// This is the bit that might be a problem but it seems to return correct results?
function raiseNumberToComplexPower(x, c) {
var s = Math.pow(x, c.real);
var pow = c.imaginary * Math.log(x);
var num = new complexNum(Math.cos(pow), Math.sin(pow));
return scalarComplex(s, num);
}

function addComplex(c1, c2) {
var real = c1.real + c2.real;
var imaginary = c1.imaginary + c2.imaginary;
return new complexNum(real, imaginary);
}

function multComplex(c1, c2) {
var real = (c1.real * c2.real) - (c1.imaginary * c2.imaginary);
var imaginary = (c1.real * c2.imaginary) + (c2.real * c1.imaginary);
return new complexNum(real, imaginary);
}

function scalarComplex(s, c) {
return new complexNum(c.real * s, c.imaginary * s);
}

function getComplexModulus(c) {
return Math.sqrt((c.real * c.real) + (c.imaginary * c.imaginary));
}

/** Drawing and manipulation **/

function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while (i--) arr[length - 1 - i] = createArray.apply(this, args);
}
return arr;
}

function drawJulia() {
CONTEXT.clearRect(0, 0, WIDTH, HEIGHT);
var start = new complexNum(-2, 2);
var c = new complexNum(readInput('realValue') * 1, readInput('imagValue') * 1);
STARTPOS = {
real: -2,
imaginary: 2
}
RANGE = 4;
plotJuliaSet(CANVASID, c);
}

function plotJuliaSet(canvasID, c) {
var complexNumberArray = createArray(WIDTH + 1, HEIGHT + 1);
var doesPointEscapeArray = createArray(WIDTH + 1, HEIGHT + 1);
ITERALGO = exponential;
for (var x = 0; x <= WIDTH; x++) {
for (var y = 0; y <= HEIGHT; y++) {
complexNumberArray[x][y] = new coordsToComplex({
x: x,
y: y
});
complexNumberArray[x][y] = complexNumberArray[x][y];
doesPointEscapeArray[x][y] = doesPointEscape(c, complexNumberArray[x][y]);
if (doesPointEscapeArray[x][y] >= 0) {
drawPointOnCanvas(x, y, getColor(doesPointEscapeArray[x][y]));
} else {
drawPointOnCanvas(x, y, 'black');
}
}
}
console.log('done');
}

function doesPointEscape(c, complexNum) {
var iterations = 0;
var iterationsToEscape = -1;
var escaped = false;
while ((!escaped) && (iterations < MAXITERATION)) {
if (getComplexModulus(complexNum) > BOUNDARY) {
escaped = true;
iterationsToEscape = iterations;
}
complexNum = ITERALGO(complexNum, c);
iterations++;
}
return iterationsToEscape;
}

function exponential(complexNum, c) {
// e^z + c
return addComplex(raiseNumberToComplexPower(Math.E, complexNum), c);
}

function drawPointOnCanvas(x, y, color) {
CONTEXT.fillStyle = color;
CONTEXT.fillRect(x, y, 1, 1);
}

function getColor(iterations) {
//console.log("Iterations: "+getBaseLog(iterations+1,255));
var color = "rgb(" + Math.floor((8 * iterations) % 255) + "," + Math.floor(2 * iterations % 255) + "," + Math.floor(255 - ((8 * iterations) % 255)) + ")";
//console.log(color);
return color;
}

function coordsToComplex(coordinates) {
return {
real: ((coordinates.x / WIDTH) * RANGE + STARTPOS.real),
imaginary: ((coordinates.y / HEIGHT) * -RANGE + STARTPOS.imaginary)
};
}

function complexToCoords(c) {
return {
x: ((c.real - STARTPOS.real) / (RANGE)) * WIDTH,
y: ((c.imaginary - STARTPOS.imaginary) / -(RANGE)) * HEIGHT
};
}

function readInput(inputID) {
return document.getElementById(inputID).value;
}
.desc {
float: right;
width: 300px;
}
#juliaDraw {
border: 1px dotted;
float: left;
}
.canvasWrapper canvas {
position: absolute;
top: 0;
left: 0;
}
<div class="desc">
<h1>Julia Set Viewer</h1>
<form>
<label>Real:
<input type="text" id="realValue" value="-0.65">
</label>
<br>
<label>Imag:
<input type="text" id="imagValue" value="0">
</label>
<input type="button" onClick="drawJulia()" value="Draw">
</form>
</div>
<canvas id="juliaDraw" width=750 height=750 onClick="drawZoomJulia()"></canvas>

最佳答案

这个问题实际上是一个数学问题。具体来说,您正在使用为指数函数上的多项式构建的逃逸准则。在所有情况下,您似乎都在迭代,直到迭代的绝对值超过 BOUNDARY 并且 BOUNDARY 一开始就设置为 4。维基百科图像显然使用了更大的转义值。在下面两幅图中,我们比较了逃逸半径为 4 和逃逸半径为 100;更大的逃逸半径更像维基百科的图像:

enter image description here

但是,坦率地说,维基百科图像也不正确。 Julia set pictures 背后的全部要点是试图将复平面分解为两组:一组动态简单,另一组动态复杂。我们迭代多项式直到绝对值变大,因为所有 绝对值大的点都会逃逸到无穷大。对于您的函数 exp(z)-0.65,情况并非如此。例如,如果 z=-100,则 abs(z) 相当大,但 exp(-100)-0.65 非常接近-0.65。就指数函数的绝对值而言,没有好的逃逸准则。

什么做得好是迭代你的函数,直到它的真实部分很大。就像在复平面的右侧有一个逃生 channel 。如果我们迭代您的函数直到实部超过 100,我们会得到如下结果:

enter image description here

关于javascript - 生成的 Julia 集与维基百科图像之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com