Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Удалова #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions Fractal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
let Scale = 0;
let Field = {
left: 0,
top: 0,
right: 0,
bottom: 0
};

function mousePress() {
canvas.addEventListener("mousedown",
function (e) {
reScale(canvas, e);
},
false);
}

function reScale(canvas, e) {
let mouseCoords = canvas.relMouseCoords(e);
let x = mouseCoords.x * (Field.right - Field.left) / (canvas.width - 1) + Field.left;
let y = mouseCoords.y * (Field.bottom - Field.top) / (canvas.height - 1) + Field.top;
if (e.button === 0) {
Scale /= 1.5;
}
if (e.button === 2) {
Scale *= 1.5;
}
run(x, y);
}

function resetScale() {
Scale = 2;
}

function run(centerX, centerY) {
let fractalType = Choose.ChooseFractal.options[Choose.ChooseFractal.selectedIndex].value;
let colorType = Choose.ChooseColor.options[Choose.ChooseColor.selectedIndex].value;
Field.left = centerX - Scale;
Field.top = centerY - Scale;
Field.right = centerX + Scale;
Field.bottom = centerY + Scale;
let n = parseInt(document.getElementById('n').value);
draw(n, fractalType, colorType);
}

function draw(n, fractalType, colorType) {
let canvas = document.getElementById("canvas");
let context = canvas.getContext('2d');
let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
let width = canvas.width;
let height = canvas.height;
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let i = x * (Field.right - Field.left) / (width - 1) + Field.left;
let j = y * (Field.bottom - Field.top) / (height - 1) + Field.top;
let pointData = choseFractal(i, j, n, fractalType);
let color = choseColor(pointData, n, fractalType, colorType);
putColor(x, y, color, width, imageData);
}
}
context.putImageData(imageData, 0, 0);
}

function choseFractal(x, y, n, fractalType) {
switch (fractalType) {
case 'newtonPool':
return getNewtonAttractor(x, y, n, 0);
case 'mandelbrotSet':
return getMandelbrotAttractor(x, y, n);
case 'juliaSet':
let re = parseFloat(document.getElementById('re').value);
let im = parseFloat(document.getElementById('im').value);
return getJuliaAttractor(x, y, re, im, n);
}
}

function getNewtonAttractor(x, y, n, itter) {
let cos = Math.cos(Math.PI / 3);
let sin = Math.sin(Math.PI / 3);

if (n === 0)
return {
attr: 0,
i: itter
};
if (isCloseToAttr(x, y, 1, 0))
return {
attr: 1,
i: itter
};
if (isCloseToAttr(x, y, -cos, sin))
return {
attr: 2,
i: itter
};
if (isCloseToAttr(x, y, -cos, -sin))
return {
attr: 3,
i: itter
};
let x2 = x * x;
let y2 = y * y;
let x1 = 2 / 3 * x + (x2 - y2) / (3 * (x2 + y2) * (x2 + y2));
let y1 = 2 / 3 * y * (1 - x / ((x2 + y2) * (x2 + y2)));
return getNewtonAttractor(x1, y1, n - 1, itter + 1);
}

function isCloseToAttr(x1, y1, x2, y2) {
return Math.abs(x2 - x1) <= 0.0001 && Math.abs(y2 - y1) <= 0.0001;
}

function getMandelbrotAttractor(x, y, n) {
let x0 = 0;
let y0 = 0;
let x1 = 0;
let y1 = 0;
let k = 0;
while (k < n) {
x1 = x0 * x0 - y0 * y0 + x;
y1 = 2 * x0 * y0 + y;
if (x1 * x1 + y1 * y1 > 4)
return {
i: k
};
x0 = x1;
y0 = y1;
k++;
}
return {
i: 0
};
}

function getJuliaAttractor(x, y, re, im, n) {
let x0 = x;
let y0 = y;
let x1 = 0;
let y1 = 0;
let k = 0;
while (k < n) {
if (x0 * x0 + y0 * y0 > 4)
return {
i: k
};
x1 = x0 * x0 - y0 * y0 + re;
y1 = 2 * x0 * y0 + im;
x0 = x1;
y0 = y1;
k++;
}
return {
i: 0
};
}

function choseColor(pointData, n, fractalType, colorType) {
switch (colorType) {
case 'classicPaint':
return paintClassic(pointData, fractalType);
case 'levelPaint':
return paintLevels(n, pointData.i);
case 'zebraPaint':
return paintZebra(pointData.i);
case 'newtonHybridPaint':
return paintNewtonHybrid(pointData.attr, pointData.i, n)
}
}

function paintClassic(point, fractalType) {
if (fractalType === "newtonPool") {
switch (point.attr) {
case 0:
return [0, 0, 0, 0];
case 1:
return [255, 0, 0, 255];
case 2:
return [0, 255, 0, 255];
case 3:
return [0, 0, 255, 255];
}
} else return point.i === 0 ? [0, 0, 0, 255] : [255, 255, 255, 255];
}

function paintLevels(n, itter) {
let brightness = n > 1 ? 255 * itter * 4 / (n - 1) : 255;
return [brightness, brightness, brightness, 255];
}

