Code: Select all
//ESIMERKKI
SCREEN 800,600
DrawToWorld ON,ON,ON
Repeat
Color cbwhite
Line 0,0,MouseWX(),MouseWY()
If CLCollision(0,0,MouseWX(),MouseWY(),100,100,100) Then Color cbred
Circle2(100,100,100)
DrawScreen
Forever
Function Circle2(x,y,r)
Circle x-2*r/2,y+2*r/2,2*r,0
EndFunction
//FUNCTION
Function CLCollision(x1,y1,x2,y2,cx,cy,cr)
dx# = x2 - x1
dy# = y2 - y1
a# = dx * dx + dy * dy
b# = 2 * (dx * (x1 - cx) + dy * (y1 - cy))
c# = cx * cx + cy * cy
c = c + x1 * x1 + y1 * y1
c = c - 2 * (cx * x1 + cy * y1)
c = c - cr * cr
D# = b * b - 4 * a * c
If D < 0 Then // Not intersecting
Return False
Else
mu# = (-b + Sqrt( b*b - 4*a*c )) / (2*a)
ix1# = x1 + mu*(dx)
iy1# = y1 + mu*(dy)
mu = (-b - Sqrt(b*b - 4*a*c )) / (2*a)
ix2# = x1 + mu*(dx)
iy2# = y1 + mu*(dy)
// Figure out which point is closer to the circle
If Distance(x1, y1, cx, cy) < Distance(x2, y2, cx, cy) Then
testX# = x2
testY# = y2
Else
testX# = x1
testY# = y1
EndIf
If Distance(testX, testY, ix1, iy1) < Distance(x1, y1, x2, y2) Or Distance(testX, testY, ix2, iy2) < Distance(x1, y1, x2, y2) Then
Return True
Else
Return False
EndIf
EndIf
EndFunction