library(PlaneGeometry)
The Exeter point is defined as follows on Wikipedia.
Let \(ABC\) be any given triangle. Let the medians through the vertices \(A\), \(B\), \(C\) meet the circumcircle of triangle \(ABC\) at \(A'\), \(B'\) and \(C'\) respectively. Let \(DEF\) be the triangle formed by the tangents at \(A\), \(B\), and \(C\) to the circumcircle of triangle \(ABC\). (Let \(D\) be the vertex opposite to the side formed by the tangent at the vertex \(A\), let \(E\) be the vertex opposite to the side formed by the tangent at the vertex \(B\), and let \(F\) be the vertex opposite to the side formed by the tangent at the vertex \(C\).) The lines through \(DA'\), \(EB'\) and \(FC'\) are concurrent. The point of concurrence is the Exeter point of triangle \(ABC\).
Let’s construct it with the PlaneGeometry
package. We do not need to construct the triangle \(DEF\): it is the tangential triangle of \(ABC\), and is provided by the tangentialTriangle
method of the R6 class Triangle
.
<- c(0,2); B <- c(5,4); C <- c(5,-1)
A <- Triangle$new(A, B, C)
t <- t$circumcircle()
circumcircle <- t$centroid()
centroid <- Line$new(A, centroid)
medianA <- Line$new(B, centroid)
medianB <- Line$new(C, centroid)
medianC <- intersectionCircleLine(circumcircle, medianA)[[2]]
Aprime <- intersectionCircleLine(circumcircle, medianB)[[2]]
Bprime <- intersectionCircleLine(circumcircle, medianC)[[1]]
Cprime <- t$tangentialTriangle()
DEF <- Line$new(DEF$A, Aprime)
lineDAprime <- Line$new(DEF$B, Bprime)
lineEBprime <- Line$new(DEF$C, Cprime)
lineFCprime <- intersectionLineLine(lineDAprime, lineEBprime) )
( ExeterPoint #> [1] 2.621359 1.158114
# check whether the Exeter point is also on (FC')
$includes(ExeterPoint)
lineFCprime#> [1] TRUE
Let’s draw a figure now.
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-2,9), ylim = c(-6,7),
xlab = NA, ylab = NA, axes = FALSE)
draw(t, lwd = 2, col = "black")
draw(circumcircle, lwd = 2, border = "cyan")
draw(Triangle$new(Aprime,Bprime,Cprime), lwd = 2, col = "green")
draw(DEF, lwd = 2, col = "blue")
draw(Line$new(ExeterPoint, DEF$A, FALSE, FALSE), lwd = 2, col = "red")
draw(Line$new(ExeterPoint, DEF$B, FALSE, FALSE), lwd = 2, col = "red")
draw(Line$new(ExeterPoint, DEF$C, FALSE, FALSE), lwd = 2, col = "red")
points(rbind(ExeterPoint), pch = 19, col = "red")
par(opar)
Let \(\mathcal{C}_1\), \(\mathcal{C}_2\) and \(\mathcal{C}_3\) be three circles with respective radii \(r_1\), \(r_2\) and \(r_3\) such that \(r_3 < r_1\) and \(r_3 < r_2\). How to construct some circles simultaneously tangent to these three circles?
<- Circle$new(c(0,0), 2)
C1 <- Circle$new(c(5,5), 3)
C2 <- Circle$new(c(6,-2), 1)
C3 # inversion swapping C1 and C3 with positive power
<- inversionSwappingTwoCircles(C1, C3, positive = TRUE)
iota1 # inversion swapping C2 and C3 with positive power
<- inversionSwappingTwoCircles(C2, C3, positive = TRUE)
iota2 # take an arbitrary point on C3
<- C3$pointFromAngle(0)
M # invert it with iota1 and iota2
<- iota1$invert(M); M2 <- iota2$invert(M)
M1 # take the circle C passing through M, M1, M2
<- Triangle$new(M,M1,M2)$circumcircle()
C # take the line passing through the two inversion poles
<- Line$new(iota1$pole, iota2$pole)
cl # take the radical axis of C and C3
<- C$radicalAxis(C3)
L # let H bet the intersection of these two lines
<- intersectionLineLine(L, cl)
H # take the circle Cp with diameter [HO3]
<- C3$center
O3 <- CircleAB(H, O3)
Cp # get the two intersection points T0 and T1 of C3 with Cp
<- intersectionCircleCircle(C3, Cp)
T0_and_T1 <- T0_and_T1[[1L]]; T1 <- T0_and_T1[[2L]]
T0 # invert T0 with respect to the two inversions
<- iota1$invert(T0); T0pp <- iota2$invert(T0)
T0p # the circle passing through T0 and its two images is a solution
<- Triangle$new(T0, T0p, T0pp)$circumcircle()
Csolution0 # invert T1 with respect to the two inversions
<- iota1$invert(T1); T1pp <- iota2$invert(T1)
T1p # the circle passing through T1 and its two images is another solution
<- Triangle$new(T1, T1p, T1pp)$circumcircle() Csolution1
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-4,9), ylim = c(-4,9),
xlab = NA, ylab = NA, axes = FALSE)
draw(C1, col = "yellow", border = "red")
draw(C2, col = "yellow", border = "red")
draw(C3, col = "yellow", border = "red")
draw(Csolution0, lwd = 2, border = "blue")
draw(Csolution1, lwd = 2, border = "blue")
par(opar)
There are several circles called “Apollonius circle”. We take the one defined as follows, with respect to a reference triangle: the circle which touches all three excircles of the reference triangle and encompasses them.
It can be constructed as the inversive image of the nine-point circle with respect to the circle orthogonal to the excircles of the reference triangle. This inversion can be obtained in PlaneGeometry
with the function inversionFixingThreeCircles
.
# reference triangle
<- Triangle$new(c(0,0), c(5,3), c(3,-1))
t # nine-point circle
<- t$orthicTriangle()$circumcircle()
npc # excircles
<- t$excircles()
excircles # inversion with respect to the circle orthogonal to the excircles
<- inversionFixingThreeCircles(excircles$A, excircles$B, excircles$C)
iota # Apollonius circle
<- iota$invertCircle(npc) ApolloniusCircle
Let’s do a figure:
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-10,14), ylim = c(-5, 18),
xlab = NA, ylab = NA, axes = FALSE)
draw(t, lwd = 2)
draw(excircles$A, lwd = 2, border = "blue")
draw(excircles$B, lwd = 2, border = "blue")
draw(excircles$C, lwd = 2, border = "blue")
draw(ApolloniusCircle, lwd = 2, border = "red")
par(opar)
The radius of the Apollonius circle is \(\frac{r^2+s^2}{4r}\) where \(r\) is the inradius of the triangle and \(s\) its semiperimeter. Let’s check this point:
<- t$inradius()
inradius <- sum(t$edges()) / 2
semiperimeter ^2 + semiperimeter^2) / (4*inradius)
(inradius#> [1] 11.15942
$radius
ApolloniusCircle#> [1] 11.15942
Let two circles intersecting at two points. How to fill the lapping area of the two circles?
<- c(2,5); circ1 <- Circle$new(O1, 2)
O1 <- c(4,4); circ2 <- Circle$new(O2, 3)
O2
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(0,8), ylim = c(0,8), xlab = NA, ylab = NA)
draw(circ1, border = "purple", lwd = 2)
draw(circ2, border = "forestgreen", lwd = 2)
<- intersectionCircleCircle(circ1, circ2)
intersections <- intersections[[1]]; B <- intersections[[2]]
A points(rbind(A,B), pch = 19, col = c("red", "blue"))
<- Arg((A-O1)[1] + 1i*(A-O1)[2])
theta1 <- Arg((B-O1)[1] + 1i*(B-O1)[2])
theta2 <- Arc$new(O1, circ1$radius, theta1, theta2, FALSE)$path()
path1
<- Arg((A-O2)[1] + 1i*(A-O2)[2])
theta1 <- Arg((B-O2)[1] + 1i*(B-O2)[2])
theta2 <- Arc$new(O2, circ2$radius, theta2, theta1, FALSE)$path()
path2
polypath(rbind(path1,path2), col = "yellow")
par(opar)
In the help page of the Circle
R6 class (?Circle
), we show how to draw a hyperbolic triangle with the help of the method orthogonalThroughTwoPointsOnCircle()
. Here we will use this method to draw a hyperbolic tessellation.
<- function(depth, Thetas0, colors){
tessellation stopifnot(
>= 3,
depth is.numeric(Thetas0),
length(Thetas0) == 3L,
is.character(colors),
length(colors) >= depth
)
<- Circle$new(c(0,0), 3)
circ
<- lapply(seq_along(Thetas0), function(i){
arcs <- ifelse(i == length(Thetas0), 1L, i+1L)
ip1 $orthogonalThroughTwoPointsOnCircle(Thetas0[i], Thetas0[ip1],
circarc = TRUE)
})<- lapply(arcs, function(arc){
inversions $new(arc$center, arc$radius^2)
Inversion
})
<- vector("list", depth)
Ms
<- lapply(Thetas0, function(theta) c(cos(theta), sin(theta)))
Ms[[1L]]
<- vector("list", 3L)
Ms[[2L]] for(i in 1L:3L){
<- ifelse(i == 1L, 3L, i-1L)
im1 <- inversions[[i]]$invert(Ms[[1L]][[im1]])
M attr(M, "iota") <- i
<- M
Ms[[2L]][[i]]
}
for(d in 3L:depth){
<- length(Ms[[d-1L]])
n1 <- 2L*n1
n2 <- vector("list", n2)
Ms[[d]] <- 0L
k while(k < n2){
for(j in 1L:n1){
<- Ms[[d-1L]][[j]]
M for(i in 1L:3L){
if(i != attr(M, "iota")){
<- k + 1L
k <- inversions[[i]]$invert(M)
newM attr(newM, "iota") <- i
<- newM
Ms[[d]][[k]]
}
}
}
}
}
# plot ####
<- par(mar = c(0,0,0,0), bg = "black")
opar plot(NULL, asp = 1, xlim = c(-4,4), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
draw(circ, border = "white")
invisible(lapply(arcs, draw, col = colors[1L], lwd = 2))
<- lapply(
Thetas rapply(Ms, function(M){
Arg(M[1L] + 1i*M[2L])
how="replace"),
},
unlist)
for(d in 2L:depth){
<- sort(unlist(Thetas[1L:d]))
thetas for(i in 1L:length(thetas)){
<- ifelse(i == length(thetas), 1L, i+1L)
ip1 <- circ$orthogonalThroughTwoPointsOnCircle(thetas[i], thetas[ip1],
arc arc = TRUE)
draw(arc, lwd = 2, col = colors[d])
}
}
par(opar)
invisible()
}
tessellation(
depth = 5L,
Thetas0 = c(0, 2, 3.8),
colors = viridisLite::viridis(5)
)
Here is a version which allows to fill the hyperbolic triangles:
<- function(depth, Thetas0, colors){
tessellation2 stopifnot(
>= 3,
depth is.numeric(Thetas0),
length(Thetas0) == 3L,
is.character(colors),
length(colors)-1L >= depth
)
<- Circle$new(c(0,0), 3)
circ
<- lapply(seq_along(Thetas0), function(i){
arcs <- ifelse(i == length(Thetas0), 1L, i+1L)
ip1 $orthogonalThroughTwoPointsOnCircle(Thetas0[i], Thetas0[ip1],
circarc = TRUE)
})<- lapply(arcs, function(arc){
inversions $new(arc$center, arc$radius^2)
Inversion
})
<- vector("list", depth)
Ms
<- lapply(Thetas0, function(theta) c(cos(theta), sin(theta)))
Ms[[1L]]
<- vector("list", 3L)
Ms[[2L]] for(i in 1L:3L){
<- ifelse(i == 1L, 3L, i-1L)
im1 <- inversions[[i]]$invert(Ms[[1L]][[im1]])
M attr(M, "iota") <- i
<- M
Ms[[2L]][[i]]
}
for(d in 3L:depth){
<- length(Ms[[d-1L]])
n1 <- 2L*n1
n2 <- vector("list", n2)
Ms[[d]] <- 0L
k while(k < n2){
for(j in 1L:n1){
<- Ms[[d-1L]][[j]]
M for(i in 1L:3L){
if(i != attr(M, "iota")){
<- k + 1L
k <- inversions[[i]]$invert(M)
newM attr(newM, "iota") <- i
<- newM
Ms[[d]][[k]]
}
}
}
}
}
# plot ####
<- par(mar = c(0,0,0,0), bg = "black")
opar plot(NULL, asp = 1, xlim = c(-4,4), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
<- do.call(rbind, lapply(rev(arcs), function(arc) arc$path()))
path polypath(path, col = colors[1L])
invisible(lapply(arcs, function(arc){
<- arc$path()
path1 <- arc$startingPoint()
B <- arc$endingPoint()
A <- Arg(A[1L] + 1i*A[2L])
alpha1 <- Arg(B[1L] + 1i*B[2L])
alpha2 <- Arc$new(c(0,0), 3, alpha1, alpha2, FALSE)$path()
path2 polypath(rbind(path1,path2), col = colors[2L])
}))
<- lapply(
Thetas rapply(Ms, function(M){
Arg(M[1L] + 1i*M[2L])
how="replace"),
},
unlist)
for(d in 2L:depth){
<- sort(unlist(Thetas[1L:d]))
thetas for(i in 1L:length(thetas)){
<- ifelse(i == length(thetas), 1L, i+1L)
ip1 <- circ$orthogonalThroughTwoPointsOnCircle(thetas[i], thetas[ip1],
arc arc = TRUE)
<- arc$path()
path1 <- arc$startingPoint()
B <- arc$endingPoint()
A <- Arg(A[1L] + 1i*A[2L])
alpha1 <- Arg(B[1L] + 1i*B[2L])
alpha2 <- Arc$new(c(0,0), 3, alpha1, alpha2, FALSE)$path()
path2 polypath(rbind(path1,path2), col = colors[d+1L])
}
}
draw(circ, border = "white")
par(opar)
invisible()
}
tessellation2(
depth = 5L,
Thetas0 = c(0, 2, 3.8),
colors = viridisLite::viridis(6)
)
Let’s draw the director circle of an ellipse. We start by constructing the minimum bounding box of the ellipse.
<- Ellipse$new(c(1,1), 5, 2, 30)
ell <- ell$diameter(0)
majorAxis <- ell$diameter(pi/2)
minorAxis <- (majorAxis$B - majorAxis$A) / 2
v1 <- (minorAxis$B - minorAxis$A) / 2
v2 # sides of the minimum bounding box
<- majorAxis$translate(v2)
side1 <- majorAxis$translate(-v2)
side2 <- minorAxis$translate(v1)
side3 <- minorAxis$translate(-v1)
side4 # take a vertex of the bounding box
<- side1$A
A # director circle
<- CircleOA(ell$center, A) circ
Now let’s take a tangent \(T_1\) to the ellipse, construct the half-line directed by \(T_1\) with origin the point of tangency, determine the intersection point of this half-line with the director circle, and draw the perpendicular \(T_2\) of \(T_1\) passing by this intersection point. Then \(T_2\) is another tangent to the ellipse.
<- ell$tangent(0.3)
T1 <- T1$clone(deep = TRUE)
halfT1 $extendA <- FALSE
halfT1<- intersectionCircleLine(circ, halfT1, strict = TRUE)
I <- T1$perpendicular(I) T2
<- par(mar=c(0,0,0,0))
opar plot(NULL, asp = 1,
xlim = c(-3,6), ylim = c(-5,7), xlab = NA, ylab = NA)
# draw the ellipse
draw(ell, col = "blue")
# draw the bounding box
draw(side1, lwd = 2, col = "green")
draw(side2, lwd = 2, col = "green")
draw(side3, lwd = 2, col = "green")
draw(side4, lwd = 2, col = "green")
# draw the director circle
draw(circ, lwd = 2, border = "red")
# draw the two tangents
draw(T1); draw(T2)
# restore the graphical parameters
par(opar)
The PlaneGeometry
package has a function SteinerChain
which generates a Steiner chain of circles.
By applying an affine transformation to a Steiner chain, we can get an elliptical Steiner chain.
<- Circle$new(c(3,0), 3) # exterior circle
c0 <- SteinerChain(c0, 3, -0.2, 0.5)
circles # take an ellipse
<- Ellipse$new(c(-4,0), 4, 2.5, 140)
ell # take the affine transformation which maps the exterior circle to this ellipse
<- AffineMappingEllipse2Ellipse(c0, ell)
f # take the images of the Steiner circles by this transformation
<- lapply(circles, f$transformEllipse) ellipses
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-8,6), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
# draw the Steiner chain
invisible(lapply(circles, draw, lwd = 2, col = "blue"))
draw(c0, lwd = 2)
# draw the elliptical Steiner chain
invisible(lapply(ellipses, draw, lwd = 2, col = "red", border = "forestgreen"))
draw(ell, lwd = 2, border = "forestgreen")
par(opar)
Here is how I got the animation below, by varying the shift
parameter of the Steiner chain.
library(gifski)
<- Circle$new(c(3,0), 3)
c0 <- Ellipse$new(c(-4,0), 4, 2.5, 140)
ell <- AffineMappingEllipse2Ellipse(c0, ell)
f
<- function(shift){
fplot <- SteinerChain(c0, 3, -0.2, shift)
circles <- lapply(circles, f$transformEllipse)
ellipses <- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-8,0), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
invisible(lapply(ellipses, draw, lwd = 2, col = "blue", border = "black"))
draw(ell, lwd = 2)
par(opar)
invisible()
}
<- seq(0, 3, length.out = 100)[-1L]
shift_
save_gif(
for(shift in shift_){
fplot(shift)
},"SteinerChainElliptical.gif",
512, 512, 1/12, res = 144
)
We can choose the exterior circle of the Steiner chain. Therefore, given a circle of a Steiner chain, we can nest another Steiner chain in this circle.
<- Circle$new(c(3,0), 3) # exterior circle
c0 <- SteinerChain(c0, 3, -0.2, 0.5)
circles # Steiner chain for each circle, except the small one (it is too small)
<- lapply(circles[1:3], function(c0){
chains SteinerChain(c0, 3, -0.2, 0.5)
})
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(0,6), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
# draw the big Steiner chain
invisible(lapply(circles, draw, lwd = 2, border = "blue"))
draw(c0, lwd = 2)
# draw the nested Steiner chain
invisible(lapply(chains, function(circles){
lapply(circles, draw, lwd = 2, border = "red")
}))
par(opar)
Of course you can also nest elliptical Steiner chains, and animate the picture!
The following code plots the trajectory of a ball on an elliptical billiard.
<- function(incidentDir, normalVec){
reflect - 2*c(crossprod(normalVec, incidentDir)) * normalVec
incidentDir
}
# n: number of segments; P0: initial point; v0: initial direction
<- function(n, P0, v0){
trajectory <- vector("list", n)
out <- Line$new(P0, P0+v0)
L <- intersectionEllipseLine(ell, L)
inters <- inters$I2
Q0 1]] <- Line$new(inters$I1, inters$I2, FALSE, FALSE)
out[[for(i in 2:n){
<- atan2(Q0[2], Q0[1])
theta <- ell$theta2t(theta, degrees = FALSE)
t <- ell$normal(t)
nrmlVec <- reflect(Q0-P0, nrmlVec)
v <- intersectionEllipseLine(ell, Line$new(Q0, Q0+v))
inters <- Line$new(inters$I1, inters$I2, FALSE, FALSE)
out[[i]] <- Q0
P0 <- if(isTRUE(all.equal(Q0, inters$I1))) inters$I2 else inters$I1
Q0
}
out
}
<- Ellipse$new(c(0,0), 6, 3, 0)
ell
<- ell$pointFromAngle(60)
P0 <- c(cos(pi+0.8), sin(pi+0.8))
v0 <- trajectory(150, P0, v0)
traj
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-7,7), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
draw(ell, border = "red", col = "springgreen", lwd = 3)
invisible(lapply(traj, draw))
par(opar)
Run the code below to see an animated trajectory:
<- par(mar = c(0,0,0,0))
opar plot(NULL, asp = 1, xlim = c(-7,7), ylim = c(-4,4),
xlab = NA, ylab = NA, axes = FALSE)
draw(ell, border = "red", col = "springgreen", lwd = 3)
for(i in 1:length(traj)){
draw(traj[[i]])
Sys.sleep(0.3)
}par(opar)
A generalized circle is either a circle or a line. The following code generates a family of generalized circles by repeatedly applying inversions:
# generation 0
<- c(0, pi/2, pi, 3*pi/2)
angles <- Circle$new(center = c(0, 0), radius = 2)
bigCircle attr(bigCircle, "gen") <- 0L
attr(bigCircle, "base") <- length(angles) + 1L
<- c(
gen0 lapply(seq_along(angles), function(i){
<- angles[i]
beta <- Circle$new(center = c(cos(beta), sin(beta)), radius = 1)
circle attr(circle, "gen") <- 0L
attr(circle, "base") <- i
circle
}),list(
bigCircle
)
)<- length(gen0)
n0
# generations 1, 2, 3
<- vector("list", length = 4L)
generations <- gen0
generations[[1L]] for(g in 2L:4L){
<- generations[[g-1L]]
gen <- length(gen)
n <- n*(n0 - 1L)
n1 <- vector("list", length = n1)
gen_new <- 0L
k while(k < n1){
for(j in 1L:n){
<- gen[[j]]
gcircle_j <- attr(gcircle_j, "base")
base for(i in 1L:n0){
if(i != base){
<- k + 1L
k <- gen0[[i]]
circ <- Inversion$new(pole = circ$center, power = circ$radius^2)
iota <- iota$invertGcircle(gcircle_j)
gcircle attr(gcircle, "gen") <- g - 1L
attr(gcircle, "base") <- i
<- gcircle
gen_new[[k]]
}
}
}
}<- gen_new
generations[[g]]
}
<- c(
gcircles
generations[[1L]], generations[[2L]], generations[[3L]], generations[[4L]] )
There are 425 generalized circles:
length(gcircles)
#> [1] 425
But some of them are duplicated. In order to remove the duplicates, I will use the following function uniqueWith
, which takes as arguments a list or a vector and a function representing a binary relation between the elements of this collection:
<- function(v, f){
uniqueWith <- length(v)
size for(i in seq_len(size-1L)){
<- i + 1L
j while(j <= size){
if(f(v[[i]], v[[j]])){
<- v[-j]
v <- size - 1L
size else{
}<- j + 1L
j
}
}
}:size]
v[1L }
For example:
uniqueWith(
c(a = "you", b = "are", c = "great"),
function(x, y) nchar(x) == nchar(y)
)#> a c
#> "you" "great"
So we can remove the duplicated generalized circles as follows:
<- uniqueWith(
gcircles
gcircles,function(g1, g2){
class(g1)[1L] == class(g2)[1L] && g1$isEqual(g2)
} )
Now it remains 161 generalized circles:
length(gcircles)
#> [1] 158
Let’s write a helper function to draw these generalized circles:
<- function(gcircle, colors = rainbow(4L), ...){
drawGcircle <- attr(gcircle, "gen")
gen if(is(gcircle, "Circle")){
draw(gcircle, border = colors[1L + gen], ...)
else{
}draw(gcircle, col = colors[1L + gen], ...)
} }
And now let’s draw them:
<- par(mar = c(0,0,0,0), bg = "black")
opar plot(0, 0, type = "n", xlim = c(-2.3, 2.3), ylim = c(-2.3, 2.3),
asp = 1, axes = FALSE, xlab = NA, ylab = NA)
invisible(lapply(gcircles, drawGcircle, lwd = 2))
par(opar)
This construction is taken from the book Indra’s Pearls: The Vision of Felix Klein.
library(freegroup)
<- alpha(1)
a <- inverse(a)
A <- alpha(2)
b <- inverse(b)
B
# words of size n
<- 6L
n <- do.call(expand.grid, rep(list(c("a", "A", "b", "B")), n))
G <- split(as.matrix(G), 1:nrow(G))
G <- lapply(G, function(w){
G sum(do.call(c.free, lapply(w, function(x) switch(x, a=a, A=A, b=b, B=B))))
})<- uniqueWith(G, free_equal)
G <- vapply(G, total, numeric(1L))
sizes <- G[sizes == n]
Gn
# starting circles ####
<- Line$new(c(-1,0), c(1,0))
Ca <- sqrt(2)/4
Rc <- -3*sqrt(2)/4
yI <- Circle$new(c(0,yI), Rc)
CA <- -0.5
theta <- c(Rc*cos(theta), yI+Rc*sin(theta))
T <- c(T[1]+T[2]*tan(theta), 0)
P <- sqrt(c(crossprod(T-P)))
PT <- P[1]+PT
xTprime <- -yI/tan(theta)
xPprime <- abs(xTprime-xPprime)
PprimeTprime <- abs(yI*PprimeTprime/xPprime)
Rcprime <- Circle$new(c(xTprime, -Rcprime), Rcprime)
Cb <- Circle$new(c(-xTprime, -Rcprime), Rcprime)
CB <- list(a = Ca, A = CA, b = Cb, B = CB)
GCIRCLES
# Mobius transformations ####
<- Mobius$new(rbind(c(sqrt(2), 1i), c(-1i, sqrt(2))))
Mob_a <- Mob_a$inverse()
Mob_A
<- function(xy) complex(real = xy[1], imaginary = xy[2])
toCplx <- Mobius$new(rbind(
Mob_b c(toCplx(Cb$center), c(crossprod(Cb$center))-Cb$radius^2),
c(1, -toCplx(CB$center))
))<- Mob_b$inverse()
Mob_B
<- list(a = Mob_a, A = Mob_A, b = Mob_b, B = Mob_B)
MOBS
# Conversion word of size n to circle
<- function(g){
word2seq <- c()
seq <- reduce(g)[[1L]]
gr for(j in 1L:ncol(gr)){
<- gr[, j]
monomial <- c("a", "b")[monomial[1L]]
t <- monomial[2L]
i if(i < 0L){
<- -i
i <- toupper(t)
t
}<- c(seq, rep(t, i))
seq
}
seq
}<- function(g){
word2circle <- word2seq(g)
seq <- MOBS[seq]
mobs <- Reduce(function(M1, M2) M1$compose(M2), mobs[-n])
mobius $transformGcircle(GCIRCLES[[seq[n]]])
mobius }
Here is the picture:
<- par(mar = c(0,0,0,0), bg = "black")
opar plot(NULL, asp = 1, xlim = c(-3,3), ylim = c(-3,3),
axes = FALSE, xlab = NA, ylab = NA)
draw(Ca); draw(CA); draw(Cb); draw(CB)
<- Mob_A$transformCircle(CA)
C1 <- Mob_A$transformCircle(CB)
C2 <- Mob_A$transformCircle(Cb)
C3 draw(C1, lwd = 2, border = "red")
draw(C2, lwd = 2, border = "red")
draw(C3, lwd = 2, border = "red")
<- Mob_a$transformLine(Ca)
C1 <- Mob_a$transformCircle(Cb)
C2 <- Mob_a$transformCircle(CB)
C3 draw(C1, lwd = 2, border = "green")
draw(C2, lwd = 2, border = "green")
draw(C3, lwd = 2, border = "green")
<- Mob_b$transformLine(Ca)
C1 <- Mob_b$transformCircle(CA)
C2 <- Mob_b$transformCircle(Cb)
C3 draw(C1, lwd = 2, border = "blue")
draw(C2, lwd = 2, border = "blue")
draw(C3, lwd = 2, border = "blue")
<- Mob_B$transformLine(Ca)
C1 <- Mob_B$transformCircle(CA)
C2 <- Mob_B$transformCircle(CB)
C3 draw(C1, lwd = 2, border = "yellow")
draw(C2, lwd = 2, border = "yellow")
draw(C3, lwd = 2, border = "yellow")
for(g in Gn){
<- word2circle(g)
circ draw(circ, lwd = 2, border = "orange")
}
par(opar)
Here is the same picture but with better quality:
I realized this picture with the tikzDevice package.
I did this animation after I came across the paper Complex Variables Visualized written by Thomas Ponweiser. This is the paper which motivated me to implement the generalized power of a Möbius transformation.
library(elliptic) # for the unimodular matrices
# Möbius transformations
<- Mobius$new(rbind(c(0,-1), c(1,0)))
T <- Mobius$new(rbind(c(1,1), c(0,1)))
U <- U$compose(T)
R # R**t, generalized power
<- function(t){
Rt $gpower(t)
R
}
# starting circles
<- Circle$new(c(0, 1.5), 0.5)
I <- T$transformCircle(I)
TI
# modified Cayley transformation
<- Mobius$new(rbind(c(1i, 1), c(1, 1i)))
Phi
<- function(M, u, compose = FALSE){
draw_pair if(compose) M <- M$compose(T)
<- M$compose(Rt(u))$compose(Phi)
A <- A$transformCircle(I)
C draw(C, col = "magenta")
<- A$transformCircle(TI)
C draw(C, col = "magenta")
if(!compose){
draw_pair(M, u, compose=TRUE)
}
}
<- 8L
n <- unimodular(n)
transfos
<- function(u){
fplot <- par(mar = c(0,0,0,0), bg = "black")
opar plot(NULL, asp = 1, xlim = c(-1.1, 1.1), ylim = c(-1.1, 1.1),
axes = FALSE, xlab = NA, ylab = NA)
for(i in 1L:dim(transfos)[3L]){
<- transfos[, , i]
transfo <- Mobius$new(transfo)
M draw_pair(M, u)
<- M$inverse()
M draw_pair(M, u)
diag(transfo) <- -diag(transfo)
<- Mobius$new(transfo)
M draw_pair(M, u)
<- M$inverse()
M draw_pair(M, u)
<- diag(transfo)
d if(d[1L] != d[2L]){
diag(transfo) <- rev(diag(transfo))
<- Mobius$new(transfo)
M draw_pair(M, u)
<- M$inverse()
M draw_pair(M, u)
}
} }
To get the animation, run:
library(gifski)
<- seq(0, 3, length.out = 181)[-1]
u_ save_gif(
for(u in u_){
fplot(u)
},width = 512,
height = 512,
delay = 0.1
)
It is not hard to draw an Apollonian gasket with PlaneGeometry
. We do a function, in order to use it later to do an animation.
# function to construct the "children" ####
<- function(inversions, circles1){
ApollonianChildren <- length(inversions)
m <- length(circles1)
n <- list()
circles2 for(i in 1:n){
<- circles1[[i]]
circ <- attr(circ, "inversion")
k for(j in 1:m){
if(j != k){
<- inversions[[j]]$invertCircle(circ)
circle attr(circle, "inversion") <- j
<- append(circles2, circle)
circles2
}
}
}
circles2
}
<- function(c0, n, phi, shift, depth){
ApollonianGasket <- SteinerChain(c0, n, phi, shift)
circles0 # construct the inversions ####
<- vector("list", n + 1)
inversions for(i in 1:n){
<- inversionFixingThreeCircles(
inversions[[i]] %% n) + 1]]
c0, circles0[[i]], circles0[[(i
)
}+1]] <- inversionSwappingTwoCircles(c0, circles0[[n+1]])
inversions[[n# first generation of children
<- list()
circles1 for(i in 1:n){
<- (i %% n) + 1
ip1 for(j in 1:(n+1)){
if(j != i && j != ip1){
<- inversions[[i]]$invertCircle(circles0[[j]])
circle attr(circle, "inversion") <- i
<- append(circles1, circle)
circles1
}
}
}# construct children ####
<- vector("list", depth)
allCircles 1]] <- circles0
allCircles[[2]] <- circles1
allCircles[[for(i in 3:depth){
<- ApollonianChildren(inversions, allCircles[[i-1]])
allCircles[[i]]
}
allCircles }
Let’s apply our function:
library(viridisLite) # for the colors
<- Circle$new(c(0,0), 3) # the exterior circle
c0 <- 5
depth <- plasma(depth)
colors <- ApollonianGasket(c0, n = 3, phi = 0.3, shift = 0, depth)
ApollonianCircles # plot ####
<- c0$center
center0 <- c0$radius
radius0 <- center0[1] + c(-radius0 - 0.1, radius0 + 0.1)
xlim <- center0[2] + c(-radius0 - 0.1, radius0 + 0.1)
ylim <- par(mar = c(0, 0, 0, 0))
opar plot(NULL, type = "n", xlim = xlim, ylim = ylim,
xlab = NA, ylab = NA, axes = FALSE, asp = 1)
draw(c0, border = "black", lwd = 2)
for(i in 1:depth){
for(circ in ApollonianCircles[[i]]){
draw(circ, col = colors[i])
} }
par(opar)
We can do an animation now:
<- function(shift){
fplot <- ApollonianGasket(c0, n = 3, phi = 0.3, shift = shift, depth)
gasket par(mar = c(0, 0, 0, 0))
plot(NULL, type = "n", xlim = xlim, ylim = ylim,
xlab = NA, ylab = NA, axes = FALSE, asp = 1)
draw(c0, border = "black", lwd = 2)
for(i in 1:depth){
for(circ in gasket[[i]]){
draw(circ, col = colors[i])
}
}
}
<- function(){
fanim <- seq(0, 3, length.out = 101)[-101]
shifts for(shift in shifts){
fplot(shift)
}
}
library(gifski)
save_gif(
fanim(),
"ApollonianGasket.gif",
width = 512, height = 512,
delay = 0.1
)
We can also animate the Apollonian gasket with the help of a Möbius transformation. Consider the following complex matrix:
\[ M = \begin{pmatrix} i & \gamma \\ \bar\gamma & -i \end{pmatrix} \] with \(|\gamma| < 1\).
The Möbius transformation associated to \(M\) maps the unit disk to the unit disk and it is of order \(2\). Its powers map the unit disk to the unit dis as well. By the way, after some calculus, one can give the expression of \(M^t\). We find
<- function(gamma, t){
Mt <- sqrt(1 - Mod(gamma)^2)
h <- h^t * (cos(t*pi/2) + 1i*sin(t*pi/2))
d2 <- Conj(d2)
d1 <- Re(d1) - 1i*Im(d1)/h
A11 <- Im(d2) * gamma / h
A12 rbind(
c(A11, A12),
c(Conj(A11), Conj(A12))
) }
Now let’s do the animation.
<- Circle$new(c(0,0), 1) # the exterior circle
c0 <- 5
depth <- ApollonianGasket(c0, n = 3, phi = 0.1, shift = 0.5, depth)
ApollonianCircles <- c(-1.1, 1.1)
xlim <- c(-1.1, 1.1)
ylim <- par(mar = c(0, 0, 0, 0))
opar <- function(gamma, t){
fplot plot(NULL, type = "n", xlim = xlim, ylim = ylim,
xlab = NA, ylab = NA, axes = FALSE, asp = 1)
draw(c0, border = "black", lwd = 2)
<- Mt(gamma, t)
Mob for(i in 1:depth){
for(circ in ApollonianCircles[[i]]){
draw(Mob$transformCircle(circ), col = colors[i])
}
}
}<- function(){
fanim <- 0.5 + 0.4i
gamma <- seq(0, 2, length.out = 91)[-91]
t_ for(t in t_){
fplot(gamma, t)
} }
library(gifski)
save_gif(
fanim(),
"ApollonianMobius.gif",
width = 512, height = 512,
delay = 0.1
)par(opar)
Let’s do another Apollonian fractal which uses the inner Soddy circle. As you can see, the code is short:
<- function(c1, c2, c3, n){
apollony <- soddyCircle(c1, c2, c3)
soddycircle if(n == 1){
soddycircleelse{
}c(
apollony(c1, c2, soddycircle, n-1),
apollony(c1, soddycircle, c3, n-1),
apollony(soddycircle, c2, c3, n-1)
)
}
}
<- function(n){
fractal = Circle$new(c(1, -1/sqrt(3)), 1)
c1 = Circle$new(c(-1, -1/sqrt(3)), 1)
c2 = Circle$new(c(0, sqrt(3) - 1/sqrt(3)), 1)
c3 do.call(c, lapply(1:n, function(i) apollony(c1, c2, c3, i)))
}
<- fractal(4) circs
Let’s plot the fractal in 3D with the help of the rgl package.
library(rgl)
# the spheres in rgl, obtained with the `spheres3d` function, are not smooth;
# the way we use below provides pretty spheres
<- subdivision3d(icosahedron3d(), depth = 4)
unitSphere $vb[4, ] <-
unitSphereapply(unitSphere$vb[1:3, ], 2, function(x) sqrt(sum(x * x)))
$normals <- unitSphere$vb
unitSphere<- function(circle, ...){
drawSphere <- circle$center
center <- circle$radius
radius <- scale3d(unitSphere, radius, radius, radius)
sphere shade3d(translate3d(sphere, center[1], center[2], 0), ...)
}
Now here is how to plot the fractal and make an animation:
# plot ####
open3d(windowRect = c(50, 50, 562, 562))
bg3d(color = "#363940")
view3d(35, 60, zoom = 0.95)
for(circ in circs){
drawSphere(circ, color = "darkred")
}# animation ####
movie3d(
spin3d(axis = c(0, 0, 1), rpm = 15),
duration = 4, fps = 15,
movie = "Apollony", dir = ".",
convert = "magick convert -dispose previous -loop 0 -delay 1x%d %s*.png %s.%s",
startTime = 1/60
)
Now we will do something a bit more complicated. We will take a triangle, fill each of its three Malfatti circles with an Apollonian gasket, and fill the rest of the triangle with tangent circles. In fact, tangent spheres: we will draw the result in 3D, this will be more pretty.
<- function(M){
toCplx 1] + 1i * M[2]
M[
}<- function(z){
fromCplx c(Re(z), Im(z))
}
<- function(A, B){
distance sqrt(c(crossprod(B - A)))
}
<- function(r1, r2, r3){
innerSoddyRadius 1 / (1/r1 + 1/r2 + 1/r3 + 2 * sqrt(1/r1/r2 + 1/r2/r3 + 1/r3/r1))
}
<- function(c1, c2, c3, ...){
innerSoddyCircle <- innerSoddyRadius(c1$radius, c3$radius, c3$radius)
radius <- Triangle$new(c1$center, c2$center, c3$center)$equalDetourPoint()
center <- Circle$new(center, radius)
c123 drawSphere(c123, ...)
list(
list(type = "ccc", c1 = c123, c2 = c1, c3 = c2),
list(type = "ccc", c1 = c123, c2 = c2, c3 = c3),
list(type = "ccc", c1 = c123, c2 = c1, c3 = c3)
)
}
<- function(A, B, cA, cB, ...){
side.circle.circle if(A[2] > B[2]){
return(side.circle.circle(B, A, cB, cA, ...))
}<- cA$radius
rA <- cB$radius
rB <- toCplx(cA$center)
zoA <- toCplx(cB$center)
zoB <- toCplx(A)
zB <- acos((B[1] - A[1]) / distance(A, B))
alpha <- exp(-1i * alpha) * (zoA - zB)
zX1 <- exp(-1i * alpha) * (zoB - zB)
zX2 <- innerSoddyRadius(rA, rB, Inf)
soddyR if(Re(zX1) < Re(zX2)){
<- (2 * rA * sqrt(rB) / (sqrt(rA) + sqrt(rB)) + Re(zX1)) +
Y sign(Im(zX1)) * 1i * soddyR
else{
}<- (2 * rB * sqrt(rA) / (sqrt(rA) + sqrt(rB)) + Re(zX2)) +
Y sign(Im(zX1)) * 1i * soddyR
}<- fromCplx(Y * exp(1i * alpha) + zB)
M <- Circle$new(M, soddyR)
cAB drawSphere(cAB, ...)
list(
list(type = "ccc", c1 = cAB, c2 = cA, c3 = cB),
list(type = "ccl", cA = cA, cB = cAB, A = A, B = B),
list(type = "ccl", cA = cAB, cB = cB, A = A, B = B)
)
}
<- function(A, B, C, circle, ...){
side.side.circle <- toCplx(A)
zA <- toCplx(circle$center)
zO <- zA - zO
vec <- fromCplx(zO + circle$radius * vec / Mod(vec))
P <- P - circle$center
OP <- P + c(-OP[2], OP[1])
onTangent <- Line$new(P, onTangent)
L1 <- intersectionLineLine(L1, Line$new(A, C))
P1 <- intersectionLineLine(L1, Line$new(A, B))
P2 <- Triangle$new(A, P1, P2)$incircle()
incircle drawSphere(incircle, ...)
list(
list(type = "cll", A = A, B = B, C = C, circle = incircle),
list(type = "ccl", cA = circle, cB = incircle, A = A, B = B),
list(type = "ccl", cA = circle, cB = incircle, A = A, B = C)
)
}
<- function(holes, color){
Newholes <- list()
newholes for(i in 1:3){
<- holes[[i]]
hole <- hole[["type"]]
holeType if(holeType == "ccc"){
<- with(hole, innerSoddyCircle(c1, c2, c3, color = color))
x else if(holeType == "ccl"){
}<- with(hole, side.circle.circle(A, B, cA, cB, color = color))
x else if (holeType == "cll"){
} <- with(hole, side.side.circle(A, B, C, circle, color = color))
x
}<- c(newholes, list(x))
newholes
}
newholes
}
<- function(A, B, C){
MalfattiCircles $new(A, B, C)$MalfattiCircles()
Triangle
}
<- function(mcircles, A, B, C, colors, depth){
drawTriangularGasket <- mcircles[[1]]
C1 <- mcircles[[2]]
C2 <- mcircles[[3]]
C3 triangles3d(cbind(rbind(A, B, C), 0), col = "yellow", alpha = 0.2)
<- list(
holes side.circle.circle(A, B, C1, C2, color = colors[1]),
side.circle.circle(B, C, C2, C3, color = colors[1]),
side.circle.circle(C, A, C3, C1, color = colors[1]),
innerSoddyCircle(C1, C2, C3, color = colors[1]),
side.side.circle(A, B, C, C1, color = colors[1]),
side.side.circle(B, A, C, C2, color = colors[1]),
side.side.circle(C, A, B, C3, color = colors[1])
)for(d in 1:depth){
<- length(holes)
n_holes <- list()
Holes for(i in 1:n_holes){
<- append(Holes, Newholes(holes[[i]], colors[d + 1]))
Holes
}<- do.call(list, Holes)
holes
}
}
<- function(c0, n, phi, shift, depth, colors){
drawCircularGasket <- ApollonianGasket(c0, n, phi, shift, depth)
ApollonianCircles for(i in 1:depth){
for(circ in ApollonianCircles[[i]]){
drawSphere(circ, color = colors[i])
}
}
}
library(viridisLite)
<- c(-5, -4)
A <- c(5, -2)
B <- c(0, 6)
C <- MalfattiCircles(A, B, C)
mcircles <- 3
depth <- viridis(depth + 1)
colors <- 3
n1 <- 4
n2 <- 5
n3 <- 3
depth2 <- 0.2
phi1 <- 0.3
phi2 <- 0.4
phi3 <- 0
shift <- plasma(depth2) colors2
And we get the 3D picture:
library(rgl)
open3d(windowRect = c(50, 50, 562, 562), zoom = 0.9)
bg3d(rgb(54, 57, 64, maxColorValue = 255))
drawTriangularGasket(mcircles, A, B, C, colors, depth)
drawCircularGasket(mcircles[[1]], n1, phi1, shift, depth2, colors2)
drawCircularGasket(mcircles[[2]], n2, phi2, shift, depth2, colors2)
drawCircularGasket(mcircles[[3]], n3, phi3, shift, depth2, colors2)
As an exercise, you can add some animation to this picture, by animating the three circular gaskets.