Circle/Math Question

BlitzMax Forums/BlitzMax Programming/Circle/Math Question

Arcadenut(Posted 2005) [#1]
What I want to do is be able to constrict an objec to within a circle. Doing it inside a sqaure is easy, but I'm finding the Circle to be a bit of a challenge :-)

If anyone can help, I would greatly appreciate it!

Here is some sample code to demo what I am looking for.

Graphics 800, 600

Local cX# = 400
Local cY# = 300
Local cW# = 100
Local cH# = 100

Local x#
Local y#

Local r%

Local pX# = cX#
Local pY# = cY#

While Not KeyHit(KEY_ESCAPE)

	Cls()	
	DrawText "I want the WHITE cricle to be constricted to the inside of the Green Circle", 0,0
	DrawText "It should be able to travel smoothly around the inside of the Green Circle", 0,18
	SetColor(0,255,0)
	For r% = 0 To 360
		x# = cX# + (sin(r%) * cW)
		y# = cY# + (cos(r%) * cH)
		Plot x#, y#
	Next
	SetColor(255,255,255)
	DrawOval pX, pY, 5,5
	
	If KeyDown(KEY_LEFT) Then
		pX :- 1
	EndIf

	If KeyDown(KEY_RIGHT) Then
		pX :+ 1
	EndIf
	
	If KeyDown(KEY_UP) Then
		pY :- 1
	EndIf

	If KeyDown(KEY_DOWN) Then
		pY :+ 1
	EndIf
	
	' How do I do this...
	' IF pX on the edge of the Circle pX = the Edge of the circle
	' IF pY on the edge of the Circle pY = the Edge of the circle
	
	
	Flip()

Wend




LarsG(Posted 2005) [#2]
Maybe something like this?:
Graphics 800, 600

Global cX# = 400
Global cY# = 300
Global cW# = 100
Global cH# = 100

Local x#
Local y#

Local r%

Local pX# = cX#
Local pY# = cY#

While Not KeyHit(KEY_ESCAPE)

	Cls()	
	DrawText "I want the WHITE cricle to be constricted to the inside of the Green Circle", 0,0
	DrawText "It should be able to travel smoothly around the inside of the Green Circle", 0,18
	SetColor(0,255,0)
	For r% = 0 To 360
		x# = cX# + (Sin(r%) * cW)
		y# = cY# + (Cos(r%) * cH)
		Plot x#, y#
	Next
	SetColor(255,255,255)
	DrawOval pX, pY, 5,5
	
	If KeyDown(KEY_LEFT) Then
		If check(px-1,py)
			pX :- 1
		EndIf
	EndIf

	If KeyDown(KEY_RIGHT) Then
		If check(px+1,py)
			pX :+ 1
		EndIf
	EndIf
	
	If KeyDown(KEY_UP) Then
		If check(px,py-1)
			pY :- 1
		EndIf
	EndIf

	If KeyDown(KEY_DOWN) Then
		If check(px,py+1)
			pY :+ 1
		EndIf
	EndIf
	
	' How do I do this...
	' IF pX on the edge of the Circle pX = the Edge of the circle
	' IF pY on the edge of the Circle pY = the Edge of the circle
	
	
	Flip()

Wend

Function check(x,y)
	Local hyp:Float
	hyp = Sqr(((x-cX)^2) + ((y-cY)^2))
	If hyp > cW
		Return False
	Else
		Return True
	EndIf
EndFunction



Arcadenut(Posted 2005) [#3]
That's close, but as you try to move the WHITE Circle around the edge of the GREEN Circle, the WHITE circle get's stuck. I am trying to make it move smoothly around the inside of the circle.

I'm thinking that in order to do that, I would have to move the point back away from the wall towards the center so it doesn't get stuck.


ImaginaryHuman(Posted 2005) [#4]
If you think of the smaller inner circle as being at a given radius and angle from the center of the big circle, surely you just need to make sure the radius doesn't get bigger than a certain amount (ie the radius of the big circle minus half the radius of the small one?)