function paintZebra(itter) {
let color;
itter % 2 === 0 ? color = [0, 0, 0, 255] : color = [255, 255, 255, 255];
return color;
}

function paintNewtonHybrid(attr, k, n) {
let point = [0, 0, 0, 255];
if (attr == 1) point[0] = n > 1 ? 255 * k / (n - 1) : 255;
if (attr == 2) point[1] = n > 1 ? 255 * k / (n - 1) : 255;
if (attr == 3) point[2] = n > 1 ? 255 * k / (n - 1) : 255;
return point;
}

function putColor(x, y, color, width, imageData) {
let index = (x + y * width) * 4;
imageData.data[index + 0] = color[0];
imageData.data[index + 1] = color[1];
imageData.data[index + 2] = color[2];
imageData.data[index + 3] = color[3];
}
157 changes: 78 additions & 79 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,80 +1,79 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache"/>

<script>
HTMLCanvasElement.prototype.relMouseCoords = function (event) {
var totalOffsetX = 0;
var totalOffsetY = 0;
var canvasX = 0;
var canvasY = 0;
var currentElement = this;

do {
totalOffsetX += currentElement.offsetLeft;
totalOffsetY += currentElement.offsetTop;
}
while (currentElement = currentElement.offsetParent)

canvasX = event.pageX - totalOffsetX;
canvasY = event.pageY - totalOffsetY;

// Fix for variable canvas width
canvasX = Math.round( canvasX * (this.width / this.offsetWidth) );
canvasY = Math.round( canvasY * (this.height / this.offsetHeight) );

return {x:canvasX, y:canvasY}
}

//пример рисования произвольного множества точек
function example(canvas) {
var canvasHeight = parseInt(canvas.getAttribute("height"));
var canvasWidth = parseInt(canvas.getAttribute("width"));

//создаем 2d context
var context = canvas.getContext('2d');

//создаем "буфер" imageData, в который будем класть новую информацию о цветах
var imageData = context.createImageData(canvasWidth, canvasHeight);
for (var i = 0; i < canvasWidth; i++) {
for (var j = 0; j < canvasHeight; j++) {
var red = 0;
var green = 0;
var blue = (i + j) % 255;
if (i % 20 == 0)
red = 150;
if (j % 20 == 0)
green = 150;
var opacity = 255;

imageData.data[4*(i + canvasWidth*j) + 0] = red;
imageData.data[4*(i + canvasWidth*j) + 1] = green;
imageData.data[4*(i + canvasWidth*j) + 2] = blue;
imageData.data[4*(i + canvasWidth*j) + 3] = opacity;
}
}
//заполненный "буфер" imageData передаем в context для вывода на экран
context.putImageData(imageData, 0, 0);
}

function mouseDownHandler(canvas, e) {
var coords = canvas.relMouseCoords(e);
alert("x=" + coords.x + " " + "y=" + coords.y);
}

//вызывается после загрузки body
function run () {
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown",
function(e) { mouseDownHandler(canvas, e); },
false);
example(canvas);
}
</script>

<body onload="run()">
<canvas height='200' width='200' id='canvas'></canvas>
</body>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<script src="Fractal.js" type="text/javascript" charset="utf-8"></script>
<script>
HTMLCanvasElement.prototype.relMouseCoords = function (event) {
var moveX = 0;
var moveY = 0;
var canvasX = 0;
var canvasY = 0;
var currentElement = this;
do {
moveX += currentElement.offsetLeft;
moveY += currentElement.offsetTop;
}
while (currentElement = currentElement.offsetParent)
canvasX = event.pageX - moveX;
canvasY = event.pageY - moveY;
canvasX = Math.round(canvasX * (this.width / this.offsetWidth));
canvasY = Math.round(canvasY * (this.height / this.offsetHeight));
return {
x: canvasX,
y: canvasY
}
}
</script>
</head>
<form name="Choose">
<body onload="mousePress();">
<table>
<tr>
<td>Тип фрактала:</td>
</tr>
<tr>
<td>
<select name="ChooseFractal">
<option value='newtonPool'>Бассейн Ньютона</option>
<option value='mandelbrotSet'>Множество Мандельброта</option>
<option value='juliaSet'>Множество Жюлиа</option>
</select>
</td>
</tr>
<tr>
<td>Тип расскраски:</td>
</tr>
<tr>
<td>
<select name="ChooseColor">
<option value='classicPaint'>Классический</option>
<option value='levelPaint'>Уровни</option>
<option value='zebraPaint'>Зебра</option>
<option value='newtonHybridPaint'>Гибриная</option>
</select>
</td>
</tr>
<tr>
<td>Характеризующее множество Жюлиа значение C:</td>
</tr>
<tr>
<td>
Re:<input type="number" id="re" name="re" value="-0.12">
Im: <input type="number" id="im" name="im" value="0.74">
</td>
</tr>
<tr>
<td colspan="2">Максимальная итерация <input type="number" id="n" name="n" value="150"></td>
</tr>
<tr>
<td>
<button type="button" onclick="resetScale(); run(0, 0)">Нарисовать</button>
</td>
</tr>
</table>
<canvas height='500' width='500' id='canvas' oncontextmenu="return false;"></canvas>
</body>
</form>
</html>