I've also made a small 2D line-intersection, I think nearly everyone has already made one and posted it here.. anyway. ^^
I've commented the code, and I think it's easy to understand what I've done in the function.
The function returns, if the lines collide, and two other functions can return the coordinates then.
The code includes an example:
rem General Screen-Settings
set display mode 640,480,32
sync on : sync rate 0
rem Program-Settings
Line_Start()
x = rnd(640)
y = rnd(480)
rem Main-Loop
do
rem clear screen
cls
rem Show lines
line 50,50,300,200
line x,y,mousex(),mousey()
rem Reposition line
if mouseclick() then x = mousex() : y = mousey()
rem Calculation
mx = mousex()
my = mousey()
t = timer()
for run = 1 to 10000
col = LineIntersection(50,50,300,200, x,y,mx,my)
next run
t = timer()-t
rem Show Collision-Point
if col = 1 then ink rgb(255,0,0),0 else ink rgb(128,128,128),0
line Line_ColX()-5,Line_ColY()-5,Line_ColX()+5,Line_ColY()+5
line Line_ColX()-5,Line_ColY()+5,Line_ColX()+5,Line_ColY()-5
ink rgb(255,255,255),0
rem Show Information
print "Move mouse or click to change line-coordinates!"
print "Collision: ", col
print "Calculation-time: ", t, "/10,000 ms"
rem Update screen
sync
loop
rem This function should be called before the other 3 functions
function Line_Start()
global Line_Col_X as integer
global Line_Col_Y as integer
endfunction
rem This function returns the X-Coordinate of the last collision (so LineIntersection(...) has to be called before this function)
function Line_ColX()
endfunction Line_Col_X
rem This function returns the collisions Y-Coordinate
function Line_ColY()
endfunction Line_Col_Y
rem This function calculates
function LineIntersection(XS1,YS1,XE1,YE1, XS2,YS2,XE2,YE2)
rem Debugging: Vertical lines are not allowed!
if XS1 = XE1 then XE1 = XS1+1
if XS2 = XE2 then XE2 = XS2+1
rem First line
rem y=mx+b
rem Calculate m
xdif1 = (XE1-XS1)
ydif1 = (YE1-YS1)
m1# = ydif1/(1.0*xdif1)
rem Calculate b
b1# = YS1-XS1*m1#
rem Second line
rem Calculate m
xdif2 = (XE2-XS2)
ydif2 = (YE2-YS2)
m2# = ydif2/(1.0*xdif2)
rem Calculate b
b2# = YS2-XS2*m2#
rem Collision-Detection
rem m1#*x+b1# = m2#*x+b2# | -(m2#*x)-b1#
rem <=> (m1#-m2#)*x = b2#-b1# | /(m1#-m2#)
rem <=> x = (b2#-b1#)/(m1#-m2#)
rem Calculate X-Pos
x# = (b2#-b1#)/(m1#-m2#)
Line_Col_X = x#
rem Calculate Y-Pos
rem y=mx+b
rem y# = m1#*x#+b1#
y# = m1#*x#+b1#
Line_Col_Y = y#
rem Make sure that XS<XE, YS<YE
if XS1 > XE1 then XT1 = XS1 : XS1 = XE1 : XE1 = XT1` : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if YS1 > YE1 then YT1 = YS1 : YS1 = YE1 : YE1 = YT1`XT1 = XS1 : XS1 = XE1 : XE1 = XT1 : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if XS2 > XE2 then XT2 = XS2 : XS2 = XE2 : XE2 = XT2` : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
if YS2 > YE2 then YT2 = YS2 : YS2 = YE2 : YE2 = YT2`XT2 = XS2 : XS2 = XE2 : XE2 = XT2 : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
rem Check, if lines collide
if x# => XS1
if x# <= XE1
if x# => XS2
if x# <= XE2
if y# => YS1
if y# <= YE1
if y# => YS2
if y# <= YE2
exitfunction 1
endif
endif
endif
endif
endif
endif
endif
endif
endfunction 0
Maybe someone knows if it can be improved anywhere.. but I think with 0.0004 ms/calculation on my lower-average-computer it's fast enough to use it in realtime applications for 2D Sprite-Collisions etc..
Have fun with it.
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1