Hi all
How can i detect if a line I am drawing intersects an oval ?
How can i detect if a line I am drawing intersects an oval ?
The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.
Strict Type vector2d Field x:Int Field y:Int End Type Const R:Int = 15 Global p1:vector2d = New vector2d Global p2:vector2d = New vector2d Global p3:vector2d = New vector2d Global p:vector2d = New vector2d Global m:Float p1.x = 250;p1.y=250 p2.x = 400;p2.y=300 Graphics 800,600,32 Function pointincircle:Int(p:vector2d,c:vector2d) Return r*r >= getlength(c,p) End Function Function getlength:Int(v1:vector2d,v2:vector2d) Return (v2.x-v1.x) * (v2.x-v1.x) + (v2.y-v1.y) * (v2.y-v1.y) End Function While Not KeyDown(key_escape) p3.x = MouseX() p3.y = MouseY() DrawOval(p3.x,p3.y,R*2,R*2) DrawLine p1.x,p1.y,p2.x,p2.y m=(p2.y-p1.y)/(p2.x-p1.x) p.y = (m*(m*p3.y+p3.x-p2.x)+p2.y)/(1+m*m) p.x = p3.x + m*(p3.y-p.y) If(p.x >= p1.x And p.x <= p2.x) Or (p.x < p1.x And p.x > p2.x) Then DrawText "On Line Collision",100,100 If(r*r >= getlength(p3,p)) Then DrawText "end point collision",100,100 ElseIf (pointincircle(p1,p3)) Then DrawText "end point collision",100,100 ElseIf (pointincircle(p2,p3)) Then DrawText "end point collision",100,100 DrawOval p.x,p.y,5,5 DrawLine p3.x,p3.y,p.x,p.y Flip Cls Wend
Strict Function intersection:Float(x1:Float,y1:Float,x2:Float,y2:Float,x3:Float,y3:Float,r:Float) Rem x1,y1,z1 p1 coords (point of line) x2,y2,z2 p2 coords (point of line) x3,y3,z3 p3 coords and radius (sphere) x,y,z intersection End Rem Local x:Float Local y:Float Local a:Float Local b:Float Local c:Float Local mu:Float Local i:Float Local p:Float[7] a = (x2-x1)^2 + (y2-y1)^2 b = 2*((x2-x1)*(x1-x3)+(y2-y1)*(y1-y3)) c = x3^2 + y3^2 + x1^2 + y1^2 - 2*(x3*x1+y3*y1) - r^2 i = b*b - 4*a*c If ( i < 0) Then p[0] = 0 Return 0 End If If(i=0) Then p[0] = 1 Return 1 End If If(i > 2) Then Return 2 End If Return 0 End Function Graphics 800,600,32 While Not KeyDown(key_escape) DrawLine 10,10,100,100 DrawOval MouseX(),MouseY(),30,30 Local g:Float=0 Local dx:Float = MouseX() - 10 Local dy:Float = MouseY() - 10 Local mag:Float = Sqr(dx^2 + dy^2) Local dlx:Float = 100-10 Local dly:Float = 100-10 Local linemag:Float = Sqr(dlx^2+dly^2) If mag <= linemag Then g = intersection(10,10,100,100,MouseX(),MouseY(),15) If g > 0 Then DrawText "Collision!",100,100 End If DrawText mag, 200,200 Flip Cls Wend