#!/usr/bin/env python # coding: utf-8 # # Examples of 3D plots from Sage's reference manual # # rendered with three.js # # This notebook runs some examples of the [3D graphics section](http://doc.sagemath.org/html/en/reference/plot3d/) of Sage's reference manual with the [three.js](http://doc.sagemath.org/html/en/reference/plot3d/threejs.html) renderer. # In[1]: version() # ## Examples from [*Plotting functions*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/plot3d.html) # In[2]: x, y = var('x y') W = plot3d(sin(pi*((x)^2+(y)^2))/2,(x,-1,1),(y,-1,1), frame=False, color='purple', opacity=0.8) S = sphere((0,0,0),size=0.3, color='red', aspect_ratio=[1,1,1]) show(W + S, viewer='threejs') # In[3]: def f(x,y): return math.sin(y^2+x^2)/math.sqrt(x^2+y^2+0.0001) P = plot3d(f,(-3,3),(-3,3), adaptive=True, color=rainbow(60, 'rgbtuple'), max_bend=.1, max_depth=15) P.show(viewer='threejs') # In[4]: S = sphere(size=.5, color='yellow') from sage.plot.plot3d.shapes import Cone S += Cone(.5, .5, color='red').translate(0,0,.3) S += sphere((.45,-.1,.15), size=.1, color='white') \ + sphere((.51,-.1,.17), size=.05, color='black') S += sphere((.45, .1,.15),size=.1, color='white') \ + sphere((.51, .1,.17), size=.05, color='black') S += sphere((.5,0,-.2),size=.1, color='yellow') def f(x,y): return math.exp(x/5)*math.cos(y) P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow'], max_depth=10) cape_man = P.scale(.2) + S.translate(1,0,0) cape_man.show(aspect_ratio=[1,1,1], viewer='threejs') # In[5]: theta, z = var('theta z') cylindrical_plot3d(e^(-z^2)*(cos(4*theta)+2)+1, (theta,0,2*pi), (z,-2,2), plot_points=[80,80]).show(aspect_ratio=(1,1,1), viewer='threejs') # In[6]: L = plot3d(lambda x,y: 0, (-5,5), (-5,5), color="lightblue", opacity=0.8) P = plot3d(lambda x,y: 4 - x^3 - y^2, (-2,2), (-2,2), color='green') Q = plot3d(lambda x,y: x^3 + y^2 - 4, (-2,2), (-2,2), color='orange') show(L + P + Q, viewer='threejs') # ## Examples from [*Parametric plots*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/parametric_plot3d.html) # In[7]: u = var('u') sage: parametric_plot3d((sin(u), cos(u), u/10), (u,0,20), viewer='threejs') # In[8]: u, v = var('u,v') parametric_plot3d((cos(u), sin(u)+cos(v), sin(v)), (u,0,2*pi), (v,-pi,pi), mesh=True, viewer='threejs') # In[9]: cm = colormaps.ocean def c(x,y): return sin(x*y)**2 from sage.plot.plot3d.parametric_surface import MoebiusStrip MoebiusStrip(5, 1, plot_points=200, color=(c,cm), viewer='threejs') # In[10]: f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v)) f2 = (8+(3+cos(v))*cos(u), 3+sin(v), 4+(3+cos(v))*sin(u)) p1 = parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), texture="red") p2 = parametric_plot3d(f2, (u,0,2*pi), (v,0,2*pi), texture="blue") (p1 + p2).show(viewer='threejs') # In[11]: K = sqrt(0.84) G = (0.4*((K*cosh(0.4*u))^2 + (0.4*sin(K*v))^2)) f_x = (2*K*cosh(0.4*u)*(-(K*cos(v)*cos(K*v)) - sin(v)*sin(K*v)))/G f_y = (2*K*cosh(0.4*u)*(-(K*sin(v)*cos(K*v)) + cos(v)*sin(K*v)))/G f_z = -u + (2*0.84*cosh(0.4*u)*sinh(0.4*u))/G parametric_plot3d([f_x, f_y, f_z], (u,-13.2,13.2), (v,-37.4,37.4), plot_points=[90,90], frame=False, color="green", viewer='threejs') # ## Examples from [*Surfaces of revolution*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/revolution_plot3d.html) # In[12]: curve = (u, cos(4*u), u^2) P = revolution_plot3d(curve, (u,0,2), show_curve=True, parallel_axis='z', axis=(1,.2), opacity=0.5) P.show(aspect_ratio=(1,1,1), viewer='threejs') # ## Examples from [*Plotting 3D fields*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/plot_field3d.html) # In[13]: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi), colors=['red','green','blue'], viewer='threejs') # ## Examples from [*Implicit Plots*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/implicit_plot3d.html) # In[14]: implicit_plot3d((x^2 + y^2 + z^2), (x,-2,2), (y,-2,2), (z,-2,2), plot_points=60, contour=[1,3,5], region=lambda x,y,z: x<=0.2 or y>=0.2 or z<=0.2, color='aquamarine', viewer='threejs') # In[15]: T = RDF(golden_ratio) F = 2 - (cos(x+T*y) + cos(x-T*y) + cos(y+T*z) + cos(y-T*z) + cos(z-T*x) + cos(z+T*x)) r = 4.77 implicit_plot3d(F, (x,-r,r), (y,-r,r), (z,-r,r), plot_points=40, color='darkkhaki', viewer='threejs') # ## Other examples # In[16]: polytopes.dodecahedron().plot(opacity=.5) # In[17]: icosahedron() # In[18]: a, b = var('a b') E1 = x^2 + y^2 + z^2 - a^2 == 0 E2 = x^2 + y^2 - b^2*z^2 == 0 P1 = implicit_plot3d(E1.lhs().subs([a==2]),(x, -2, 2), (y, -2, 2), (z, -2, 2), color="blue", opacity=0.5) P2 = implicit_plot3d(E2.lhs().subs([b==1]),(x, -2, 2), (y, -2, 2), (z, -2, 2), color="yellow", opacity=0.5) P1 + P2 # In[ ]: