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

Done #8

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
53 changes: 53 additions & 0 deletions snowFlake/firstFractal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<html>
<head>
<title>SnowFlake</title>
<meta charset='utf-8' />
<script src="fractal.js"></script>
</head>
<body>
<canvas height='650' width='750' id='example'></canvas>
<script>
var example = document.getElementById("example"),
ctx = example.getContext('2d');
ctx.fillStyle="#ffffff";
ctx.fillRect(0, 0, example.width, example.height);

var iteration = 0;
iteration = iterStep();

console.log("iteration step " + iteration);

var pOne = new Point(110,170);
var pTwo = new Point(600,170);
var pThree = findThirdPoint(pOne, pTwo);
var points = [];
var mainPoints = [];
var allPoints = [];
var drawAllPoints = [];
mainPoints.push(pOne);
mainPoints.push(pTwo);
mainPoints.push(pThree);
mainPoints.push(pOne);
ctx.strokeStyle = '#000000';

ctx.beginPath();
if(iteration == 1){
ctx.moveTo(pOne.x, pOne.y);
ctx.lineTo(pTwo.x, pTwo.y);
ctx.lineTo(pThree.x, pThree.y);
ctx.lineTo(pOne.x, pOne.y);
}
else{
for(var i = 0; i < iteration; i++){
iterations(mainPoints, points, allPoints);
points = [];
mainPoints = equalizer(mainPoints, allPoints);
drawAllPoints = allPoints;
allPoints = [];
}
}
draw(drawAllPoints);
ctx.stroke();
</script>
</body>
</html>
80 changes: 80 additions & 0 deletions snowFlake/fractal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function Point(x,y){
this.x = x;
this.y = y;
}
function getD(p1,p2){
return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
function findAngle(p1,p2){
var angle;
if ((p2.x >= p1.x) && (p2.y >= p1.y) || (p2.x >= p1.x) && (p2.y <= p1.y)){
angle = Math.atan((p2.y-p1.y)/(p2.x-p1.x));
}
else if ((p2.x <= p1.x) && (p2.y >= p1.y)){
angle = Math.PI/2 + Math.atan((p1.x-p2.x)/(p2.y-p1.y));
}
else if ((p2.x <= p1.x) && (p2.y <= p1.y)){
angle = Math.PI + Math.atan((p1.y-p2.y)/(p1.x-p2.x));
}
return angle;
}
function findThirdPoint(p1,p2){
var midSidePoint = new Point((p2.x+p1.x)/2, (p2.y+p1.y)/2);
var inHight = getD(p1,p2);
var angle = findAngle(p1,p2);
return new Point(p1.x + (inHight)*Math.cos(angle + Math.PI/3), p1.y + (inHight)*Math.sin(angle + Math.PI/3));
}
function makeFrac(p1,p2, array){
var lengthSize = getD(p1,p2);
var newLengthSize = lengthSize/3;
var angle = findAngle(p1,p2);
var p3 = new Point(p1.x + (newLengthSize)*Math.cos(angle), p1.y + (newLengthSize)*Math.sin(angle));
var p4 = new Point(p3.x + (newLengthSize)*Math.cos(angle - Math.PI/3), p3.y + (newLengthSize)*Math.sin(angle - Math.PI/3));
var p5 = new Point(p1.x + (2*newLengthSize)*Math.cos(angle), p1.y + (2*newLengthSize)*Math.sin(angle) )
array = [];
array.push(p1);
array.push(p3);
array.push(p4);
array.push(p5);
array.push(p2);
return array;
}
function plusArrays(arrayOne, arrayTwo){
for(var i = 0; i < arrayOne.length-1; i++){
if(arrayOne[i] != arrayOne[i-1]){
arrayTwo.push(arrayOne[i]);
}
}
return arrayTwo;
}
function iterations(mainPoints, points, allPoints){
ctx.moveTo(mainPoints[0].x, mainPoints[0].y);
for(var i = 0; i < mainPoints.length;i++){
if(i == mainPoints.length - 1){
points = makeFrac(mainPoints[i], mainPoints[0], points);
}
else{
points = makeFrac(mainPoints[i], mainPoints[i+1], points);
}
allPoints = plusArrays(points, allPoints);
}
}
function draw(points){
for(var j = 0; j < points.length; j++){
ctx.lineTo(points[j].x, points[j].y);
}
}
function equalizer(arrayOne, arrayTwo){
for(var i = 0; i < arrayTwo.length; i++){
arrayOne[i] = arrayTwo[i];
}
return arrayOne;
}
function iterStep(iteration){
iteration = prompt("Введите шаг итерации ", 0);
if(iteration < 1 || iteration > 8 || isNaN(iteration) == true){
alert("Please, input number from 1 to 8");
iteration = iterStep(iteration);
}
return iteration;
}