Skip to content

Instantly share code, notes, and snippets.

@pigreco
Last active March 30, 2016 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigreco/58ad00f0bbb2e657278cded9466d2960 to your computer and use it in GitHub Desktop.
Save pigreco/58ad00f0bbb2e657278cded9466d2960 to your computer and use it in GitHub Desktop.
"""
/***************************************************************************
Name : Erone https://it.wikipedia.org/wiki/Formula_di_Erone
Description : Calcolo superficie PoligonZ da file CSV (XYZ)
Date : 25 - marzo - 2016
copyright : (C) 2016 by Salvatore Fiandaca
email : pigrecoinfinito@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
def lungh (x1,y1,z1,x2,y2,z2): #funzione lunghezza
dx,dy,dz= x1-x2, y1-y2, z1-z2
return (dx**2+dy**2+dz**2)**0.5,(dx**2+dy**2)**0.5
def Erone (a,b,c): #funzione formula di Erone
p = (a+b+c)/2.0
return (p*(p-a)*(p-b)*(p-c))**0.5
# =============== importo il file CSV e creo lista coords ===================
import csv
with open ('triangoli3D.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
coords=[] #creo liste
for row in readCSV: #popolo lista
coords+=row
# ========= calcolo lunghezze lati triangolo e creazione liste ==============
lista_3D,lista_2D = [],[] #creo liste
for i in range (0,len(coords)-3,3):
x1,y1,z1=eval(coords [i]),eval(coords[i+1]),eval(coords[i+2])
x2,y2,z2=eval(coords [i+3]),eval(coords[i+4]),eval(coords[i+5])
lun3D,lun2D =lungh (x1,y1,z1,x2,y2,z2) #lunghezza lati 3D/2D
lista_3D+=[lun3D] #popolo lista lunghezze lati 3D
lista_2D+=[lun2D] #popolo lista lunghezze lati 2D
# ================= calcolo elementi per formula di Erone ===================
sup3D,sup2D = 0,0
for j in range (0, len(lista_3D)-2,4):
a,b,c= lista_3D [j],lista_3D [j+1],lista_3D [j+2]
a2,b2,c2 = lista_2D [j],lista_2D [j+1],lista_2D [j+2]
Erone_3D,Erone_2D= Erone (a,b,c),Erone (a2,b2,c2) #formula di Erone
sup3D+=Erone_3D
sup2D+=Erone_2D
# ========================== stampa risultati ===============================
print "*==================*===================*"
print sup3D,'mq Area 3D\n'
print sup2D,'mq Area 2D\n'
print sup3D-sup2D,'mq differenza Area 3D-2D\n'
print round((sup3D-sup2D)/sup3D*100,3),'%'
print "<+++++++++++++++pigreco++++++++++++++++>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment