clc;

K = size(E,2);
fprintf('K = %d\n', K);

v_1 = E(1,:); v_2 = E(2,:); v_3 = E(3,:);
x = 0.5 * (-(r + s)' * P(1,v_1) + (r + 1)' * P(1,v_2) + (s + 1)' * P(1,v_3));
y = 0.5 * (-(r + s)' * P(2,v_1) + (r + 1)' * P(2,v_2) + (s + 1)' * P(2,v_3));

lengths1 = sqrt((x(1,:) - x(2,:)) .^ 2 + (y(1,:) - y(2,:)) .^ 2);
lengths2 = sqrt((x(2,:) - x(3,:)) .^ 2 + (y(2,:) - y(3,:)) .^ 2);
lengths3 = sqrt((x(3,:) - x(1,:)) .^ 2 + (y(3,:) - y(1,:)) .^ 2);

angles1 = acos((lengths2 .^ 2 + lengths3 .^ 2 - lengths1 .^ 2) ./ (2 * lengths2 .* lengths3));
angles2 = acos((lengths1 .^ 2 + lengths3 .^ 2 - lengths2 .^ 2) ./ (2 * lengths1 .* lengths3));
angles3 = acos((lengths1 .^ 2 + lengths2 .^ 2 - lengths3 .^ 2) ./ (2 * lengths1 .* lengths2));

gammas = 4 * (sin(angles1) .* sin(angles2) .* sin(angles3)) ./ (sin(angles1) + sin(angles2) + sin(angles3));
figure;
%hist(gammas,10);
[numbers,positions] = hist(gammas,10);
bar(positions,numbers / K,0.9);
if print_titles == 1
    title_string = sprintf('Histogram of quality parameter for %s (K = %d)',method_string,K);
    title(title_string);
end
if save_figures == 1
    filename_string = ['Quality_parameter_histogram_',algorithm_string,'_for_h=',mesh_size_parameter_string_comma];
    saveas(gcf,filename_string,'pdf');
    saveas(gcf,filename_string,'png');
end

fprintf('gamma_min = %f\n',min(gammas));
fprintf('gamma_max = %f\n',max(gammas));


lengths = [lengths1; lengths2; lengths3]; lengths = lengths(:);
figure;
%hist(lengths,20);
[numbers,positions] = hist(lengths,20);
bar(positions,numbers / (3 * K),0.9);
if print_titles == 1
    title_string = sprintf('Histogram of edge lengths for %s (K = %d)',method_string,K);
    title(title_string);
end