October 28, 2020, 06:38:48 AM

Author Topic: [bmx] Circle Circle Instersection Points by Haramanai [ 1+ years ago ]  (Read 1360 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Circle Circle Instersection Points
Author : Haramanai
Posted : 1+ years ago

Description : It's not my code and it's just a translation of a java code in BlitzMax
All credits go here

    Circle Intersection
    By William Ngan (http:/www.metaphorical.net)
    Processing BETA code (http://www.processing.org)


Code :
Code: BlitzMax
  1. Rem
  2.         Circle Intersection
  3.          By William Ngan (http:/www.metaphorical.net)
  4.          Processing BETA code (http://www.processing.org)
  5. End Rem
  6.  
  7. Type Circle
  8.  
  9.         Field x, y, r, r2
  10.  
  11.         Function create:Circle( px#, py#, pr# )
  12.                 Local Circle_:Circle = New Circle
  13.                 Circle_.x = px
  14.                 Circle_.y = py
  15.                 Circle_.r = pr
  16.                 Circle_.r2 = pr*pr
  17.                 Return Circle_
  18.         End Function
  19.  
  20. End Type
  21.  
  22.  
  23. Function intersect( cA:Circle , cB:Circle )
  24.  
  25.         Local dx# = cA.x - cB.x
  26.         Local  dy# = cA.y - cB.y
  27.         Local  d2# = dx*dx + dy*dy
  28.         Local  d# = Sqr( d2 )
  29.  
  30.         If  d>cA.r+cB.r Or d<Abs(cA.r-cB.r) Return'; // no solution
  31.        
  32.  
  33.         Local a# = (cA.r2 - cB.r2 + d2) / (2*d)
  34.         Local h# = Sqr( cA.r2 - a*a )
  35.         Local x2# = cA.x + a*(cB.x - cA.x)/d
  36.         Local y2# = cA.y + a*(cB.y - cA.y)/d
  37.  
  38.  
  39.         DrawRect( x2, y2, 5, 5 )
  40.  
  41.         Local paX% =  x2 + h*(cB.y - cA.y)/d
  42.         Local paY% =  y2 - h*(cB.x - cA.x)/d
  43.         Local pbX# = x2 - h*(cB.y - cA.y)/d
  44.         Local pbY# = y2 + h*(cB.x - cA.x)/d
  45.         DrawText pax , 10 , 10
  46.         DrawRect paX , PaY , 5 , 5
  47.         DrawRect pbX , PbY , 5 , 5
  48.  
  49. End Function
  50.  
  51. Function DrawCircle(R:Circle)
  52.         DrawOval R.x - R.r  , R.y - R.r , R.r*2 , R.r*2
  53. End Function
  54.  
  55. Local Cir:Circle = Circle.create(10 , 10 , 100)
  56. Local Cir2:Circle = Circle.create(100 , 100 , 50)
  57.  
  58. Graphics 640 , 480 , 0
  59.  
  60. While Not KeyDown(Key_Escape)
  61.         Cls
  62.         SetColor 255 , 0 , 0
  63.         drawCircle(Cir)
  64.         SetColor 0 , 255 , 0
  65.         drawCircle(Cir2)
  66.         SetColor 0 , 0 , 255
  67.         intersect(cir , cir2)
  68.         Cir.X = MouseX()
  69.         Cir.Y = MouseY()
  70.         Flip
  71.         FlushMem
  72. Wend


Comments :


Haramanai(Posted 1+ years ago)

 And just a function
Code: [Select]

Function Circle_Circle_intersect(paX# Var , paY# Var , pbX# Var , pbY# Var , x1# , y1# , r1# , x2# , y2# , r2#)


Local dx# = x1 - x2
Local  dy# = y1 - y2
Local  d2# = dx*dx + dy*dy
Local  d# = Sqr( d2 )

If  d>r1+r2 Or d<Abs(r1-r2) Then Return False'; // no solution


Local a# = (r1*r1 - r2*r2 + d2) / (2*d)
Local h# = Sqr( r1*r1 - a*a )
Local rx2# = x1 + a*(x2 - x1)/d
Local ry2# = y1 + a*(y2 - y1)/d


paX# =  rx2 + h*(y2 - y1)/d
paY# =  ry2 - h*(x2 - x1)/d
pbX# = rx2 - h*(y2 - y1)/d
pbY# = ry2 + h*(x2 - x1)/d

Return True

End Function



Bobysait(Posted 1+ years ago)

 we maybe could optimise using simple previous test:
Code: [Select]
Function intersect( cA:Circle , cB:Circle )
Local R0:Float=cA.r+cB.r
Local dx:Float=cA.x - cB.x
' no solution ------------------
If Abs(dx)>R0 Return False
Local dy:Float=cA.y - cB.y
If Abs(dy)>R0 Return False
Local  d2# = dx*dx + dy*dy
If d2>R0*R0 Return False
Local R_:Float=cA.r-cB.r
If d2<R_*R_ Return False
' ------------------------------
Local  d# = Sqr( d2 )
[...]
End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal