Don't blame the methods...
Most of the time is spend on randomizing the numbers...
You are of course correctMost of the time is spend on randomizing the numbers...
SuperStrict Graphics 800,600 Global _x1#,_x2#,_x3#,_x4# Global _y1#,_y2#,_y3#,_y4# Global Seed:Int Global time1%,count1% Global time2%,count2% Global time3%,Count3% Global Array:Int[,] = New Int[500000,8] While Not KeyDown (Key_Space) Count1=0 Count2=0 Count3=0 Seed = Rnd (0,MilliSecs()) SeedRnd (Seed) For Local i%=1 To 500000 array [i-1,0]=Rnd(0,800) array [i-1,1]=Rnd(0,600) array [i-1,2]=Rnd(0,800) array [i-1,3]=Rnd(0,600) array [i-1,4]=Rnd(0,800) array [i-1,5]=Rnd(0,600) array [i-1,6]=Rnd(0,800) array [i-1,7]=Rnd(0,600) Next Delay(20) time1=MilliSecs() For Local i%=1 To 500000 If LinesCollide(array [i-1,0],array [i-1,1],array [i-1,2],array [i-1,3],array [i-1,4],array [i-1,5],array [i-1,6],array [i-1,7]) Then count1:+1 Next time1=MilliSecs()-time1 Delay(20) time2=MilliSecs() For Local i%=1 To 500000 If LinesCollide2(array [i-1,0],array [i-1,1],array [i-1,2],array [i-1,3],array [i-1,4],array [i-1,5],array [i-1,6],array [i-1,7]) Then count2:+1 Next time2=MilliSecs()-time2 Delay(20) time3=MilliSecs() For Local i%=1 To 500000 If LinesCollide3(array [i-1,0],array [i-1,1],array [i-1,2],array [i-1,3],array [i-1,4],array [i-1,5],array [i-1,6],array [i-1,7]) Then count3:+1 Next time3=MilliSecs()-time3 Print "Method 1: "+count1+" colisions found in "+time1+" ms" Print "Method 2: "+count2+" colisions found in "+time2+" ms" Print "Method 3: "+count3+" colisions found in "+time3+" ms" Wend End Function LinesCollide%(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#) Local dpx#= x3 - x1 + x4 - x2 Local dpy#= y3 - y1 + y4 - y2 Local qax#= x2 - x1 Local qay#= y2 - y1 Local qbx#= x4 - x3 Local qby#= y4 - y3 Local d#= qay*qbx - qby*qax Local la# = qbx*dpy - qby*dpx Local lb# = qax*dpy - qay*dpx If Abs(la)<=Abs(d) And Abs(lb) <=Abs(d) Then Return True Return False End Function Function LinesCollide3%(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#) If Max (x2,x1)<Min (X3,x4) Or Max (x3,x4)<Min (x2,x1) Or Max (y2,y1)<Min (y3,y4) Or Max (y3,y4)<Min (y2,y1) Then Return False Local Ay# = y2 - y1 Local By# = y3 - y4 Local Ax# = x2 - x1 Local Bx# = x3 - x4 Local Cx# = x1 - x3 Local Cy# = y1 - y3 Local d# = (By# * Cx#) - (Bx# * Cy#) Local f# = (ay# * Bx#) - (Ax# * By#) Local e# = (Ax#* Cy#) - (ay# * Cx#) If (f>0.0 And (d<0.0 Or d>f Or e<0.0 Or e>f)) Or (F<=0.0 And (d>0.0 Or d<f Or e>0.0 Or e<f)) Then Return False Return True End Function Function LinesCollide2%(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#) Local LowerX#,UpperX#,LowerY#,UpperY# Local Ax#,Ay#,Bx#,By#,Cx#,Cy#,d#,e#,f# Ax# = x2 - x1 Bx# = x3 - x4 If Ax<0.0 LowerX = x2 UpperX = x1 Else UpperX = x2 LowerX = x1 End If If Bx>0.0 If UpperX<x4 Or x3<LowerX Then Return False ElseIf (UpperX<x3 Or x4<LowerX) Return False EndIf Ay = y2 - y1 By = y3 - y4 If Ay<0.0 LowerY = y2 UpperY = y1 Else UpperY = y2 LowerY = y1 End If If By>0.0 If UpperY<y4 Or y3<LowerY Then Return False ElseIf UpperY<y3 Or y4<LowerY Return False EndIf Cx = x1 - x3 Cy = y1 - y3 d = (By * Cx) - (Bx * Cy) f = (Ay * Bx) - (Ax * By) If f>0.0 If d<0.0 Or d>f Then Return False ElseIf d>0.0 Or d<f Return False EndIf e = (Ax * Cy) - (Ay * Cx) If f>0.0 Then If e<0.0 Or e>f Then Return False ElseIf e>0.0 Or e<f Then Return False EndIf Return True End Function
Method 1 is always faster for me, H&K's occasionally is slower, but never faster than method 2
Well on mine, mine is twice as fast as method 2, but never faster than method 1The Rnds were the major bottle neck. Test the one Ive just posted. It runs until "Space Down"