%%%% ECE-210-B HW5 - Advanced Manipulations - James Ryan
% James Ryan, 03/18/24

% preamble
close all; clc; clear;

%% On the Air
% 1 
R = [-9:9] + fliplr([-9:9]).'.*j;

% 2
n = [1:50];
t = [linspace(0,1,1e5)].';
sin_int = sum(trapz(sin(t.^n)) .* (t(2) - t(1)), 'all');
		% <-.02> erm... the integral calculation seems to be correct
		% (though `trapz` has an independent variable argument), but
		% why the `sum` at the end? should have said this was supposed
		% to be 50 different integral estimates in parallel. :p

% 3
% a
[theta, phi] = meshgrid(...
               linspace(0, 2*pi, 5), ...
			   linspace(0, pi, 5));
% b
x = sin(phi) .* cos(theta);
y = sin(phi) .* sin(theta);
z = cos(phi);

% c
figure;
surf(x,y,z);
pbaspect([1 1 1]);
colormap winter;

title("The best sphere");
xlabel("over axis")
ylabel("yonder axis")
zlabel("up axis")

%% Under the Sea
% 1
interest = sin(linspace(0,5,100).*linspace(-5,0,100).');
half_extract = ( abs(interest - .5) < (5/1000) ) .* interest; % < min quantized val
indices = find(half_extract);
		% <-.06> two things:
		%   1. the minimum quantized value is pretty uncertain after
		%   you pass the input through the sine function -- in fact,
		%   the nearest points are (i think) significantly closer than
		%   this. you'll have to use `min` to find them.
		%   2. i did say something about 2D indices, but that's less
		%   important -- linear indices often work just as well.

% 2
X = linspace(-5,5,100);
[x, y] = meshgrid(X, X);
intersect = (1/4).*sqrt(x.^2 + y.^2) < exp(-(1-x.*y).^2);
% integral(end) = volume
integral = cumtrapz(X, cumtrapz(X, exp(-(1-x.*y).^2).*intersect, 2));
		% <-.02> not, alas, the volume contained between the surfaces,
		% but just the volume under the first surface and above the
		% xy-plane. i think that's just a small oversight, though.


figure;
surf(x, y, exp(-(1-x.*y).^2).*intersect);
xlabel("X Axis");
ylabel("Y Axis");
zlabel("Z Axis");
title('Volume representing $\frac{1}{4}\sqrt{x^2 + y^2} < e^{-(1-x * y)^2}$',...
      'interpreter', 'latex');
pbaspect([4 4 3])
		% <+.02> volume is consistent with the above, so i'll give a
		% few points.


%% I Need a Vacation
% 1
A_vals = sqrt(...
         ([1:256] - 99).^2 + ...
		 ([1:256].' - 99).^2);
A = A_vals < 29;

% What's happening here is that our A indices are taken, shifted "down" 99
% values, and then a norm is taken on them. If its final norm is < 29, its true!
% otherwise, they're false.
% Theres a circle localized around an origin of (i,j) = (99,99), and extends
% out. This makes sense! relative to everywhere else, this is our lowest value,
% and it creates the smallest norm we see (zer0). 
% This holds experimentally! If we change 29 to 1, we just get a single teeny
% pixel at (99,99), surrounded by a false sea. So, the equality we have acts
% to give our circle some radius.  
figure;
imshow(A);

% 2
B_vals = sqrt(...
         ([1:256] - 62).^2 + ...
		 ([1:256].' - 62).^2);
B = B_vals < 58;

% Close to the previous idea, however we shift down only by 68 units, so our 
% origin is (68, 68).
% We have a larger radius, given we doubled our tolerance for whats true.

figure;
imshow(B);

% 3
C_vals = [1:256] - 4.*sin(([1:256].'./10));
C = C_vals > 200;
		% <-.02> traditionally, the i index is the first index in the
		% MATLAB sense -- that is, the index which increments down each
		% column, not across each row. in short, (x, y) ←→ (j, i),
		% whereas you effectively have y equivalent to j.

% We get a vertical ripple!! The ripple runs along our y axis and has a period
% of 10 and a peak-to-trough distance of 8. This acts as a wavefront from our
% true shoreline and our false sea.
figure;
imshow(C);

% 4
S_vals = rand(256, 256, 3);
S = permute(permute(S_vals,[3 1 2]) .* [0 0 1].', [2 3 1]);

% Interpreting this as a 256 x 256 image with 3 color channels, it looks
% like we applied a filter to kill R and G! We're left with the blue channel
figure;
imshow(S);

% 5
M = A & ~B;

% We get a crescent moon! The portion of B that cast over A was negated and
% compared, which resulted in that poriton being removed.
% We're left with the parts of A that was free from B's grasp
figure;
imshow(M);

% 6
Z = (C .* S) + M;

% Its a scene of a beach! Our moon is casting its light onto the dark beach,
% as the water casts itself onto the shore.
% reminds me of good times :D
figure;
imshow(Z);
		% ahhhh, that it does me. figured y'all could use something
		% calming.
