# detecting broken outlines (intersecting lines - workaround )

**URL:** <https://forums.solar2d.com/t/detecting-broken-outlines-intersecting-lines-workaround/337023>\
**Category:** Physics\
**Created:** [December 7, 2015, 6:07pm UTC](https://forums.solar2d.com/t/detecting-broken-outlines-intersecting-lines-workaround/337023 "2015-12-07T18:07:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajko](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rajko](https://forums.solar2d.com/u/Rajko)\
**Post date:** [December 7, 2015, 6:07pm UTC](https://forums.solar2d.com/t/detecting-broken-outlines-intersecting-lines-workaround/337023/1 "2015-12-07T18:07:40Z")

</div>

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 )

&nbsp; &nbsp; local x1, y1 = F.x1, F.y1

&nbsp; &nbsp; local x2, y2 = F.x2, F.y2

&nbsp; &nbsp; local x3, y3 = S.x1, S.y1

&nbsp; &nbsp; local x4, y4 = S.x2, S.y2

&nbsp; &nbsp; local d = (x1-x2)\*(y3-y4) - (y1-y2)\*(x3-x4)

&nbsp; &nbsp; if d == 0 then

&nbsp; &nbsp; &nbsp; &nbsp; – parallel

&nbsp; &nbsp; &nbsp; &nbsp; return false

&nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; local nx = (x1\*y2-y1\*x2)\*(x3-x4) - (x1-x2)\*(x3\*y4-y3\*x4)

&nbsp; &nbsp; &nbsp; &nbsp; local ny = (x1\*y2-y1\*x2)\*(y3-y4) - (y1-y2)\*(x3\*y4-y3\*x4)

&nbsp; &nbsp; &nbsp; &nbsp; local x5, y5 = nx/d, ny/d

&nbsp; &nbsp; &nbsp; &nbsp; if ((x2-x5)\*(x5-x1)) \>= 0 and ((y2-y5)\*(y5-y1)) \>= 0 and

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((x4-x5)\*(x5-x3)) \>= 0 and ((y4-y5)\*(y5-y3)) \>= 0 then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; – intersect on line

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true

&nbsp; &nbsp; &nbsp; &nbsp; else

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; end

end

local function isOutlineInvalid( outline )

&nbsp; &nbsp; --print( “detectInvalidOutline” )

&nbsp; &nbsp; local F, S = {}, {}

&nbsp; &nbsp; local invalidOutline = false

&nbsp; &nbsp; local maxI = #outline - 7

&nbsp; &nbsp; local maxJ = #outline - 3

&nbsp; &nbsp; local i = 1

&nbsp; &nbsp; while i \<= maxI do

&nbsp; &nbsp; &nbsp; &nbsp; – first

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

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

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; local j = i + 4

&nbsp; &nbsp; &nbsp; &nbsp; while j \<= maxJ do

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; – rest

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

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if linesIntersect( F, S ) then

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

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = j + 2

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; i = i + 2

&nbsp; &nbsp; end

&nbsp; &nbsp; return invalidOutline

end

[/lua]

Usage:

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