Finish 2012-11-07 16:00:00 UTC

complexiting

by Fel

Status: Passed
Results: 374269 (cyc: 25, node: 741)
CPU Time: 0.003
Score: 375795.0
Submitted at: 2012-11-01 17:30:25 UTC
Scored at: 2012-11-01 21:23:46 UTC

Current Rank: 1796th (Highest: 107th )
Based on: timing (diff)

Comments
Please login or create a profile.
Code
function xyOut = solver(a, xyIn, wts)

% Sample solver
xyOut   = xyIn;

return

% Matriz geométrica
N = numKnots(xyIn,a);



end

function N = numKnots(XY,A)

% This function finds the number of knots in a network of points XY whose
% connectivity is given by A.

% Consider only unique lines (A is symmetric with ones on diagonal)
A = triu(A,1);
nLines = nnz(A);

% Find xy coordinates for first and second point on each line
[p1i,p2i] = find(A);
p1 = XY(p1i,:);
p2 = XY(p2i,:);

% Check each pair of lines for an intersection
N = 0;
for i = 1:nLines-1
    line1 = [p1(i,:) p2(i,:)];     %[x1 y1 x2 y2]

    for j = i:nLines
        line2 = [p1(j,:) p2(j,:)]; %[x1 y1 x2 y2]
        
        if areIntersecting(line1,line2)
            N = N + 1;
        end
        
    end
end

function bool = areIntersecting(l1, l2)
    
x1 = l1(1);
y1 = l1(2);
x2 = l1(3);
y2 = l1(4);
x3 = l2(1);
y3 = l2(2);
x4 = l2(3);
y4 = l2(4);

% Test to see if the lines share exactly one endpoint. If so, the lines
% intersect if either of the free points lies on the line segment formed by
% the other two points.
if isequal([x1, y1], [x3, y3]) && ~isequal([x2, y2], [x4, y4])
    bool = isPointOnSegment(x2, y2, x1, y1, x4, y4) || isPointOnSegment(x4, y4, x1, y1, x2, y2);
elseif isequal([x1, y1], [x4, y4]) && ~isequal([x2, y2], [x3, y3])
    bool = isPointOnSegment(x2, y2, x1, y1, x3, y3) || isPointOnSegment(x3, y3, x1, y1, x2, y2);
elseif isequal([x2, y2], [x3, y3]) && ~isequal([x1, y1], [x4, y4])
    bool = isPointOnSegment(x1, y1, x2, y2, x4, y4) || isPointOnSegment(x4, y4, x1, y1, x2, y2);
elseif isequal([x2, y2], [x4, y4]) && ~isequal([x1, y1], [x3, y3])
    bool = isPointOnSegment(x1, y1, x2, y2, x3, y3) || isPointOnSegment(x3, y3, x1, y1, x2, y2);

% Next we check for parallel and coincident lines. Parallel lines
% obviously don't intersect. Coincident lines intersect if an endpoint from
% one of the lines lies on the other segment.
elseif haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4)
    if haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4)  % lines are coincident
        bool = (isPointBetween(x3, x4, x1, 1) && isPointBetween(y3, y4, y1, 1)) || ...
               (isPointBetween(x3, x4, x2, 1) && isPointBetween(y3, y4, y2, 1)) || ...
               (isPointBetween(x1, x2, x3, 1) && isPointBetween(y1, y2, y3, 1)) || ...
               (isPointBetween(x1, x2, x4, 1) && isPointBetween(y1, y2, y4, 1));
    else  % lines are parallel
        bool = false;
    end

% If we get this far, we're in the general case of two well-defined
% non-parallel lines. The lines formed by the two segments must intersect
% somewhere. Find this intersection point and determine if it lies on the
% segments.
else   % general case
    % To avoid precision issues, represent point of intersection as a ratio
    % of two integers: (Px, Py) = (Px_n/Px_d, Py_n/Py_d)
    Px_n = (x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4);
    Px_d = (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4);
    
    Py_n = (x1*y2 - y1*x2)*(y3 - y4) - (y1 - y2)*(x3*y4 - y3*x4);
    Py_d = (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4);
    
    bool = isPointBetween(x1, x2, Px_n, Px_d) && ...
           isPointBetween(y1, y2, Py_n, Py_d) && ...
           isPointBetween(x3, x4, Px_n, Px_d) && ...
           isPointBetween(y3, y4, Py_n, Py_d);
end
end

function bool = haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4)
bool = (x4 - x3)*(y1*(x2 - x1) - x1*(y2 - y1)) == ...
       (x2 - x1)*(y3*(x4 - x3) - x3*(y4 - y3));
end

function bool = haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4)
bool = (y2 - y1)*(x4 - x3) == (x2 - x1)*(y4 - y3);
end

function bool = isPointOnSegment(x1, y1, x2, y2, x3, y3)
% Determine if point (x1, y1) is on the segment formed by (x2, y2) and (x3, y3).
bool = haveSameSlope(x2, y2, x1, y1, x1, y1, x3, y3) && ...
       isPointBetween(x2, x3, x1, 1) && ...
       isPointBetween(y2, y3, y1, 1);
end

function bool = isPointBetween(pt1, pt2, num, den)
% Determine if either of the following is satisfied:
% pt1 <= num/den <= pt2
% pt1 >= num/den >= pt2

bool = ((pt1*den <= num) && (num <= pt2*den)) || ...
       ((pt1*den >= num) && (num >= pt2*den));
end
end