detecting broken outlines (intersecting lines - workaround )

I had problem when newOutline() did not return well formed outline.

(some outlines had intersecting lines, usually on low res with texels 1-2-3-4)

here is my workaround(tested it with more than few 100 outlines):

[lua]local function linesIntersect( F, S )

    local x1, y1 = F.x1, F.y1

    local x2, y2 = F.x2, F.y2

    local x3, y3 = S.x1, S.y1

    local x4, y4 = S.x2, S.y2

    local d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)

    if d == 0 then

        – parallel

        return false

    else

        local nx = (x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)

        local ny = (x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)

        local x5, y5 = nx/d, ny/d

        if ((x2-x5)*(x5-x1)) >= 0 and ((y2-y5)*(y5-y1)) >= 0 and

           ((x4-x5)*(x5-x3)) >= 0 and ((y4-y5)*(y5-y3)) >= 0 then

            – intersect on line

            return true

        else

            return false

        end

    end

end

local function isOutlineInvalid( outline )

    --print( “detectInvalidOutline” )

    local F, S = {}, {}

    local invalidOutline = false

    local maxI = #outline - 7

    local maxJ = #outline - 3

    local i = 1

    while i <= maxI do

        – first

        F.x1, F.y1 = outline[i+0], outline[i+1]

        F.x2, F.y2 = outline[i+2], outline[i+3]

        

        local j = i + 4

        while j <= maxJ do

            – rest

            S.x1, S.y1 = outline[j+0], outline[j+1]

            S.x2, S.y2 = outline[j+2], outline[j+3]

            if linesIntersect( F, S ) then

                --print( “lines intersect (”…F.x1…","…F.y1…") ("…F.x2…","…F.y2…") <-> ("…S.x1…","…S.y1…") ("…S.x2…","…S.y2…")" )

                return true

            end

            j = j + 2

        end

        i = i + 2

    end

    return invalidOutline

end

[/lua]

Usage:

[lua]if isOutlineInvalid( outlineTable ) then print( “OUTLINE invalid” ) end[/lua]