From a6696ffc61fa809929661ac4130cbe7aa8f0f76b Mon Sep 17 00:00:00 2001 From: Nikita Satin Date: Tue, 19 Jun 2018 14:08:25 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A1=D0=B0=D1=82=D0=B8=D0=BD=20=D0=9D.?= =?UTF-8?q?=D0=90.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DragonFractal.js | 86 ++++++++++++++++++++++++++++++++++++++++++++ index.html | 92 ++++-------------------------------------------- 2 files changed, 92 insertions(+), 86 deletions(-) create mode 100644 DragonFractal.js diff --git a/DragonFractal.js b/DragonFractal.js new file mode 100644 index 0000000..c97fa5b --- /dev/null +++ b/DragonFractal.js @@ -0,0 +1,86 @@ +class Pixels { + + constructor() { + this.pixelsCollection = []; + } + + setPixels(x, y) { + this.pixelsCollection.push({ x: x, y: y }); + } + + draw(image, size) { + if (this.pixelsCollection.length === 0) return; + const maxX = this.max((p) => p.x); + const maxY = this.max((p) => p.y); + const minX = this.min((p) => p.x); + const minY = this.min((p) => p.y); + const width = maxX - minX; + const height = maxY - minY; + const scaleX = (image.width - 20) / width; + const scaleY = (image.height - 20) / height; + const scale = Math.min(scaleX, scaleY); + for (let pixel of this.pixelsCollection) { + let x = (pixel.x - minX - width / 2) * scale + image.width / 2.0; + let y = (pixel.y - minY - height / 2) * scale + image.height / 2.0; + x = Math.ceil(x); + y = Math.ceil(y); + this.putColor(image, x, y, size.width); + } + return image; + } + + setPixel(x, y) { + this.pixelsCollection.push({ x: x, y: y }); + } + + putColor(image, x, y, width) { + image.data[4 * (x + width * y) + 0] = 125; + image.data[4 * (x + width * y) + 1] = 125; + image.data[4 * (x + width * y) + 2] = 125; + image.data[4 * (x + width * y) + 3] = 255; + return image; + } + + max(selector) { + let max = Number.MIN_SAFE_INTEGER; + for (let pixel of this.pixelsCollection) { + let element = selector(pixel); + if (element > max) { + max = element; + } + } + return max; + } + + min(selector) { + let min = Number.MAX_SAFE_INTEGER; + for (let pixel of this.pixelsCollection) { + if (selector(pixel) < min) { + min = selector(pixel); + } + } + return min; + } +} + +function drawDragonFractal(image, size) { + const iterCount = 100000; + let x = 1.0; + let x1 = 0.0; + let y = 0.0; + let y1 = 0.0; + const angle45 = Math.PI / 4; + const pixels = new Pixels(); + for (let i = 0; i < iterCount; i++) + { + const rnd = Math.random() > 0.5 ? 1 : 0; + x1 = (x * Math.cos(angle45 + Math.PI / 2 * rnd) + - y * Math.sin(angle45 + Math.PI / 2 * rnd)) / Math.sqrt(2) + 1 * rnd; + y1 = (x * Math.sin(angle45 + Math.PI / 2 * rnd) + + y * Math.cos(angle45 + Math.PI / 2 * rnd)) / Math.sqrt(2); + x = x1; + y = y1; + pixels.setPixel(x, y); + } + return pixels.draw(image, size); +} \ No newline at end of file diff --git a/index.html b/index.html index 39a9d04..ff60f34 100644 --- a/index.html +++ b/index.html @@ -1,13 +1,11 @@ - + - + - - - + + \ No newline at end of file From b1824d19df6dbb4be3f8fe378f5a32c043366642 Mon Sep 17 00:00:00 2001 From: Nikita Satin Date: Tue, 19 Jun 2018 14:13:22 +0500 Subject: [PATCH 2/2] fix crutches --- DragonFractal.js | 9 ++++----- index.html | 42 ++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/DragonFractal.js b/DragonFractal.js index c97fa5b..ee5698b 100644 --- a/DragonFractal.js +++ b/DragonFractal.js @@ -8,7 +8,7 @@ class Pixels { this.pixelsCollection.push({ x: x, y: y }); } - draw(image, size) { + drawDragonFractal(image, size) { if (this.pixelsCollection.length === 0) return; const maxX = this.max((p) => p.x); const maxY = this.max((p) => p.y); @@ -63,7 +63,7 @@ class Pixels { } } -function drawDragonFractal(image, size) { +function getDragonFractal(image, size) { const iterCount = 100000; let x = 1.0; let x1 = 0.0; @@ -71,8 +71,7 @@ function drawDragonFractal(image, size) { let y1 = 0.0; const angle45 = Math.PI / 4; const pixels = new Pixels(); - for (let i = 0; i < iterCount; i++) - { + for (let i = 0; i < iterCount; i++) { const rnd = Math.random() > 0.5 ? 1 : 0; x1 = (x * Math.cos(angle45 + Math.PI / 2 * rnd) - y * Math.sin(angle45 + Math.PI / 2 * rnd)) / Math.sqrt(2) + 1 * rnd; @@ -82,5 +81,5 @@ function drawDragonFractal(image, size) { y = y1; pixels.setPixel(x, y); } - return pixels.draw(image, size); + return pixels.drawDragonFractal(image, size); } \ No newline at end of file diff --git a/index.html b/index.html index ff60f34..77c72cb 100644 --- a/index.html +++ b/index.html @@ -1,30 +1,28 @@ + - + - + + + + - - + - \ No newline at end of file