-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnowflake.m
More file actions
63 lines (48 loc) · 1.55 KB
/
Snowflake.m
File metadata and controls
63 lines (48 loc) · 1.55 KB
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
function v = Snowflake(n, resolution)
% Koch Snowflake inspired by
% https://codereview.stackexchange.com/questions/144700/plotting-the-koch-snowflake
% n: Iteration number
% resolution: number of grid points per dimension
L = 1;
p1 = [0, 0];
p2 = [1/2, L*sin(pi/3)] ;
p3 = [L, 0];
centroid = (p1 + p2 + p3)/3;
flake_points = [p1 - centroid; p2 - centroid; p3 - centroid];
x = linspace(-1, 1, resolution);
y = linspace(-1, 1, resolution);
v = Triangle(1, x, y, flake_points);
for j = 1:n
n_points = size(flake_points,1);
new_triangle_points = nan(n_points*3,2);
for i = 1:n_points
p = flake_points(i,:);
if i == n_points
vector = flake_points(1,:)-flake_points(i,:);
else
vector = flake_points(i+1,:)-flake_points(i,:);
end
b1 = p + 1/3*vector;
b2 = p + 1/2*vector + perp(vector)*L/3*sin(pi/3);
b3 = p + 2/3*vector;
new_triangle_points(3*i-2:3*i,:) = [b1; b2; b3];
v = v + Triangle(1, x, y, [b1; b2; b3]);
end
flake_points(end+1:end+9,:) = nan(9,2);
new_flake_points = nan(n_points*3+n_points,2);
n = 1;
for i = 1:n_points
new_flake_points(n,:) = flake_points(i,:);
index = 3*i-2;
new_flake_points(n+1:n+3,:) = new_triangle_points(index:index+2,:);
n = n + 4;
end
flake_points = new_flake_points;
L = 1/3*L;
end
function p_vec = perp(vec)
p_vec(1) = -vec(2);
p_vec(2) = vec(1);
p_vec = p_vec / norm(p_vec);
end
end