September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,53 @@
|
|||
// version 1.1.3
|
||||
|
||||
data class Circle(val x: Double, val y: Double, val r: Double)
|
||||
|
||||
val Double.sq get() = this * this
|
||||
|
||||
fun solveApollonius(c1: Circle, c2: Circle, c3: Circle,
|
||||
s1: Int, s2: Int, s3: Int): Circle {
|
||||
val (x1, y1, r1) = c1
|
||||
val (x2, y2, r2) = c2
|
||||
val (x3, y3, r3) = c3
|
||||
|
||||
val v11 = 2 * x2 - 2 * x1
|
||||
val v12 = 2 * y2 - 2 * y1
|
||||
val v13 = x1.sq - x2.sq + y1.sq - y2.sq - r1.sq + r2.sq
|
||||
val v14 = 2 * s2 * r2 - 2 * s1 * r1
|
||||
|
||||
val v21 = 2 * x3 - 2 * x2
|
||||
val v22 = 2 * y3 - 2 * y2
|
||||
val v23 = x2.sq - x3.sq + y2.sq - y3.sq - r2.sq + r3.sq
|
||||
val v24 = 2 * s3 * r3 - 2 * s2 * r2
|
||||
|
||||
val w12 = v12 / v11
|
||||
val w13 = v13 / v11
|
||||
val w14 = v14 / v11
|
||||
|
||||
val w22 = v22 / v21 - w12
|
||||
val w23 = v23 / v21 - w13
|
||||
val w24 = v24 / v21 - w14
|
||||
|
||||
val p = -w23 / w22
|
||||
val q = w24 / w22
|
||||
val m = -w12 * p - w13
|
||||
val n = w14 - w12 * q
|
||||
|
||||
val a = n.sq + q.sq - 1
|
||||
val b = 2 * m * n - 2 * n * x1 + 2 * p * q - 2 * q * y1 + 2 * s1 * r1
|
||||
val c = x1.sq + m.sq - 2 * m * x1 + p.sq + y1.sq - 2 * p * y1 - r1.sq
|
||||
|
||||
val d = b.sq - 4 * a * c
|
||||
val rs = (-b - Math.sqrt(d)) / (2 * a)
|
||||
val xs = m + n * rs
|
||||
val ys = p + q * rs
|
||||
return Circle(xs, ys, rs)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val c1 = Circle(0.0, 0.0, 1.0)
|
||||
val c2 = Circle(4.0, 0.0, 1.0)
|
||||
val c3 = Circle(2.0, 4.0, 2.0)
|
||||
println(solveApollonius(c1, c2, c3, 1, 1, 1))
|
||||
println(solveApollonius(c1, c2, c3,-1,-1,-1))
|
||||
}
|
||||
|
|
@ -1,37 +1,33 @@
|
|||
/*REXX program solves the problem of Apollonius, named after the Greek */
|
||||
/*──────────────── Apollonius of Perga [Pergæus] (circa 262 BC ──► 190 BC).*/
|
||||
w=20; numeric digits w-5 /*the width used to display the numbers*/
|
||||
c1.x=0; c1.y=0; c1.r=1
|
||||
c2.x=4; c2.y=0; c2.r=1
|
||||
c3.x=2; c3.y=4; c3.r=2
|
||||
call tell 'external tangent:', Apollonius( 1, 1, 1)
|
||||
call tell 'internal tangent:', Apollonius(-1, -1, -1)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
Apollonius: parse arg s1,s2,s3 /*could be an internal/external tangent*/
|
||||
numeric digits digits()*3 /*reduce rounding by using 3 times digs*/
|
||||
x1=c1.x; x2=c2.x; x3=c3.x
|
||||
y1=c1.y; y2=c2.y; y3=c3.y
|
||||
r1=c1.r; r2=c2.r; r3=c3.r
|
||||
va=2*x2-2*x1; vb=2*y2-2*y1
|
||||
vc=x1*x1-x2*x2+y1*y1-y2*y2-r1*r1+r2*r2
|
||||
vd=2*s2*r2-2*s1*r1; ve=2*x3-2*x2; vf=2*y3-2*y2
|
||||
vg=x2*x2-x3*x3+y2*y2-y3*y3-r2*r2+r3*r3; vh=2*s3*r3-2*s2*r2
|
||||
vj=vb/va; vk=vc/va; vm=vd/va; vn=vf/ve-vj
|
||||
vp=vg/ve-vk; vr=vh/ve-vm; p=-vp/vn; q =vr/vn
|
||||
m=-vj*p-vk; n=vm-vj*q; a=n*n+q*q-1
|
||||
b=2*m*n-2*n*x1+2*p*q-2*q*y1+2*s1*r1
|
||||
c=x1*x1+m*m-2*m*x1+p*p+y1*y1-2*p*y1-r1*r1
|
||||
_=b*b-4*a*c; $.r=(-b-sqrt(_))/(a+a); $.x=m+n*$.r; $.y=p+q*$.r
|
||||
numeric digits digits()%3 /*reset DIGITS to the original.*/
|
||||
return $.x $.y $.r /*return with 3 args, normalized.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
|
||||
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return (g/1)i /*make complex if X < 0.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: parse arg _,a b c; say _ left(a/1,w) left(b/1,w) left(c/1,w); return
|
||||
/*dividing by 1 normalizes the numbers.*/
|
||||
/*REXX program solves the problem of Apollonius, named after the Greek Apollonius of */
|
||||
/*────────────────────────────────────── Perga [Pergæus] (circa 262 BCE ──► 190 BCE). */
|
||||
numeric digits 15; c1.x=0; c1.y=0; c1.r=1
|
||||
c2.x=4; c2.y=0; c2.r=1
|
||||
c3.x=2; c3.y=4; c3.r=2
|
||||
call tell 'external tangent: ', Apollonius( 1, 1, 1)
|
||||
call tell 'internal tangent: ', Apollonius(-1, -1, -1)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Apollonius: parse arg s1,s2,s3 /*could be internal or external tangent*/
|
||||
d=digits(); numeric digits d*3 /*reduce rounding, use thrice digits. */
|
||||
x1=c1.x; x2=c2.x; x3=c3.x
|
||||
y1=c1.y; y2=c2.y; y3=c3.y
|
||||
r1=c1.r; r2=c2.r; r3=c3.r
|
||||
va=2*x2 - 2*x1; vb=2*y2 - 2*y1
|
||||
vc=x1**2 - x2**2 + y1**2 - y2**2 - r1**2 + r2**2
|
||||
vd=2*s2*r2 - 2*s1*r1; ve=2*x3 - 2*x2; vf=2*y3 - 2*y2
|
||||
vg=x2**2 - x3**2 + y2**2 - y3**2 - r2**2 + r3**2; vh=2*s3*r3 - 2*s2*r2
|
||||
vj=vb/va; vk=vc/va; vm=vd/va; vn=vf/ve - vj
|
||||
vp=vg/ve - vk; vr=vh/ve - vm; p =-vp/vn; q =vr/vn
|
||||
m=-vj*p - vk; n=vm - vj*q; a=n**2 + q**2 - 1
|
||||
b=2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
|
||||
c=x1**2 + m**2 - 2*m*x1 + p**2 + y1**2 - 2*p*y1 - r1**2
|
||||
_=b**2 - 4*a*c; $r=(-b - sqrt(_)) / (a+a); $x=m + n*$r; $y=p + q*$r
|
||||
numeric digits d; return $x $y $r /*return with three arguments. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); h=d+6; numeric digits
|
||||
m.=9; numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
|
||||
do j=0 while h>9; m.j=h; h=h%2 + 1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g) * .5; end /*k*/
|
||||
return g
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: parse arg _,a b c; w=digits()+4; say _ left(a/1,w%2) left(b/1,w) left(c/1,w); return
|
||||
|
|
|
|||
42
Task/Problem-of-Apollonius/Zkl/problem-of-apollonius-1.zkl
Normal file
42
Task/Problem-of-Apollonius/Zkl/problem-of-apollonius-1.zkl
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
class Circle{
|
||||
fcn init(xpos,ypos,radius){
|
||||
var [const] x=xpos.toFloat(), y=ypos.toFloat(),r=radius.toFloat();
|
||||
}
|
||||
fcn toString{ "Circle(%f,%f,%f)".fmt(x,y,r) }
|
||||
fcn apollonius(c2,c3,outside=True){
|
||||
s1:=s2:=s3:=outside and 1 or -1;
|
||||
|
||||
v11:=2.0*(c2.x - x);
|
||||
v12:=2.0*(c2.y - y);
|
||||
v13:=x.pow(2) - c2.x.pow(2) +
|
||||
y.pow(2) - c2.y.pow(2) -
|
||||
r.pow(2) + c2.r.pow(2);
|
||||
v14:=2.0*(s2*c2.r - s1*r);
|
||||
|
||||
v21:=2.0*(c3.x - c2.x);
|
||||
v22:=2.0*(c3.y - c2.y);
|
||||
v23:=c2.x.pow(2) - c3.x.pow(2) +
|
||||
c2.y.pow(2) - c3.y.pow(2) -
|
||||
c2.r.pow(2) + c3.r.pow(2);
|
||||
v24:=2.0*(s3*c3.r - s2*c2.r);
|
||||
|
||||
w12,w13,w14:=v12/v11, v13/v11, v14/v11;
|
||||
w22,w23,w24:=v22/v21 - w12, v23/v21 - w13, v24/v21 - w14;
|
||||
|
||||
P:=-w23/w22;
|
||||
Q:= w24/w22;
|
||||
M:=-w12*P - w13;
|
||||
N:= w14 - w12*Q;
|
||||
|
||||
a:=N*N + Q*Q - 1;
|
||||
b:=2.0*(M*N - N*x + P*Q - Q*y + s1*r);
|
||||
c:=x*x + M*M - 2.0*M*x + P*P + y*y - 2.0*P*y - r*r;
|
||||
|
||||
// find a root of a quadratic equation.
|
||||
// This requires the circle centers not to be e.g. colinear
|
||||
D:=b*b - 4.0*a*c;
|
||||
rs:=(-b - D.sqrt())/(2.0*a);
|
||||
|
||||
Circle(M + N*rs, P + Q*rs, rs);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a,b,c:=Circle(0,0,1), Circle(4,0,1), Circle(2,4,2);
|
||||
a.apollonius(b,c).println(" Outside");
|
||||
a.apollonius(b,c,False).println(" Inside");
|
||||
Loading…
Add table
Add a link
Reference in a new issue