用python产生随机地球模型生成器
程序员文章站
2024-03-21 16:15:22
...
用python产生随机地球模型生成器
这是一个用数学方法,生成数字地球的一种路径。
特点
1、用三维空间坐标系,x、y、z方向,随机方法产生地球上任意球面点位置;
2、生成txet文档
3、直接用Autocad2012及以上版本,采用 “l”(直线命令)+“c”(闭合命令),生成三维空间坐标地球。
本方法产生速度快,效率高。
使用
输入想产生点的数量n,在文件目录下会生成txt文档,第一个为x坐标轴,第二个为y坐标轴,第三个为z坐标轴,每段隔行,便于CAD直接张贴;在第一行之前,输入直线命令 “l”,在结尾输入“c”(此步可省略)。
代码
直接上python原代码
from random import random
from math import sqrt
import time
## 地球球体的立体坐标及计算
second=time.time() #获取开始时间
a=input("\n\n请输入点数量,按下enter键后退出") #点数量
n=int(a)
R=6371000 #地球半径,m为单位
for i in range(1,n):
##上半球--第一象限
x1=random()*R
y1=sqrt(R*R-x1*x1)*random()
z1=sqrt(R*R-y1*y1-x1*x1)
d1=sqrt(x1*x1+y1*y1+z1*z1)
ticks = time.time() # 获取完成时间
t=time.time()-second
print("x1=",x1,"y1=",y1,"z1=",z1)
print("d1=",d1)
print("time1=",t,"秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x1));f.write(",");f.write(str(y1));f.write(",");f.write(str(z1))
f.write("\n")
for i in range(1, n):
##上半球--第二象限
x2=-random()*R
y2=sqrt(R*R-x2*x2)*random()
z2=sqrt(R*R-y2*y2-x2*x2)
d2=sqrt(x2*x2+y2*y2+z2*z2)
ticks = time.time() # 获取完成时间
t=time.time()-second
print("x2=",x2,"y2=",y2,"z2=",z2)
print("d2=",d2)
print("time2=",t,"秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x2));f.write(",");f.write(str(y2));f.write(",");f.write(str(z2))
f.write("\n")
for i in range(1, n):
##上半球--第三象限
x3=-random()*R
y3=-sqrt(R*R-x3*x3)*random()
z3=sqrt(R*R-y3*y3-x3*x3)
d3=sqrt(x3*x3+y3*y3+z3*z3)
ticks = time.time() # 获取完成时间
t=time.time()-second
print("x3=",x3,"y3=",y3,"z3=",z3)
print("d3=",d3)
print("time3=",t,"秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x3));f.write(",");f.write(str(y3));f.write(",");f.write(str(z3))
f.write("\n")
for i in range(1, n):
##上半球--第四象限
x4=random()*R
y4=-sqrt(R*R-x4*x4)*random()
z4=sqrt(R*R-y4*y4-x4*x4)
d4=sqrt(x4*x4+y4*y4+z4*z4)
ticks = time.time() # 获取完成时间
t=time.time()-second
print("x4=",x4,"y4=",y4,"z4=",z4)
print("d4=",d4)
print("time4=",t,"秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x4));f.write(",");f.write(str(y4));f.write(",");f.write(str(z4))
f.write("\n")
##=======================================================================
for i in range(1, n):
##下半球--第一象限
x5 = random() * R
y5 = sqrt(R * R - x5 * x5) * random()
z5 = -sqrt(R * R - y5 * y5 - x5 * x5)
d5 = sqrt(x5 * x5 + y5 * y5 + z5 * z5)
ticks = time.time() # 获取完成时间
t = time.time() - second
print("x5=", x5, "y5=", y5, "z5=", z5)
print("d5=", d5)
print("time5=", t, "秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x5));f.write(",");f.write(str(y5));f.write(",");f.write(str(z5))
f.write("\n")
##下半球--第二象限
for i in range(1, n):
x6 = -random() * R
y6 = sqrt(R * R - x6 * x6) * random()
z6 = -sqrt(R * R - y6 * y6 - x6 * x6)
d6 = sqrt(x6 * x6 + y6 * y6 + z6 * z6)
ticks = time.time() # 获取完成时间
t = time.time() - second
print("x6=", x6, "y6=", y6, "z6=", z6)
print("d6=", d6)
print("time6=", t, "秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x6));f.write(",");f.write(str(y6));f.write(",");f.write(str(z6))
f.write("\n")
##下半球--第三象限
for i in range(1, n):
x7 = -random() * R
y7 = -sqrt(R * R - x7 * x7) * random()
z7 = -sqrt(R * R - y7 * y7 - x7 * x7)
d7 = sqrt(x7 * x7 + y7 * y7 + z7 * z7)
ticks = time.time() # 获取完成时间
t = time.time() - second
print("x7=", x7, "y7=", y7, "z7=", z7)
print("d7=", d7)
print("time7=", t, "秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x7));f.write(",");f.write(str(y7));f.write(",");f.write(str(z7))
f.write("\n")
##上半球--第四象限
for i in range(1, n):
x8 = random() * R
y8 = -sqrt(R * R - x8 * x8) * random()
z8 = -sqrt(R * R - y8 * y8 - x8 * x8)
d8 = sqrt(x8 * x8 + y8 * y8 + z8 * z8)
ticks = time.time() # 获取完成时间
t = time.time() - second
print("x8=", x8, "y8=", y8, "z8=", z8)
print("d8=", d8)
print("time8=", t, "秒")
with open("sphere earth.txt", "a", encoding='utf-8') as f:
f.write(str(x8));f.write(",");f.write(str(y8));f.write(",");f.write(str(z8))
f.write("\n")
注释
R为半径
分为4个象限,8个区间,各自产生随机点,坐标轴同空间三位坐标。
甘特图
导出与导入
直接张贴复制
上一篇: 【C++深度解析】22、数组操作符重载
下一篇: ubuntu下载百度网盘的文件
推荐阅读