计算已知点组的密度

2023-01-04 16:21:05

array([[-3.90611544e+00, -5.47953465e-01],

[-5.22999684e+00,  5.56145331e-01],

[-4.84611012e+00,  5.54304197e-02],

[-4.85019718e+00, -3.19791419e-01],

[-4.59453620e+00,  5.70821744e-01],

[-6.65068624e+00, -9.97229190e-01],

[-6.57787930e+00, -5.03538827e-01],

[-4.80275333e+00, -8.42197968e-02],

[-4.55720113e+00,  8.23122108e-01],

[-4.47469205e+00, -6.77669238e-01],

[-5.84095559e+00, -8.19564981e-01],

[-4.93963103e+00, -8.66167854e-01],

[-4.98336307e+00, -4.45923700e-02],

[-4.56953722e+00, -4.27976712e-01],

[-6.25553298e+00,  1.32863878e-01],

[-6.11860914e+00, -1.09009817e+00],

[-5.60347264e+00,  1.34600670e+00],

[-4.85974421e+00, -2.03600566e-01],

[-4.38049846e+00,  1.27302889e+00],

.......

1 回答

TA贡献1566条经验 获得超6个赞

from  math import sqrt

points = [

[1, 3],

[2, 4],

[9, 1],

[2, 6],

[5, 3],

]

def density(points):

total_distance = 0

count = 0

i = 0

for x1,y1 in points:

for x2,y2 in points[i+1:]:

count += 1

total_distance += sqrt((x1-x2)**2 + (y1-y2)**2)

i += 1

print(count)

return count/total_distance

print(density(points))

0.2131384729384717

• 1 回答
• 0 关注
• 10 浏览

0/150