I like the idea of a non-algebraic solution too.
Here are some suggested simplifications:

load 'math/misc/amoeba'

NB. y is Cx0 Cy0 R0  Cx1 Cy1 R1  Cx2 Cy2 R2
NB. x are radius scale factors to control which circles are included
NB.   in the common tangent circle.  1 to surround, _1 to exclude.
NB. returns (center, radius); final simplex volume
apollonius =: verb define"1 _
 1 apollonius y
:
 c=. 2{."1 y             NB. centers
 r=. x * {:"1 y          NB. radii
 v=. 1e_20               NB. goal simplex volume
 dist=: r 2 : 'm + [: +/"1&.:*: n -"1 ]' c  NB. distances to tangents
 'soln err'=. ([: +/@:*:@, -/~...@dist) amoeba v c  
 if. err > 10 * v do. '' return. end.       NB. no solution found
 avg =. +/ % #
 (, a...@dist) soln
)

Note 'Example Use'
 rctst=: 0 0 1,4 0 1,:2 4 2           NB. Rosetta Code test data
 (_1 _1 _1 ,: 1 1 1) apollonius rctst NB. both solutions
)

NB. Display Solutions
NB. =================

require 'graph'
NB. convert between circle definition formats 
NB. (centerX,centerY,radius) to/from 
NB. (topleftX,topleftY,width,height)
cr2xywh=: 2&{."1 (- ,. 2&#@:,:@+:@]) {:"1  
xywh2cr=: 2&{."1 (+ ,. ]) -:@:{:"1  NB. not strictly required

draw=: [: gdellipse gddraw 0.1 * cr2xywh@,

Note 'display solutions'
 draw rctst                           NB. initial circles
 (draw _1 _1 _1&apollonius) rctst     NB. internally tangent solution
 (draw 1 1 1&apollonius) rctst        NB. externally tangent solution
 (draw ((,:-)1 1 1)&apollonius) rctst NB. both required solutions
 (draw (<:+:#:i.8)&apollonius) rctst  NB. all (5) solutions
)

> From: David Ward Lambert
> Sent: Sunday, 19 September 2010 02:18
> 
> NB.I propose this solution.  The posted solutions are algebraic.
> 
> NB. amoeba is the simplex optimization method found at
> www.jsoftware.com
> 
> require'amoeba'['/usr/local/j64-602/addons/math/misc/amoeba.ijs'
> 
> NB. y is Cx0 Cy0 R0  Cx1 Cy1 R1  Cx2 Cy2 R2
> NB. x are radius scale factors to control which circles are included
> NB.   in the common tangent circle.  1 to surround, _1 to exclude.
> NB. returns (center, radius); final simplex volume
> apollonius =: 3 :0
>  1 apollonius y
> :
>  a=.9$0 0 1              NB. boolean selection vector
>  c=.3 2$(-.a)#,y         NB. centers
>  r=.x*a#,y               NB. radii
>  v=.1e_20                NB. goal simplex volume
>  NB. scale the problem rather than the volume?
>  d=:r 2 : 'm + [: +/"1&.:*: n -"1 ]' c NB. distances to tangents
>  s=.([: +/ [: *:@, -/~@:d) amoeba v c              NB. solution
>  avg =. +/ % #
>  ((, avg@:d)&.:>@:{. , {:)s
> )
> 
> NB. all 8 solutions
> (<:+:#:i.8)apollonius"1 _ ] _1 1 1,4 2 2,:1 _3 3
> 
> 
> NB........graphical demonstration...........
> 
> load'graph'
> 
> p0 =: 1,.~|:2 1 o./ 2p1 * (i. % <:) 80
> 
> draw =: 4 : 0
>  gdclose name=:'apollo'
>  gdopen name
>  gdcolor GREEN
>  gdpolygon ,/"2 p0(+/ .*)("_ _1)0.1*((0 1,3 4,:6 7){,y),"2 1~(2 5
> 8{,y)*/=i.2
>  gdcolor RED
>  gdlines ,p0+/ .*0.1*(2{.,x),~({:,x)*=i.2
>  gdshow name
> )
> 
> (draw~ >@{.@:((-_1 1 1)&apollonius))_1 1 1,4 2 2,:1 _3 3
> 
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to