Finish Line end on intersection

Blitz3D Forums/Blitz3D Beginners Area/Finish Line end on intersection

If you look at my example code (no media) it will be clearer what i'm trying to do. I have a ball bounce around the screen, if i left-click a wall (or line) will start to expand outwards from the place where a clicked on the x axis. Same thing goes for right-clicking except it will expand on the y axis. Once the lines collide with the edge of the screen, they're set. If the lines aren't set the ball can break through them.
Now what i need is a way to determine if a wall collided with another wall, and then to set that side but continue the expansion of the other side until another collision if detect on that point.

Here's what ive got so far. Thanks for helping:

Graphics 800,600,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Type ball
	Field x,y
	Field xv,yv
End Type 

Type wall
	Field x,y,x2,y2
	Field wd,hg,placed1,placed2,axis ;(1 for x,2 for y)
End Type 

Setup()

While Not KeyDown(1)
	
	UpdateBalls()
	UpdateWalls()
	
	Draw()
	
Wend 
End 

Function Setup()
	
	b.ball=New ball
	b\x=Rand(100,700)
	b\y=Rand(100,500)
	b\xv=Rand(-5,5)
	b\yv=Rand(-5,5)
	
End Function

Function UpdateBalls()
	
	For b.ball=Each ball
		
		b\x=b\x+b\xv
		b\y=b\y+b\yv
		
		If b\x>=800 Then b\xv=-b\xv
		If b\x<=0 Then b\xv=-b\xv
		If b\y>=600 Then b\yv=-b\yv
		If b\y<=0 Then b\yv=-b\yv
		
		For w.wall=Each wall
		
			If LineOverlapCircle(w\x,w\y,w\x2,w\y2,b\x,b\y,5)=1 Then 
			If w\placed1=1 And w\placed2=1 Then 
			b\xv=-b\xv
			b\yv=-b\yv
			Else 
			Delete w
			Return 
			EndIf 
			EndIf 
				
		Next 
	Next 
	
End Function 

Function UpdateWalls()
	
p=GetMouse()

For w.wall=Each wall
		
		If w\placed1=0 And w\placed2=0
			If w\axis=1 Then 
				If Not w\x2>800 And w\placed2=0 Then w\x2=w\x2+2
				If Not w\x<0 And w\placed1=0 Then w\x=w\x-2
				If w\x<0 And w\x2>800 Then w\placed1=1:w\placed2=1
			Else 
				If Not w\y2>600 And w\placed2=0 Then w\y2=w\y2+2
				If Not w\y<0 And w\placed1=0 Then w\y=w\y-2
				If w\y<0 And w\y2>600 Then w\placed1=1:w\placed2=1
			EndIf 
			
			CheckWallCollisions()
			
		EndIf 
	
Next 

If MouseHit(1) Then 
		w.wall=New wall
		w\x=MouseX()
		w\y=MouseY()
		w\hg=1
		w\wd=1
		w\x2=MouseX()
		w\y2=MouseY()
	
		w\axis=1
		FlushMouse
		Return 
EndIf 
		
If MouseHit(2) Then 
		w.wall=New wall
		w\x=MouseX()
		w\y=MouseY()
		w\hg=1
		w\wd=1
		w\x2=MouseX()
		w\y2=MouseY()
		w\axis=2
		FlushMouse
		Return 
EndIf 

End Function

Function Draw()
Local draw

	Cls 
	
	For b.ball=Each ball
		Oval b\x,b\y,10,10,1
	Next 
	
	For w.wall=Each wall
		Line w\x,w\y,w\x2,w\y2
	Next 
	Flip
	
End Function 

Function LineOverlapCircle(x1#,y1#,x2#,y2#,cx#,cy#,Radius#)
	;'Calc Closest Point To circle center
	Local dx31#=cx#-x1#
	Local dx21#=x2#-x1#
	Local dy31#=cy#-y1#
	Local dy21#=y2#-y1#
	Local d#=((dx21#*dx21#)+(dy21#*dy21#))
	If d#<>0 Then d#=((dx31#*dx21#)+(dy31#*dy21#))/d#
	;'Clip To the line segments legal bounds
	If d#<0.0 Then d#=0
	If d#>1.0 Then d#=1
	Local dx#=cx#-(x1#+(dx21#*d#))
	Local dy#=cy#-(y1#+(dy21#*d#))
	If Radius# => Sqr((dx#*dx#)+(dy#*dy#))
		;'Line intersects circle
		Return 1 
	EndIf
	Return 0
End Function

Function linesIntersect(x1%,y1%, x2%,y2%, x3%,y3%, x4%,y4%)

	numeratorA#  = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
	numeratorB#  = (x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
	denominator# = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)

	If denominator = 0.0 Then
;		Text(10,20,"No intersection")
;		If numeratorA = 0.0 And numeratorB = 0.0 And denominator = 0.0 Then
;			Text(10,40,"Lines are coincident")
;		Else
;			Text(10,40,"Lines are parallel")
;		End If
		Return False
	Else
		Ua# = numeratorA/denominator
		Ub# = numeratorB/denominator
		range1% = Ua >= 0.0 And Ua <= 1.0
		range2% = Ub >= 0.0 And Ub <= 1.0
		If range1 And range2 Then
			intersectX% = Floor(x1 + Ua*(x2-x1)+.5)
			intersectY% = Floor(y1 + Ua*(y2-y1)+.5)
			Return True
		Else
;			Text(10,20,"No intersection")
			Return False
		End If
    End If
End Function

Function CheckWallCollisions()
End Function