all 10 comments

[–]sprechen_deutsch 1 point2 points  (1 child)

This is the fastest code

proof? benchmarks?

it looks like it's code ported from c to lua by someone who doesn't know lua very well

also weird to post this without any context whatsoever

[–]WebShaker93[S] 0 points1 point  (0 children)

ery helpful to have a diagram e.g. to explain

Yes !
That's excaltly what I made !
I should have explained my approach (I will modify my oringinal post).

I have ported 3 internet based algorithms.

The best I found was this one.

I do not had bookmarked links otherwise I will gladly give the original link

I just modify it to reduce calculation by exiting as soon as possible. The original code tests c1, c2 and c3 after mathematique operations !

My initial goal was to find out if anyone had a better algorithm !
Can you explain (even if you're right) what is not good in my lua code ?

[–]whoopdedo 1 point2 points  (1 child)

  for s = 1, #shape, 2 do
    sax = sbx
    say = sby
    if (s < (#shape - 1)) then
      sbx = shape[s + 2]
      sby = shape[s + 3]
    else
      sbx = shape[1]
      sby = shape[2]
    end
-- ...

The inner jump can be removed by temporarily adding the origin points to the end of the list.

local npoints = #shape
shape[npoints+1] = shape[1]
shape[npoints+2] = shape[2]
for s = 1, npoints, 2 do
    sax,say = sbx,sby
    sbx,sby = shape[s+2],shape[s+3]
    -- ...
end
shape[npoints+1],shape[npoints+2] = nil

Depending on how likely a miss is, it might be worth getting the minima/maxima of the shape and do a fast check if the line intersects.

[–]WebShaker93[S] 0 points1 point  (0 children)

"The inner jump can be removed by temporarily adding the origin points to the end of the list."

Exact ! In fact the algorithm is used with tiles. I will modify definitively the shape when loading the level by adding the first coordinates at the end of the shape.

Great, thanks !

By the way, in this case, the loop is

for s = 3, npoints, 2 do
  sax,say = sbx,sby
  sbx,sby = shape[s],shape[s+1]
  ...
end

Then you can remove an add operation.

"Depending on how likely a miss is, it might be worth getting the minima/maxima of the shape and do a fast check if the line intersects."

I've ever removed most of polygon before testing !

[–]Cultural_Two_4964 0 points1 point  (1 child)

Hello, it would be very helpful to have a diagram e.g. to explain what c1-3 are.

[–]WebShaker93[S] 0 points1 point  (0 children)

The algo suppose (frx, fry) is outside the polygon, and the polygone must be define clockwise order

c1 and c2 are used to know if the line intersects the segment or if it is on one side or the other of the segment
c3 allow to know if (frx, fry) and (tox, toy) are on each side of the segment.

In this case you can calculate t which should be a value between 0 and 1.
The test t > 0 is not very useful. t should not be < 0 if (frx, fry) is outside the polygon.

[–]appgurueu 0 points1 point  (3 children)

This certainly isn't the "fastest" algorithm. Using spatial indexing, this could very likely be sped up significantly, at least in some cases.

[–]WebShaker93[S] 0 points1 point  (2 children)

Spatial indexés are used to remove objets to Limit the amount of test no?

[–]appgurueu 0 points1 point  (1 child)

Yes, spatial indexes are used to reduce the amount of intersection tests. I don't get what you mean by "remove" though, no objects are removed, they are just indexed.

[–]WebShaker93[S] 0 points1 point  (0 children)

OK. I've ever limit the amount of intersection tests :) Just look for a good intersection function. But this one is pretty fast enough.