Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ws1415:projekte_im_wintersemester_2014_15:nuetzliche_programmschnipsel

Dies ist eine alte Version des Dokuments!


Nützliche Programmschnipsel

Dreiecke zeichnen

Noch eine Variante

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  delaunay.py
#  
#  Copyright 2014 Stefan Born <born@math.tu-berlin.de>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
import numpy as np
 
###  erzeuge ein Gitter von x,y-Werten mit zugehörigen z-Werten
 
x=np.linspace(-1,1,10)
y=np.linspace(-1,1,10)
XX,YY=np.meshgrid(x,y)
ZZ=np.sin(XX*XX+YY*YY)
 
###  Erzeuge ein Array mit (x,y)-Koordinaten und eines
###  mit (x,y,z)-Koordinaten
 
points3d = np.array([XX.flatten(),YY.flatten(),ZZ.flatten()]).T
points2d=  np.array([XX.flatten(),YY.flatten()]).T
 
### Berechne Delaunay-Triangulierung des Gitters
 
from scipy.spatial import Delaunay
tri = Delaunay(points2d)
 
###  Darstellung auf der Basis der erzeugten Dreiecke -- es gibt
###  in Matplotlib ganz verschiedene Schnittstellen, die dasselbe
###  tun. 
 
###  Hier wird mit ax=mplot3d.Axes3D(...) ein 3d-Koordinatensystem
###  erzeugt, dem man 3d-Objekte hinzufügen kann mit ax.add_collection3d(...)
###  In unserem Fall werden Dreiecke hinzugefügt, die mit 
###  mplot3d.art3d.Poly3DCollection(...) werden
 
import mpl_toolkits.mplot3d as mplot3d
import matplotlib.colors as colors
import pylab as pl
import numpy as np
 
ax = mplot3d.Axes3D(pl.figure())
for simplex in tri.simplices:
    ecken = np.array([points3d[simplex[0]],points3d[simplex[1]],points3d[simplex[2]]])
 
    dreieck = mplot3d.art3d.Poly3DCollection([ecken])
    ### Man kann von diesen Graphikobjekten Eigenschaften einstellen:
    dreieck.set_color(colors.rgb2hex(np.random.rand(3)))
    dreieck.set_edgecolor('k')
 
    ax.add_collection3d(dreieck)
 
pl.show()
ws1415/projekte_im_wintersemester_2014_15/nuetzliche_programmschnipsel.1417080045.txt.gz · Zuletzt geändert: 2016/05/10 14:46 (Externe Bearbeitung)