function [new,theType] = combineldb (rin1,rin2,OPT,varargin)

new     = [];
theType = [];

n1 = find(isnan(rin1.x));
n2 = find(rin1.x == 999.999);
nr1 = unique([n1 n2]);
if ~isempty(nr1)
    n2 = 0;
    for n1 = 1 : length(nr1)
        ind1(n1,1) = n2 + 1;
        ind1(n1,2) = nr1(n1) - 1;
        n2 = nr1(n1);
    end
    if n2 < length(rin1.x)
        ind1(length(nr1)+1,1) = n2 + 1;
        ind1(length(nr1)+1,2) = length(rin1.x);
    end
else
    ind1(1,1) = 1;
    ind1(1,2) = length(rin1.x);
end

n1 = find(isnan(rin2.x));
n2 = find(rin2.x == 999.999);
nr2 = unique([n1 n2]);
if ~isempty(nr2)
    n2 = 0;
    for n1 = 1 : length(nr2)
        ind2(n1,1) = n2 + 1;
        ind2(n1,2) = nr2(n1) - 1;
        n2 = nr2(n1);
    end
    if n2 < length(rin2.x)
        ind2(length(nr2)+1,1) = n2 + 1;
        ind2(length(nr2)+1,2) = length(rin2.x);
    end
else
    ind2(1,1) = 1;
    ind2(1,2) = length(rin2.x);
end


for n1 = 1 : length(ind1)
    %remove identical points
    [t1a.x,t1a.y] = removeidentical(rin1.x(ind1(n1,1):ind1(n1,2)),rin1.y(ind1(n1,1):ind1(n1,2)));
    % t1a.x=r1.x;
    % t1a.y=r1.y;
    mv1 = 0;
    mv2 = 0;

    mv1 = OPT.shiftldb1;
    mv2 = OPT.shiftldb2;

    if mv1 > 0
        r1a.x = t1a.x(mv1+1:end);
        r1a.x = [r1a.x;t1a.x(1:mv1)];
        r1a.y = t1a.y(mv1+1:end);
        r1a.y = [r1a.y;t1a.y(1:mv1)];
    else
        r1a = t1a;
    end

    for n2 = 1 : length(ind2)
        [t2a.x,t2a.y] = removeidentical(r2.x,r2.y);
        % t2a.x=r2.x;
        % t2a.y=r2.y;
        if mv2 > 0
            r2a.x = t2a.x(mv2+1:end);
            r2a.x = [r2a.x;t2a.x(1:mv2)];
            r2a.y = t2a.y(mv2+1:end);
            r2a.y = [r2a.y;t2a.y(1:mv2)];
        else
            r2a = t2a;
        end

        Clockwise1 = polyIsClockwise(r1a.x,r1a.y); %!!!clockwise is tegen de klok in
        if Clockwise1
            r1a.x = flipud(r1a.x);
            r1a.y = flipud(r1a.y);
        end

        Clockwise2 = polyIsClockwise(r2a.x,r2a.y);
        if Clockwise2
            r2a.x = flipud(r2a.x);
            r2a.y = flipud(r2a.y);
        end

        mm = ones(length(r1a.x),length(r2a.x),1)* NaN;

        minimum = -1;
        for n = 1 : length(r1a.x)
            for m = 1 : length(r2a.x)
                mm(n,m) = sqrt((r1a.x(n)-r2a.x(m))^2 +(r1a.y(n)-r2a.y(m))^2);
                if minimum < 0
                    minimum = mm(n,m);
                else
                    minimum = min(minimum,mm(n,m));
                end
            end
        end
        [in,im] = find (mm <= OPT.distance);
        if isempty(in) && isempty(im)
            disp('Not any point coincides for the two ldb polygons')
            return
        end

        [delen1,ip11,ip12] = getParts(length(r1a.x),in);
        [delen2,ip21,ip22] = getParts(length(r2a.x),im);
        theType = [];

        if delen1 == 2 && delen2 == 1 %situatie A
            theType = 'A';
            new.x = r1a.x(1:ip11-1);
            new.y = r1a.y(1:ip11-1);

            if ip22 < length(r2a.x)
                new.x = [new.x;r2a.x(ip21-1:ip22+1)];
                new.y = [new.y;r2a.y(ip21-1:ip22+1)];
            else
                new.x = [new.x;r2a.x(ip21-1:ip22);r2a.x(1)];
                new.y = [new.y;r2a.y(ip21-1:ip22);r2a.y(1)];
            end

            new.x = [new.x;r1a.x(ip12+1:length(r1a.x));new.x(1)];
            new.y = [new.y;r1a.y(ip12+1:length(r1a.x));new.y(1)];
        elseif delen1 == 2 && delen2 == 2 % situatie B
            theType = 'B';
            new.x = r1a.x(1:ip11-1);
            new.y = r1a.y(1:ip11-1);

            new.x = [new.x;r2a.x(ip22:length(r2a.x));r2a.x(1:ip21)];
            new.y = [new.y;r2a.y(ip22:length(r2a.y));r2a.y(1:ip21)];

            new.x = [new.x;r1a.x(ip12+1:length(r1a.x));r1a.x(1)];
            new.y = [new.y;r1a.y(ip12+1:length(r1a.y));r1a.y(1)];
        elseif delen1 == 1 && delen2 == 1 % situatie C
            theType = 'C';
            new.x = r1a.x(ip11:ip12);
            new.y = r1a.y(ip11:ip12);

            if ip22 < length(r2a.x)
                new.x = [new.x;r2a.x(ip21-1:ip22+1)];
                new.y = [new.y;r2a.y(ip21-1:ip22+1)];
            else
                new.x = [new.x;r2a.x(ip21-1:ip22);r2a.x(1)];
                new.y = [new.y;r2a.y(ip21-1:ip22);r2a.y(1)];
            end

            new.x = [new.x;new.x(1)];
            new.y = [new.y;new.y(1)];
        elseif delen1 == 1 && delen2 == 2 % situatie D
            theType = 'D';
            new.x = r1a.x(ip11:ip12);
            new.y = r1a.y(ip11:ip12);

            new.x = [new.x;r2a.x(ip22:length(r2a.x));r2a.x(1:ip21)];
            new.y = [new.y;r2a.y(ip22:length(r2a.y));r2a.y(1:ip21)];

            new.x = [new.x;new.x(1)];
            new.y = [new.y;new.y(1)];
        end
    end
end

end