clear

function k = kinter(h,P)
    n = size(h.data,1);
    [_,k] = min(sum(abs(h.data-P(ones(n,1),:)),2));
endfunction

clf

curve1= csvRead('curve1.csv');
curve2= csvRead('curve2.csv');
circle= csvRead('ref_circle.csv');
points=csvRead('points.csv');

X1=curve1(1:$,1);Y1=curve1(1:$,2);
X2=curve2(1:$,1),Y2=curve2(1:$,2);
X=circle(1:$,1),Y=circle(1:$,2);

h=plot(X1,Y1, X2,Y2, X,Y);
isoview on

[xp yp]=(points(1),points(2));
[s1x s1y]=(points(3),points(4));
[s2x s2y]=(points(5),points(6));

A=[xp yp];B=[s1x s1y];C=[s2x s2y];

//original
k1A = kinter(h(1),A);
k1B = kinter(h(1),B);
k3B = kinter(h(3),B);
k3C = kinter(h(3),C);
k2C = kinter(h(2),C);
k2A = kinter(h(2),A);


data = [
h(1).data(k1A:k1B-1,:)
h(3).data(k3B:k3C-1,:)
h(2).data(k2C:k2A,:)
]

xfpoly(data(:,1),data(:,2),addcolor([1 1 0]))

xstring(A(1),A(2),"A")
xstring(B(1),B(2),"B")
xstring(C(1),C(2),"C")
