SuperStrictFor Local tmpIntersection#[] = EachIn big10psFunction( [0.0,0.0], [1.0,1.0], [0.0,0.0],25 ) Print "Intersection at (" + tmpIntersection[0] + ", " + tmpIntersection[1] + ")"NextFunction big10psFunction#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# ) Local tmpResult#[][] Local tmpIntersectLineCircle#[][] = GetIntersectionLineCircle( pLineStart, pLineEnd, pCircleCenter, pCircleRadius ) Local minX# = Min(pLineStart[0],pLineEnd[0]), maxX# = Max(pLineStart[0],pLineEnd[0]) Local minY# = Min(pLineStart[1],pLineEnd[1]), maxY# = Max(pLineStart[1],pLineEnd[1]) For Local tmpIntersectionPoint#[] = EachIn tmpIntersectLineCircle If tmpIntersectionPoint[0] < maxX And tmpIntersectionPoint[0] > minX And .. tmpIntersectionPoint[1] < maxY And tmpIntersectionPoint[1] > minY Then tmpResult = tmpResult + [tmpIntersectionPoint] EndIf Next Return tmpResult EndFunctionFunction GetIntersectionLineCircle#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# ) Local tmpIntersections#[][] Local p# = pCircleCenter[0], q# = pCircleCenter[1] Local m# = (pLineEnd[1]-pLineStart[1])/(pLineEnd[0]-pLineStart[0]) Local r# = pCircleRadius Local t# = pLineEnd[1]- (m*pLineEnd[0]) Local s# = t-q Local a# = m*m + 1, b# = (2*m*s) - (2*p), c# = s^2 + p^2 - (r^2) Local bsqminfourac# = b*b-4*a*c If bsqminfourac > 0 Then bsqminfourac = Sqr(bsqminfourac) Local x1# = ((-b)+bsqminfourac)/(2*a) Local x2# = ((-b)-bsqminfourac)/(2*a) tmpIntersections = [[x1,(m*x1)+t],[x2,(m*x2)+t]] ElseIf bsqminfourac = 0 Then tmpIntersections = [[(-b)/(2*a),(-b*m)/(2*a)+t]] EndIf Return tmpIntersectionsEndFunction
SuperStrictLocal tmpIntersectLineCircle#[][]Graphics 640, 480, 0Local mx:Float, my: FloatWhile Not KeyHit(key_escape) Cls SetColor(50, 200, 100) DrawCircle(320, 240, 40) mx = MouseX() my = MouseY() SetColor(50, 70, 222) DrawLine(0,0, mx, my) tmpIntersectLineCircle = GetIntersectionLineCircle( [0.0,0.0], [mx, my], [320.0, 240.0], 40.0 ) If tmpIntersectLineCircle.length = 1 SetColor(255, 0,0) DrawCircle(tmpIntersectLineCircle[0][0], tmpIntersectLineCircle[0][1], 4) EndIf If tmpIntersectLineCircle.length = 2 SetColor(255,50,30) DrawText "COLLISION", 290, 20 SetColor(255, 0,0) DrawCircle(tmpIntersectLineCircle[1][0], tmpIntersectLineCircle[1][1], 4) DrawCircle(tmpIntersectLineCircle[0][0], tmpIntersectLineCircle[0][1], 4) EndIf Flip 0WendFunction DrawCircle(xCentre:Float, yCentre:Float, Radius:Float) DrawOval(xCentre - (Radius), yCentre - (Radius), Radius * 2, Radius * 2) End FunctionFunction GetIntersectionLineCircle#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# ) Local tmpIntersections#[][] Local p# = pCircleCenter[0], q# = pCircleCenter[1] Local m# = (pLineEnd[1]-pLineStart[1])/(pLineEnd[0]-pLineStart[0]) Local r# = pCircleRadius Local t# = pLineEnd[1]- (m*pLineEnd[0]) Local s# = t-q Local a# = m*m + 1, b# = (2*m*s) - (2*p), c# = s^2 + p^2 - (r^2) Local bsqminfourac# = b*b-4*a*c If bsqminfourac > 0 Then bsqminfourac = Sqr(bsqminfourac) Local x1# = ((-b)+bsqminfourac)/(2*a) Local x2# = ((-b)-bsqminfourac)/(2*a) tmpIntersections = [[x1,(m*x1)+t],[x2,(m*x2)+t]] ElseIf bsqminfourac = 0 Then tmpIntersections = [[(-b)/(2*a),(-b*m)/(2*a)+t]] EndIf Return tmpIntersectionsEndFunction
SuperStrictLocal tmpIntersectLineCircle#[][]Graphics 640, 480, 0Local mx:Float, my: FloatWhile Not KeyHit(key_escape) Cls SetColor(50, 200, 100) DrawCircle(320, 240, 40) mx = MouseX() my = MouseY() SetColor(50, 70, 222) DrawLine(0,0, mx, my) tmpIntersectLineCircle = big10psFunction( [0.0,0.0], [mx, my], [320.0, 240.0], 40.0 ) If tmpIntersectLineCircle.length = 1 SetColor(255, 0,0) DrawCircle(tmpIntersectLineCircle[0][0], tmpIntersectLineCircle[0][1], 4) EndIf If tmpIntersectLineCircle.length = 2 SetColor(255,50,30) DrawText "COLLISION", 290, 20 SetColor(255, 0,0) DrawCircle(tmpIntersectLineCircle[1][0], tmpIntersectLineCircle[1][1], 4) DrawCircle(tmpIntersectLineCircle[0][0], tmpIntersectLineCircle[0][1], 4) EndIf Flip 0WendFunction DrawCircle(xCentre:Float, yCentre:Float, Radius:Float) DrawOval(xCentre - (Radius), yCentre - (Radius), Radius * 2, Radius * 2) End FunctionFunction big10psFunction#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# ) Local tmpResult#[][] Local tmpIntersectLineCircle#[][] = GetIntersectionLineCircle( pLineStart, pLineEnd, pCircleCenter, pCircleRadius ) Local minX# = Min(pLineStart[0],pLineEnd[0]), maxX# = Max(pLineStart[0],pLineEnd[0]) Local minY# = Min(pLineStart[1],pLineEnd[1]), maxY# = Max(pLineStart[1],pLineEnd[1]) For Local tmpIntersectionPoint#[] = EachIn tmpIntersectLineCircle If tmpIntersectionPoint[0] < maxX And tmpIntersectionPoint[0] > minX And .. tmpIntersectionPoint[1] < maxY And tmpIntersectionPoint[1] > minY Then tmpResult = tmpResult + [tmpIntersectionPoint] EndIf Next Return tmpResult EndFunctionFunction GetIntersectionLineCircle#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# ) Local tmpIntersections#[][] Local p# = pCircleCenter[0], q# = pCircleCenter[1] Local m# = (pLineEnd[1]-pLineStart[1])/(pLineEnd[0]-pLineStart[0]) Local r# = pCircleRadius Local t# = pLineEnd[1]- (m*pLineEnd[0]) Local s# = t-q Local a# = m*m + 1, b# = (2*m*s) - (2*p), c# = s^2 + p^2 - (r^2) Local bsqminfourac# = b*b-4*a*c If bsqminfourac > 0 Then bsqminfourac = Sqr(bsqminfourac) Local x1# = ((-b)+bsqminfourac)/(2*a) Local x2# = ((-b)-bsqminfourac)/(2*a) tmpIntersections = [[x1,(m*x1)+t],[x2,(m*x2)+t]] ElseIf bsqminfourac = 0 Then tmpIntersections = [[(-b)/(2*a),(-b*m)/(2*a)+t]] EndIf Return tmpIntersectionsEndFunction
Function circle_on_plane%(xx#,yy#,rr#, x1#, y1#, x2#, y2#) Local intersectx#, intersecty# Local xdif#, ydif# xdif = x2-x1 ydif = y2-y1 If xdif=0 And ydif=0 Then Return False EndIf If xdif<>0 Then Local a1#,b1#,a2#,b2# a1 = ydif/xdif b1 = y1-a1*x1 If a1=0 Then intersectx = xx intersecty = y1 Else a2 = -1/a1 b2 = yy-a2*xx intersectx = (b2-b1)/(a1-a2) intersecty = a1*intersectx+b1 EndIf If rr<point_distance(xx,yy,intersectx,intersecty) Then Return False EndIf ElseIf ydif<>0 Then Local a1#,b1#,a2#,b2# a1 = xdif/ydif b1 = x1-a1*y1 If a1=0 Then intersectx = x1 intersecty = yy Else a2 = -1/a1 b2 = xx-a2*yy intersecty = (b2-b1)/(a1-a2) intersectx = a1*intersecty+b1 EndIf If rr<point_distance(xx,yy,intersectx,intersecty) Then Return False EndIf EndIf If Not (point_distance(xx,yy,x1,y1)>rr And point_distance(xx,yy,x2,y2)>rr) Then Return True EndIf If x1<x2 Then If y1<y2 Then If (x1>intersectx Or y1>intersecty Or x2<intersectx Or y2<intersecty) Then Return False EndIf Else If (x1>intersectx Or y1<intersecty Or x2<intersectx Or y2>intersecty) Then Return False EndIf EndIf Else If y1<y2 Then If (x1<intersectx Or y1>intersecty Or x2>intersectx Or y2<intersecty) Then Return False EndIf Else If (x1<intersectx Or y1<intersecty Or x2>intersectx Or y2>intersecty) Then Return False EndIf EndIf EndIf Return True EndFunction