forked from trpo-moais-2016/fg04-ifs
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Сатин Н.А. #4
Open
SanGreen74
wants to merge
2
commits into
trpomoais2018:master
Choose a base branch
from
unknown repository
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Сатин Н.А. #4
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class Pixels { | ||
|
||
constructor() { | ||
this.pixelsCollection = []; | ||
} | ||
|
||
setPixels(x, y) { | ||
this.pixelsCollection.push({ x: x, y: y }); | ||
} | ||
|
||
drawDragonFractal(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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Более правильно - addPixel |
||
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 getDragonFractal(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.drawDragonFractal(image, size); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,28 @@ | ||
<!doctype html> | ||
<html> | ||
|
||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<meta http-equiv="Cache-Control" content="no-cache"/> | ||
|
||
<script> | ||
//пример рисования произвольного множества точек | ||
function example1() { | ||
//получаем html-элемент типа canvas и его характеристики | ||
var canvas = document.getElementById("canvas1"); | ||
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 example2() { | ||
//получаем html-элемент типа canvas и его характеристики | ||
var canvas = document.getElementById("canvas2"); | ||
var canvasHeight = parseInt(canvas.getAttribute("height")); | ||
var canvasWidth = parseInt(canvas.getAttribute("width")); | ||
|
||
//создаем 2d context | ||
var context = canvas.getContext('2d'); | ||
|
||
//задаем стиль линий | ||
context.lineWidth = "5"; | ||
context.strokeStyle = "green"; | ||
|
||
//начинаем создание нового пути | ||
context.beginPath(); | ||
//задаем начальную точку линии | ||
context.moveTo(0, 75); | ||
//задаем конечную точку линии и добавляем линию в путь | ||
context.lineTo(150, 100); | ||
//выводим путь на экран | ||
context.stroke(); | ||
} | ||
|
||
//пример рисования полилиний (сначала вычисляем точки, затем рисуем) | ||
function example3() { | ||
//получаем html-элемент типа canvas и его характеристики | ||
var canvas = document.getElementById("canvas3"); | ||
var canvasHeight = parseInt(canvas.getAttribute("height")); | ||
var canvasWidth = parseInt(canvas.getAttribute("width")); | ||
|
||
//создаем 2d context | ||
var context = canvas.getContext('2d'); | ||
|
||
//задаем стиль линий | ||
context.lineWidth = "2"; | ||
context.strokeStyle = "#FF0000"; | ||
|
||
//создаем массив точек | ||
//в качестве точек используются анонимные объекты со свойствами x и y | ||
var points = [{x: 20, y: 50}, {x: 120, y: 50}, {x: 120, y: 100}, {x: 50, y: 20}]; | ||
|
||
//начинаем создание нового пути | ||
context.beginPath(); | ||
//добавляем в путь все точки из массива | ||
for (var i = 0; i < points.length; i++) { | ||
var point = points[i]; | ||
if (i == 0) | ||
context.moveTo(point.x, point.y); | ||
else | ||
context.lineTo(point.x, point.y); | ||
} | ||
//выводим путь на экран | ||
context.stroke(); | ||
} | ||
|
||
//вызывается после загрузки body | ||
function run () { | ||
example1(); | ||
example2(); | ||
example3(); | ||
} | ||
</script> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<meta http-equiv="Cache-Control" content="no-cache" /> | ||
<script type="text/javascript" src="DragonFractal.js"></script> | ||
<script> | ||
function example1() { | ||
var canvas = document.getElementById("canvas1"); | ||
var canvasHeight = parseInt(canvas.getAttribute("height")); | ||
var canvasWidth = parseInt(canvas.getAttribute("width")); | ||
var context = canvas.getContext('2d'); | ||
var imageData = context.createImageData(canvasWidth, canvasHeight); | ||
var size = { width: canvasWidth, height: canvasHeight }; | ||
context.putImageData(getDragonFractal(imageData, size), 0, 0); | ||
} | ||
function run() { | ||
example1(); | ||
} | ||
</script> | ||
|
||
<body onload="run()"> | ||
<canvas height='400' width='400' id='canvas1'></canvas> | ||
|
||
</body> | ||
|
||
<body onload="run()"> | ||
<canvas height='200' width='200' id='canvas1'></canvas> | ||
<canvas height='200' width='200' id='canvas2'></canvas> | ||
<canvas height='200' width='200' id='canvas3'></canvas> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Цикле for of можно const pixel.