forked from mathworks/awesome-matlab-students
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Moonrun.m
129 lines (101 loc) · 2.81 KB
/
Moonrun.m
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
animateFrames();
function animateFrames()
animFilename = 'Moonrun.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame);
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, 'LoopCount', Inf, 'DelayTime', delayTime);
else
imwrite(A,map,animFilename, 'WriteMode', 'append', 'DelayTime', delayTime);
end
end
end
function drawframe(f)
if f == 1
%% Make landscape
clf
rng(4,'v4');
N=200;
q=linspace(-1,1,N);
k=abs(ifft2(exp(6i*randn(N))./(q.^2+q'.^2+1e-9)));
k=rescale(1./(k+1));
imagesc(k);
[x,y]=meshgrid(linspace(-1,1,N*7)*7);
k=repmat(k,[7,7]);
surf(x,y,k/2,'edgeC','none','SpecularS',0);
axis equal
colormap(copper)
hold on;
% Make moon
bscl = 1e5;
rng default
nl=1e4;
rd = rand(nl,1)/3+1;
po = randn(3,nl);
po = po./vecnorm(po);
[p,k,s]=SM(po',rd,7);
ms = mean(s);
ns = s - ms;
s = erf(ns*500)/50+ms;
[~,~,sb]=SM(po',randn(nl,1),1);
% Now, take a page from Adam D.' book for the craters
rng(1,'twister');
x = randn(3,16); % Let's add 16 craters
x = x./vecnorm(x);
mfc = @(x,y,z)y.*(erf((x-z)*30)/2+.5); % Using erf as the crater function...
for n = 1:size(x, 2)
d = vecnorm(x(:,n)-p');
s = mfc(d',s-1.14,rand(1)/2)+1.14;
end
s = s + sb/20;
s = (s-mean(s))/10+mean(s);
p2 = (p.'./vecnorm(p.'))'.*s;
E='EdgeC';
F='FaceC';
O='none';
G='FaceA';
% Plot moon
hold on;
bscl=1e1;
T2=trisurf(k,bscl*p2(:,1)+1*bscl,bscl*p2(:,2)+4*bscl,bscl*p2(:,3)+1*bscl,'FaceC','flat','FaceVertexCData',rescale(s,.4,1).*[1,1,1],E,O); % Plot
material([0,1,0,10]);
axis equal off
set(gcf,'color','k');
light('position',[1,-1,1]);
light('position',[1,-1,1]);
camproj p
campos([0,-7.2,.35]);
camva(40);
end
x = linspace(0, 1, 49)*2;
dx=mean(diff(x));
x = 0:dx:200*dx;
y = cos(pi*x)/15;
a=linspace(0,2*pi,49)-2;
% for n = 1:1:48
n=f;
cpos = [0.05, -7.2+2, .42] + [y(n), x(n), y(n)/2];
camup([0, sin(a(n))/2, cos(a(n))/2+1]);
campos(cpos);
camtarget([0.05, -7.2+2, .42] + [y(n+1), x(n+1), y(n+1)/2]);
1;
end
function [p,k,s]=SM(in,rd,r)
n=size(in,1);
k=convhull(in); % Points on "in" must lie on unit circle
c=@(x)sparse(k(:,x)*[1,1,1],k,1,n,n); % Connectivity
t=c(1)|c(2)|c(3); % Connectivity
f=spdiags(-sum(t,2)+1,0,t*1.)*r; % Weighting
s=((speye(n)+f'*f)\rd); % Solve for s w/regularizer
p=in.*s; % Apply
end