Circle_Circle_Intersection_Points

BlitzMax Forums/BlitzMax Programming/Circle_Circle_Intersection_Points

It's not my code and it's just a translation of a java code in BlitzMax from this one : http://www.metaphorical.net/code/processing/circle_intersect.html
I believe it will help lot of people int this community.
So here it is.

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

Type Circle 

	Field x, y, r, r2

	Function create:Circle( px#, py#, pr# ) 
		Local Circle_:Circle = New Circle
		Circle_.x = px
		Circle_.y = py
		Circle_.r = pr
		Circle_.r2 = pr*pr
		Return Circle_
	End Function

End Type


Function intersect( cA:Circle , cB:Circle ) 

	Local dx# = cA.x - cB.x
	Local  dy# = cA.y - cB.y
	Local  d2# = dx*dx + dy*dy
	Local  d# = Sqr( d2 )

	If  d>cA.r+cB.r Or d<Abs(cA.r-cB.r) Return'; // no solution
	

	Local a# = (cA.r2 - cB.r2 + d2) / (2*d)
	Local h# = Sqr( cA.r2 - a*a )
	Local x2# = cA.x + a*(cB.x - cA.x)/d
	Local y2# = cA.y + a*(cB.y - cA.y)/d


	DrawRect( x2, y2, 5, 5 )

	Local paX% =  x2 + h*(cB.y - cA.y)/d 
	Local paY% =  y2 - h*(cB.x - cA.x)/d 
	Local pbX# = x2 - h*(cB.y - cA.y)/d
	Local pbY# = y2 + h*(cB.x - cA.x)/d
	DrawText pax , 10 , 10
	DrawRect paX , PaY , 5 , 5
	DrawRect pbX , PbY , 5 , 5

End Function

Function DrawCircle(R:Circle)
	DrawOval R.x - R.r  , R.y - R.r , R.r*2 , R.r*2
End Function

Local Cir:Circle = Circle.create(10 , 10 , 100)
Local Cir2:Circle = Circle.create(100 , 100 , 50)

Graphics 640 , 480 , 0

While Not KeyDown(Key_Escape)
	Cls
	SetColor 255 , 0 , 0
	drawCircle(Cir)
	SetColor 0 , 255 , 0
	drawCircle(Cir2)
	SetColor 0 , 0 , 255
	intersect(cir , cir2)
	Cir.X = MouseX()
	Cir.Y = MouseY()
	Flip
	FlushMem
Wend



I hope this is not violeting any license.

or just a function to get the two points

Rem
	aX and aY are the above point and will give their values to the float that will be passed to the function
	bX and bY are the bottom point and will give their values to the float that will be passed to the function
	x1 , y1 are the center of the first circle and r1 is the radius
	x2 , y2 , are the center of the second circle and r2 the radius
End Rem

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


You should put this in the code archive.

I guess it might be useful, thanks,.


You should put this in the code archive.


But this is not my code. It's just a translation.

so what? the code archive's for simple routines like this one, which have almost all been thought of before by somebody else. And I don't think we've quite got to the point where someone can copyright geometry...

You coded the translation, so it has become your own code, unless the algorithm is protected by patent (as is the marching ants code). Although even in that case someone posted code for it.

And, besides that: you posted it here. Why is that different to posting it in the code archive?