proviamo in 3D

In [1]:
import numpy as np
import math
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3
import matplotlib.colors as colors
#import pylab as pl
from celluloid import Camera
from IPython.display import HTML
import warnings
In [2]:
def cubo(x,y,x1,y1,x2,y2,x3,y3):
    l = math.sqrt((x-x1)**2+(y-y1)**2)
    trib11 = []
    trib12 = []
    trif11 = []
    trif12 = []
    trif21 = []
    trif22 = []
    trif31 = []
    trif32 = []
    trif41 = []
    trif42 = []
    trib21 = []
    trib22 = []
    coloref = 'b'
    spessore = 0
    trasp =0.10
    #base giĆ¹
    trib11.append([x,y,0])
    trib11.append([x1,y1,0])
    trib11.append([x2,y2,0])
    trib12.append([x,y,0])
    trib12.append([x2,y2,0])
    trib12.append([x3,y3,0])
    qua1 = a3.art3d.Poly3DCollection(trib11,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua2 = a3.art3d.Poly3DCollection(trib12,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua1)
    spazio.add_collection(qua2)
    #faccia 1
    trif11.append([x,y,0])
    trif11.append([x1,y1,0])
    trif11.append([x1,y1,l])
    trif12.append([x,y,0])
    trif12.append([x1,y1,l])
    trif12.append([x,y,l])
    qua3 = a3.art3d.Poly3DCollection(trif11,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua4 = a3.art3d.Poly3DCollection(trif12,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua3)
    spazio.add_collection(qua4)
    #faccia 2
    trif21.append([x1,y1,0])
    trif21.append([x2,y2,0])
    trif21.append([x2,y2,l])
    trif22.append([x1,y1,0])
    trif22.append([x2,y2,l])
    trif22.append([x1,y1,l])
    qua5 = a3.art3d.Poly3DCollection(trif21,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua6 = a3.art3d.Poly3DCollection(trif22,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua5)
    spazio.add_collection(qua6)
    #faccia 3
    trif31.append([x2,y2,0])
    trif31.append([x3,y3,0])
    trif31.append([x2,y2,l])
    trif32.append([x2,y2,l])
    trif32.append([x3,y3,l])
    trif32.append([x3,y3,0])
    qua7 = a3.art3d.Poly3DCollection(trif31,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua8 = a3.art3d.Poly3DCollection(trif32,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua7)
    spazio.add_collection(qua8)
    #faccia 4
    trif41.append([x3,y3,0])
    trif41.append([x,y,0])
    trif41.append([x3,y3,l])
    trif42.append([x,y,l])
    trif42.append([x,y,l])
    trif42.append([x3,y3,0])
    qua9 = a3.art3d.Poly3DCollection(trif41,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua10 = a3.art3d.Poly3DCollection(trif42,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua9)
    spazio.add_collection(qua10)
    #base su
    trib21.append([x,y,l])
    trib21.append([x1,y1,l])
    trib21.append([x2,y2,l])
    trib22.append([x,y,l])
    trib22.append([x2,y2,l])
    trib22.append([x3,y3,l])
    qua11 = a3.art3d.Poly3DCollection(trib21,facecolors=coloref, linewidths=spessore, alpha=trasp)
    qua12 = a3.art3d.Poly3DCollection(trib22,facecolors=coloref, linewidths=spessore, alpha=trasp)
    spazio.add_collection(qua11)
    spazio.add_collection(qua12)            
In [8]:
# ruota il segmento di estremi (ptx,pty) di pi/2 attorno (cx,cy)
def ruota(ptx,pty,cx,cy,verso):
    x = ptx-cx
    y = pty-cy
    px = -verso*y+cx
    py = verso*x+cy
    return (px, py)
# costruisce triangoli e quadrati in 3D sul piano z=0
def triangoloequadrati3D(x,y,x1,y1):
    triangolo = []
    lato = math.sqrt((x-x1)**2+(y-y1)**2)
    if lato > dettaglio:        
        bx = (x-x1)*(math.cos(alpha))**2
        by = (y-y1)*(math.cos(alpha))**2
        (nx,ny) = ruota(bx,by,0,0,-1)
        ax = nx*math.tan(alpha)
        ay = ny*math.tan(alpha)
        x2 = x1+bx+ax
        y2 = y1+by+ay
        (q2x, q2y) = ruota(x2,y2,x,y,1)
        (q3x, q3y) = ruota(x,y,q2x,q2y,1)
        triangolo.append([x,y,h])
        triangolo.append([x1,y1,h])
        triangolo.append([x2,y2,h])
        tri = a3.art3d.Poly3DCollection(triangolo,facecolors='k', linewidths=1, alpha=0.8)
        spazio.add_collection(tri)
        # cubo primo cateto
        cubo(x,y,x2,y2,q3x,q3y,q2x,q2y)
        # cubo secondo cateto
        (r2x, r2y) = ruota(x2,y2,x1,y1,-1)
        (r3x, r3y) = ruota(x1,y1,r2x,r2y,-1)
        cubo(x2,y2,x1,y1,r2x,r2y,r3x,r3y)
        # chiamata ricorsiva
        triangoloequadrati3D(r3x,r3y,r2x,r2y)
        triangoloequadrati3D(q2x,q2y,q3x,q3y)
    else:
        return 1
In [19]:
h = 0
alpha = math.pi/3
figura = plt.figure(figsize=(20,10))
#spazio = a3.Axes3D(plt.figure())
spazio = figura.gca(projection='3d')
camera = Camera(figura)
dettaglio = 3.9
dx = 0.05
n = int((dettaglio-0.1)/dx)
spazio.set_axis_off()
spazio.set_xlim(-10,10)
spazio.set_ylim(-5,15)
spazio.set_zlim(0,5)
#spazio.view_init(120, 0)
for i in range(0,n):
    triangoloequadrati3D(0,0,4,0)
    dettaglio -= dx
    camera.snap()
anim = camera.animate()
HTML(anim.to_jshtml())
#triangoloequadrati3D(0,0,4,0)
#pl.savefig('logo3D.png')
#pl.show()
/home/joao/PycharmProjects/logoJoao3D/venv_test/lib64/python3.7/site-packages/numpy/lib/function_base.py:393: RuntimeWarning: Mean of empty slice.
  avg = a.mean(axis)
Out[19]: