Simulating a spider web

 

A friend shared a video of a spider making web.

Found the equations for drawing different components of the web.

The figure above was drawn by the following code in R.

--------
# Code based on material provided by Andrew Chambers
# https://ibmathsresources.com/2021/09/10/weaving-a-spider-web/
# 30 Sep 2022

graphics.off()

# First make a graph paper where the web shall be drawn
plot(1,1, type="n", pch=".", xlim=c(-15,15), ylim=c(-15, 15), 
     xlab="", ylab="", axes=F)
for(i in (-15):15) {
  if(i%%5==0) {
    mycol = "gray80"
  } else {
    mycol="gray90"
  }
  abline(h=i, v=i, col=mycol)
}

# Straight lines passing through sharp points
# formula for getting the lines
# Note: In the tutorial, this was drawn at the end
# However it is drawn first here to make it consistent
# with the video of actual spider's work today
get_slope = function(p) {
  numr = sin(9*pi*p/5)-9*sin(pi*p/5)
  denr = cos(9*pi*p/5)+9*cos(pi*p/5)
  return(numr/denr)
}
x = seq(-15,15,by=0.001)
for(p in 1:5) {
  y = get_slope(p)*x
  points(x,y, type="p", pch=".", col="black", cex=2)
}
mtext(text="Step 1: Straight lines", col="black", side=1, cex=2)
# Archimedean spiral, modified from the tutorial
# In the tutorial it was drawn after the hypocycloid
t <- seq(0,7, len=1000)  # the parametric index
# Then convert ( t, 2*pi*t ) to rectilinear coordinates
x = (t)* cos(2*pi*t) 
y = (t)* sin(2*pi*t)
points(x,y, type="l", col="blue", lwd=3)
mtext(text="Step 2: Spiral", col="blue", side=2, cex=2)


# hypocycloid
get_x = function(tx, n=9) {
  cos(tx) + n*cos(tx/9)
}
get_y = function(tx, n=9) {
  sin(tx) - n*sin(tx/9)
}
tx = seq(from=1, to=100, by=0.01)
n = 9
x = get_x(tx, n=n)
y = get_y(tx, n=n)


for(n in 9:13) {
  x = get_x(tx, n=n)
  y = get_y(tx, n=n)
  points(x,y, type="p", pch=".", col="red", cex=2)
}
mtext(text="Step 3: Outer lines", col="red", side=3, cex=2)
-------

Comments

Post a Comment

Popular posts from this blog

The Ground Cipher

The Hill and the Valley