Line Joining two circles

HI,
I am trying to think of the best way of doing something simple and am stuck.
I have two circles on the screen and I can move them around, I have a line between the centres of the two circles and I need to for the line to grow, shrink and move so that it always remains connected to the circles as I move one of them.

Can anyone suggest the best way of achieving this or point me in the direction of an example.

Many thanks

Richard [import]uid: 7830 topic_id: 6459 reply_id: 306459[/import]

HI,
Thanks for that, problem now solved.
Most kind.
Richard [import]uid: 7830 topic_id: 6459 reply_id: 23846[/import]

[lua]-- factory for basic draggable circles
function make_circle( x, y, r )
local c = display.newCircle( x, y, r )
c:setFillColor( 127, 127, 127 )
c:addEventListener( ‘touch’, function( e )
if e.phase == ‘began’ then
c:setFillColor( 0, 255, 0 ) – hilight while dragging
elseif e.phase == ‘ended’ or e.phase == ‘cancelled’ then
c:setFillColor( 127, 127, 127 ) – un-hilight when done dragging
elseif e.phase == ‘moved’ then
c.x = e.x; c.y = e.y
end
end )
return c
end

– factory for connector lines between draggable objects
– note that since the connector line is destroyed and re-created every
– frame any direct pointers to it will invalidate, so instead we have
– encapsulated it in a group container so that the container’s
– properties and scene Z-order can survive.
function make_connector( o1, o2 )
local g = display.newGroup()
– record pointers to the objects this line connects
g.o1 = o1; g.o2 = o2
– define a method which destroys (if it exists) and redraws the line
g.redraw = function()
– if there is an old line in place, destroy it
if g.l then
g.l:removeSelf()
end
– draw a new line based on the current coordinates of the two
– objects and record a pointer to this line in the group container
g.l = display.newLine( g, g.o1.x, g.o1.y, g.o2.x, g.o2.y )
g.l:setColor( 255, 255, 0 )
g.l.width = 3
end
– and register it to redraw the line every frame
Runtime:addEventListener( ‘enterFrame’, g.redraw )
– return the group object
return g
end
– now make three circles
local circles = {
make_circle( 100, 100, 15 ),
make_circle( 150, 100, 20 ),
make_circle( 120, 150, 25 ),
}
– make two lines connecting them
local connectors = {
make_connector( circles[1], circles[2] ),
make_connector( circles[2], circles[3] ),
}
– move the connector lines’ Z-order behind the circles (if you like)
for i, v in ipairs( connectors ) do
v:toBack()
end[/lua] [import]uid: 32962 topic_id: 6459 reply_id: 23823[/import]