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

Algebraic Fractal was done #5

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
246 changes: 246 additions & 0 deletions AlgebrFrac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
var Plane = {
left: -2,
top: -2,
right: 2,
bottom: 2,
};

var Fractal;
var Coloring;
var N;
var ConstJulia= {x: -0.12, y: 0.72};
var Scale = 2;
var Center = {x:0, y:0};

function run() {
Fractal = Form.fractal.options[Form.fractal.selectedIndex].value;
Coloring = Form.color.options[Form.color.selectedIndex].value;
N = parseInt(document.getElementById("n").value);
ConstJulia.x = parseFloat(document.getElementById("x").value);
ConstJulia.y = parseFloat(document.getElementById("y").value);
Plane.left = Center.x - Scale;
Plane.right = Center.x + Scale;
Plane.top = Center.y - Scale;
Plane.bottom = Center.y + Scale;
draw();
}

function draw() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
var canvasHeight = parseInt(canvas.getAttribute("height"));
var canvasWidth = parseInt(canvas.getAttribute("width"));
var imageData = context.createImageData(canvasWidth, canvasHeight);



for (var i = 0; i < canvasWidth; i++) {
for (var j = 0; j < canvasHeight; j++) {
var point = getRealCoordinates(i, j, canvasWidth, canvasHeight);

var atr = getAttractor(point);
var color = getColor(atr);

imageData.data[4 * (i + canvasWidth * j) + 0] = color[0];
imageData.data[4 * (i + canvasWidth * j) + 1] = color[1];
imageData.data[4 * (i + canvasWidth * j) + 2] = color[2];
imageData.data[4 * (i + canvasWidth * j) + 3] = color[3];
}
}
context.putImageData(imageData, 0, 0);
}

function getRealCoordinates(i, j, width, height) {
var X = Plane.left + i * (Plane.right - Plane.left) / (width - 1);
var Y = Plane.top + j * (Plane.bottom - Plane.top) / (height - 1);
return { x: X, y: Y };
}

function getAttractor(point) {
switch (Fractal) {
case '1':
return getNewtonAttr(point, N, 0);
break;
case '2':
return getMandelbrotAttr(point);
break;
case '3':
return getJuliaAttr(point);
break;

}
}

function getNewtonAttr(point, n, it) {
var sin = Math.sin(Math.PI / 3);
var cos = Math.cos(Math.PI / 3);
if (n == 0)
return { attr: 0, iter: it };
if (checkLimit(point, 1, 0))
return { attr: 1, iter: it };
if (checkLimit(point, -cos, sin))
return { attr: 2, iter: it };
if (checkLimit(point, -cos, -sin))
return { attr: 3, iter: it };

var x = point.x * point.x;
var y = point.y * point.y;
var newX = 2 * point.x/3 + (x - y) / (3 * (x + y) * (x + y));
var newY = 2 * point.y * (1 - point.x /Math.pow((x + y),2)) /3;
var newP = { x: newX, y: newY };
return getNewtonAttr(newP, n - 1, it + 1);

}

function checkLimit(start, endX, endY) {
var eps = 0.0001;
var deltaX = Math.abs(start.x - endX);
var deltaY = Math.abs(start.y - endY);
return deltaX <= eps && deltaY <= eps;
}

function getMandelbrotAttree(point) {
var x = 0;
var y = 0;
var newX=0;
var newY=0;
for (var i = 0; i < N; i++) {
newX = x * x - y * y + point.x;
newY = 2 * x * y + point.y;
if (newX * newX + newX * newY > 4)
return i;
x = newX;
y = newY;
}
return 0;
}

function getMandelbrotAttr(point) {
var x = point.x;
var y = point.y;
var newX = 0;
var newY = 0;
for (var i = 0; i < N; i++) {
if (x * x + y * y > 4)
return i;
newX = x * x - y * y + point.x;
newY = 2 * x * y + point.y;
x = newX;
y = newY;
}
return 0;
}

function getJuliaAttr(point) {
var x = point.x;
var y = point.y;
var newX = 0;
var newY = 0;
for (var i = 0; i < N; i++) {
if (x * x + y * y > 4)
return i;
newX = x * x - y * y + ConstJulia.x;
newY = 2 * x * y + ConstJulia.y;
x = newX;
y = newY;
}
return 0;
}

function getColor(atr) {
switch (Coloring) {
case '1':
return getClassicColor(atr);
case '2':
return getLevelColor(atr);
case '3':
return getZebraColor(atr);
}

}

function getClassicColor(atr) {
if (Fractal == '1') {
switch (atr.attr) {
case 0:
return [0, 0, 0, 0];
break;
case 1:
return [255, 0, 0, 255];
break;
case 2:
return [0, 255, 0, 255];
break;
case 3:
return [0, 0, 255, 255];
break;
}
}
else{
if (atr == 0)
return [0, 0, 0, 255]
else
return [255, 255, 255, 255];
}


}

function getLevelColor(atr) {

if (Fractal == '1') {
var it = atr.iter;
}
else{
var it = atr;
}
var bright = 255;
if (N > 1)
bright = bright * it * 4 / (N - 1);
return [bright, bright, bright, bright];

}

function getZebraColor(atr) {
if (Fractal == '1') {
var it = atr.iter;
}
else{
var it = atr;
}

if (it % 2 == 0)
return [0, 0, 0, 255];
return [255, 255, 255, 255];
}

function mouseDownHandler(canvas, e) {
var сoords = canvas.relMouseCoords(e);
var i = сoords.x * (Plane.right - Plane.left) / (canvas.width - 1) + Plane.left;
var j = сoords.y * (Plane.bottom - Plane.top) / (canvas.height - 1) + Plane.top;
if (e.button === 0) {
Scale /= 1.5;
}
if (e.button === 2) {
Scale *= 1.5;
}
Center.x = i;
Center.y = j;
run();
}

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

function resetScale()
{
Scale = 2;
Center.x = 0;
Center.y = 0;
}

137 changes: 58 additions & 79 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,80 +1,59 @@
<!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>
<html>

<head>
<meta charset="utf-8">
<script src="AlgebrFrac.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>

<body onload="mousePress()">
<canvas oncontextmenu = "return false"; height='500' width='500' id='canvas'></canvas>
<div>
<div> <input id="n" type="text">N</div>
<div> <input id="x" type="text">Константа Жюлиа(х)</div>
<div> <input id="y" type="text">Константа Жюлиа(у)</div>
<form name="Form">
<select name="fractal">
<option value="1">Бассейн Ньютона</option>
<option value="2">Множество Мандельброта</option>
<option value="3">Множество Жюлиа</option>
</select>
<select name="color">
<option value="1">Классическая</option>
<option value="2">Уровни</option>
<option value="3">Зебра</option>
</select>
</form>

</div>
<div><button type="button" id='button' onclick=" resetScale(); run()">Нарисовать</button></div>


</body>

</html>