-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflakes.js
149 lines (137 loc) · 4.11 KB
/
snowflakes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var defaultSnowflakeLength = 500;
var snowflakePartsCount = 8;
var defaultSegmentLength = 5;
var maxSegmentAngle = 360 / snowflakePartsCount;
var allowedAngles = [0, 45, 90, 135, 180, 225, 270, 315];
function randomFromInterval(start, end)
{
return start + Math.floor(Math.random()*(end - start + 1))
}
function degreeToRadian(degree)
{
return (degree * Math.PI) / 180;
}
function radianToDegree(radian)
{
return (radian * 180) / Math.PI;
}
function clearCanvas()
{
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "#000000";
context.fillRect(0,0,canvas.width,canvas.height);
}
function drawBackground()
{
clearCanvas();
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "#808080";
context.fillRect(border, border, canvas.width - 2*border, canvas.height - 2*border);
context.strokeStyle = "#000000";
for (x=0; x<canvas.width; x+=50)
{
context.beginPath();
context.moveTo(x, 0);
context.lineTo(x, canvas.height);
context.stroke();
}
for (y=0; y<canvas.height; y+=50)
{
context.beginPath();
context.moveTo(0, y);
context.lineTo(canvas. width, y);
context.stroke();
}
}
/*function generateSnowflake()
{
var snowflake = new Array();
for (var i=0; i<snowflakeLength; i++)
snowflake[i] = randomFromInterval(1, maxSegmentAngle);
return snowflake;
}
function drawSnowflake(startX, startY)
{
drawBackground();
var currentX = startX;
var currentY = startY;
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
context.strokeStyle = "#000000";
context.lineWidth = 1;
context.beginPath();
context.moveTo(currentX, currentY);
for (var part=0; part<snowflakePartsCount; part++)
{
for (segment=0; segment<snowflakeLength; segment++)
{
angle = snowflake[segment] + part * maxSegmentAngle;
currentX = currentX + segmentLength * Math.cos(degreeToRadian(angle));
currentY = currentY + segmentLength * Math.sin(degreeToRadian(angle));
context.lineTo(currentX, currentY);
}
}
context.stroke();
}
*/
function evaluateSnowflakeSize(snowflake)
//returns a radius of circumcircle
//идея в том, чтобы пробежаться по двум противоположным частям снежинки
//и найти максимальное расстояние между двумя соответствующими сегментами разных частей
{
var x = y = 0;
var maxVectorSize = 0;
for (segment=0; segment<snowflake.length; segment++)
{
x += Math.cos(degreeToRadian(snowflake[segment]));
y += Math.sin(degreeToRadian(snowflake[segment]));
vectorSize = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
if (vectorSize > maxVectorSize)
maxVectorSize = vectorSize;
}
return maxVectorSize;
}
function generateSnowflake(snowflakeLength)
{
var newSnowflake = new Array();
for (var i=0; i<snowflakeLength; i++)
newSnowflake[i] = allowedAngles[randomFromInterval(0, 7)];
return newSnowflake;
}
function drawSnowflakeCircumcircle(snowflake, x, y)
{
var radius = evaluateSnowflakeSize(snowflake);
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "#c0c0c0";
context.beginPath();
context.arc(x, y, radius, 0, 2*Math.PI, true);
context.fill();
}
function drawSnowflake(snowflake, startX, startY, segmentLength, snowflakeLength, drawCircumcircle)
{
drawBackground();
if (drawCircumcircle)
drawSnowflakeCircumcircle(snowflake, startX, startY);
var currentX = startX;
var currentY = startY;
var canvas = document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
context.strokeStyle = "#000000";
context.lineWidth = 1;
context.beginPath();
context.moveTo(currentX, currentY);
for (var part=0; part<snowflakePartsCount; part++)
{
for (segment=0; segment<snowflakeLength; segment++)
{
angle = snowflake[segment] + part * 45;
currentX = currentX + segmentLength * Math.cos(degreeToRadian(angle));
currentY = currentY + segmentLength * Math.sin(degreeToRadian(angle));
context.lineTo(currentX, currentY);
}
}
context.stroke();
}