Set max distance of dragging an object away from starting point.

I am fairly new to programming, especially with Lua & Corona.

I am using a slingshot type mechanic and would like the object to stop following the mouse(finger) when it reaches a certain distance (like the maximum stretch). However I still want the angle of the object to change and the object to follow the mouse around the center point (starting position before pulling the object back)… Just not go any further from the set distance.

http://developer.coronalabs.com/code/simple-analog-stick-joystick-module

This analog stick does exactly what I want, but with all the variables and other bits and pieces I don’t understand quite yet, I am having trouble figuring out how it works.

This is the code for the max distance of the pull back (i hope, lol).

S.distance    = Sqr (ex*ex + ey*ey)

if S.distance > S.maxDist then S.distance = S.maxDist end

S.angle       = ( (Atan2( ex-0,ey-0 )*180 / Pi) - 180 ) * -1

S.percent     = S.distance / S.maxDist

T.x       = Cos( Rad(S.angle-90) ) * (S.maxDist * S.percent) 

T.y       = Sin( Rad(S.angle-90) ) * (S.maxDist * S.percent) 

If someone could let me know the calculations to working this out, that’d be great.

I assume I need to figure out the angle and the distance away from the center point, and have the object “draw” a circle following my mouse from calculations based on those…