clc; clear; close all;

triangle = [-1, 1, -1; -1, -1, 1];
x_tri = [triangle(1,1),triangle(1,2),triangle(1,3),triangle(1,1)];
y_tri = [triangle(2,1),triangle(2,2),triangle(2,3),triangle(2,1)];

% ekvidistantni sit na referencnim trojuhelniku
N = 6;
[x_eq,y_eq] = EquidistantNodes2D(N);
[r,s] = xytors(x_eq,y_eq);
x = 0.5 * (-(r + s) * triangle(1,1) + (r + 1) * triangle(1,2) + (s + 1) * triangle(1,3));
y = 0.5 * (-(r + s) * triangle(2,1) + (r + 1) * triangle(2,2) + (s + 1) * triangle(2,3));

figure;
plot(x_tri, y_tri, 'LineWidth', 2, 'Color', 'black'); hold on;
plot(x, y, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'b', 'MarkerSize', 10); hold on;
axis equal; axis off;


% optimalni uzly na referencnim trojuhelniku
N = 6;
[x_opt,y_opt] = OptimalNodes2D(N);
[r,s] = xytors(x_opt,y_opt);
x = 0.5 * (-(r + s) * triangle(1,1) + (r + 1) * triangle(1,2) + (s + 1) * triangle(1,3));
y = 0.5 * (-(r + s) * triangle(2,1) + (r + 1) * triangle(2,2) + (s + 1) * triangle(2,3));

figure;
plot(x_tri, y_tri, 'LineWidth', 2, 'Color', 'black'); hold on;
plot(x, y, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'b', 'MarkerSize', 10); hold on;
axis equal; axis off;



% obecne 2 trojuhelniky - mozna 2. trojuhelnik mirne posunout?
triangle_1 = [0, 5, 3; 0, 0, 3];
x_tri_1 = [triangle_1(1,1),triangle_1(1,2),triangle_1(1,3),triangle_1(1,1)];
y_tri_1 = [triangle_1(2,1),triangle_1(2,2),triangle_1(2,3),triangle_1(2,1)];
triangle_2 = [5, 0, 2; 0, 0, -2];
x_tri_2 = [triangle_2(1,1),triangle_2(1,2),triangle_2(1,3),triangle_2(1,1)];
y_tri_2 = [triangle_2(2,1),triangle_2(2,2),triangle_2(2,3),triangle_2(2,1)];

N = 6;
[x,y] = OptimalNodes2D(N);
[r,s] = xytors(x,y);
x_1 = 0.5 * (-(r + s) * triangle_1(1,1) + (r + 1) * triangle_1(1,2) + (s + 1) * triangle_1(1,3));
y_1 = 0.5 * (-(r + s) * triangle_1(2,1) + (r + 1) * triangle_1(2,2) + (s + 1) * triangle_1(2,3));
x_2 = 0.5 * (-(r + s) * triangle_2(1,1) + (r + 1) * triangle_2(1,2) + (s + 1) * triangle_2(1,3));
y_2 = 0.5 * (-(r + s) * triangle_2(2,1) + (r + 1) * triangle_2(2,2) + (s + 1) * triangle_2(2,3));

figure;
y_shift = 0.2;
plot(x_tri_1, y_tri_1, 'LineWidth', 2, 'Color', 'black'); hold on;
plot(x_tri_2, y_tri_2 - y_shift, 'LineWidth', 2, 'Color', 'black'); hold on;
plot(x_1, y_1, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'blue', 'MarkerSize', 10); hold on;
plot(x_2, y_2 - y_shift, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'green', 'MarkerSize', 10); hold on;
axis equal; axis off;