I am implementing the Bentley-Ottman algorithm in Lua to find intersecting points in a polygon using the pseudocode located here .
I am relatively new to the implementation of algorithms, so I could not understand all its parts. Here is my code:
local function getPolygonIntersectingVertices( poly ) -- initializing and sorting X local X = {} for i = 1, table.getn( poly ) do if i == 1 then table.insert( X, { x = poly[i].x, y = poly[i].y, endpoint = 'left' } ) elseif i == table.getn( poly ) then table.insert( X, { x = poly[i].x, y = poly[i].y, endpoint = 'right' } ) else table.insert( X, { x = poly[i].x, y = poly[i].y, endpoint = 'right' }) table.insert( X, { x = poly[i].x, y = poly[i].y, endpoint = 'left' }) end end local sortxy = function( a, b ) if ax < bx then return true elseif ax > bx then return false elseif ay <= by then return true else return false end end table.sort( X, sortxy ) -- Main loop local SL = {} local L = {} local E local i = 1 while next(X) ~= nil do E = { x = X[i].x, y = X[i].y, endpoint = X[i].endpoint } if E.endpoint == 'left' then -- left endpoint code here elseif E.endpoint == 'right' then -- right endpoint code here else end table.remove( X, i ) end return L end
My polygon is a table using this structure: {{x = 1, y = 3}, {x = 5, y = 6}, ...}
How to define "a segment above segE in SL; " and "a segment below segE in SL; " and what if the scan line ( SL ) is empty? In addition, when I insert into X, should I mark it with endpoint = 'intersect' and attach it to the end, so when the loop falls into this part, it goes to the โelseโ statement of the main loop or Do I have the wrong algorithm?
It would be great if someone could show me a link with a simple implementation in Python, Ruby, etc., since it is difficult for me to follow the pseudocode and match it with a C ++ example.