Guest User

Untitled

a guest
Jun 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. library(plotrix) # for the draw.circle function
  2.  
  3. # radius of the inner Soddy circle ####
  4. soddyRadius <- function(r1,r2,r3){
  5. 1/(1/r1+1/r2+1/r3+2*sqrt(1/r1/r2+1/r2/r3+1/r3/r1))
  6. }
  7.  
  8. # center of the inner Soddy circle (equal detour point) ####
  9. soddyCenter <- function(A,B,C){
  10. measure <- function(M,N){
  11. sqrt(c(crossprod(N-M)))
  12. }
  13. a <- measure(B,C)
  14. b <- measure(A,C)
  15. c <- measure(A,B)
  16. AB <- B-A
  17. AC <- C-A
  18. Delta <- 0.5*abs(AB[1]*AC[2]-AB[2]*AC[1])
  19. tc <- 1 + 2*Delta/c(a*(b+c-a), b*(c+a-b), c*(a+b-c)) # triangular coordinates
  20. den <- c(tcrossprod(t(tc), c(a,b,c)))
  21. k1 <- a*tc[1]/den
  22. k2 <- b*tc[2]/den
  23. k3 <- c*tc[3]/den
  24. k1*A + k2*B + k3*C
  25. }
  26.  
  27. # draw a circle ####
  28. drawCircle <- function(C, ...){
  29. draw.circle(C$center[1], C$center[2], C$radius, ...)
  30. }
  31.  
  32. # starting circles ####
  33. C1 <- list(center=c(1,0),radius=1)
  34. C2 <- list(center=c(-1,0),radius=1)
  35. C3 <- list(center=c(0,sqrt(3)),radius=1)
  36.  
  37. # main function (recursive) ####
  38. Apollony <- function(C1, C2, C3, n, colors=FALSE, ...){
  39. soddyCenter <- soddyCenter(C1$center,C2$center,C3$center)
  40. radius <- soddyRadius(C1$radius,C2$radius,C3$radius)
  41. Cnew <- list(center=soddyCenter, radius=radius)
  42. drawCircle(Cnew, col=if(colors) Colors[n] else NA, ...)
  43. #print("x")
  44. if(n==1) return(invisible())
  45. Apollony(Cnew,C2,C3,n-1, colors=colors)
  46. Apollony(C1,Cnew,C3,n-1, colors=colors)
  47. Apollony(C1,C2,Cnew,n-1, colors=colors)
  48. }
  49.  
  50. # Apollony fractal ####
  51. par(mar=c(0,0,0,0))
  52. plot(0, 0, type="n", axes=FALSE, xlab=NA, ylab=NA, ylim=c(0,sqrt(3)/2), asp=1)
  53. drawCircle(C1); drawCircle(C2); drawCircle(C3)
  54. n <- 7
  55. Colors <- rainbow(n)
  56. Apollony(C1, C2, C3, n, colors=TRUE)
Add Comment
Please, Sign In to add comment