%%%% 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');

% 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);

% 2
% find area on xy where both intersect
% volume of z2 - volume of z1
% incomplete for now
x = linspace(0, 10, 1000);
intersect = (1/4).*sqrt(x.^2 + (x.').^2) == exp(-(1-x.*x.').^2);
volume = (...
		 ( cumtrapz( (1/4).*sqrt(x.^2 + (x.').^2) .* (x(2) - x(1)) )) - ...
		 ( cumtrapz( exp(-(1-x.*x.').^2 ) .* (x(2) - x(1)) ))...
		 ) .* intersect;

%% 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;

% 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);
