How to plot interlocking tori?
gnuplot, plot, r
Solution
My answer is based on the examples from these two websites:
3D plots in R
R: Fun with surf3D function
And here's how you do it:
library(plot3D)
x <- seq(-pi,pi,by=0.1)
y <- seq(-pi,pi,by=0.1)
R = 1.0
r = 0.5
M <- mesh(x, y)
alpha <- M$x
beta <- M$y
surf3D(x = (R + r*cos(alpha)) * cos(beta),
y = (R + r*cos(alpha)) * sin(beta),
z = r * sin(alpha),
col="blue", colkey=FALSE, alpha = 0.3, border="black",
xlim=c(-0.5*pi,0.5*pi), ylim=c(-0.5*pi,0.5*pi), zlim=c(-0.5*pi,0.5*pi))
surf3D(x = R + cos(alpha) + r*cos(alpha) * cos(beta),
y = r*sin(beta),
z = sin(alpha)+ r * sin(alpha) * cos(beta),
col="red", colkey=FALSE, alpha = 0.3, border="black", add=T)
Which generates this plot:
The important things to note are:
- use the `surf3D` function
- set up your angles using `mesh`
- to see it all, specify ranges (`xlim`, etc) with the first plot
- plot with the colors and alphas to get the coloring you are after
Problem
How can I plot interlocking tori in R as shown on this page for gnuplot: I tried following codes in R but they do not work: ``` try_tori_1 = function(){ library(rgl) xvec= yvec = seq(-pi,pi,by=0.1); z <- outer(xvec,yvec,function(x,y) {sin(x)+.5*sin(x)*cos(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue") z <- outer(xvec,yvec,function(x,y) {cos(x)+.5*cos(x)*cos(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", add=T) z <- outer(xvec,yvec,function(x,y) {.5*sin(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", add=T) z <- outer(xvec,yvec,function(x,y) {1+cos(x)+.5*cos(x)*cos(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", add=T) z <- outer(xvec,yvec,function(x,y) {.5*sin(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", add=T) z <- outer(xvec,yvec,function(x,y) {sin(x)+.5*sin(x)*cos(y)} ) persp3d(z, theta = 30, phi = 30, expand = 0.5, col = "lightblue", add=T) } try_tori_2 = function(){ library(rgl) u= v = seq(-pi,pi,by=0.1); plot3d(cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v)) plot3d(1+cos(u)+.5*cos(u)*cos(v),.5*sin(v),sin(u)+.5*sin(u)*cos(v)) } ```