62
55

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

็ทฏๅบฆ็ตŒๅบฆใ‚ˆใ‚Š่ท้›ขใ‚’๏ผˆๅœฐ็ƒใฎไธธใ•ใ‚‚่€ƒใˆใฆ๏ผ‰ๆฑ‚ใ‚ใ‚‹ใ€‚

62
Last updated at Posted at 2015-01-06

็ทฏๅบฆ็ตŒๅบฆใ‹ใ‚‰่ท้›ขใ‚’ๆฑ‚ใ‚ใ‚‹ใจใใ€ๅœฐ็ƒใฎไธธใ•ใ‚’่€ƒๆ…ฎใซๅ…ฅใ‚Œใฆใ‚‚ๅœฐ็ƒใ‚’็œŸ็ƒใซ่ฟ‘ไผผใ—ใฆ่‰ฏใ‘ใ‚Œใฐ้›ฃใ—ใ„ใ“ใจใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚

from math import sin, cos, acos, radians
earth_rad = 6378.137

def latlng_to_xyz(lat, lng):
    rlat, rlng = radians(lat), radians(lng)
    coslat = cos(rlat)
    return coslat*cos(rlng), coslat*sin(rlng), sin(rlat)

def dist_on_sphere(pos0, pos1, radius=earth_rad):
    xyz0, xyz1 = latlng_to_xyz(*pos0), latlng_to_xyz(*pos1)
    return acos(sum(x * y for x, y in zip(xyz0, xyz1)))*radius

Osaka = 34.702113, 135.494807
Tokyo = 35.681541, 139.767103
London = 51.476853, 0.0

print(dist_on_sphere(Osaka, Tokyo)) # 403.63km
print(dist_on_sphere(London, Tokyo)) # 9571.22km

ใ•ใ‚‰ใซ็ฒพๅบฆใ‚ˆใ่จˆ็ฎ—ใ—ใŸใ‘ใ‚Œใฐใ€ๅ›ž่ปขๆฅ•ๅ††ไฝ“ใจใ‹ใง่ฟ‘ไผผใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใฃใŸใ‚Šใ€acos้–ขๆ•ฐใฏ1่ฟ‘ๅ‚ใง่ชคๅทฎใ‹ๅคงใใ„ใจใ‹ๆ”น่‰ฏใฎไฝ™ๅœฐใฏใ‚ใ‚Šใพใ™ใŒใ€ใ‚ซใ‚ธใƒฅใ‚ขใƒซใช็›ฎ็š„ใชใ‚‰ใ“ใ‚Œใงใใ“ใใ“่กŒใ‘ใ‚‹ใ‹ใจใ€‚

ใจใซใ‹ใใ€ๅˆฅใซไธธใฟใ‚’่€ƒๆ…ฎใ—ใฆใ‚‚ใใ‚“ใช่ค‡้›‘ใ˜ใ‚ƒใ‚ใ‚Šใพใ›ใ‚“ใ‚ˆใ€ใจใ„ใ†ใ“ใจใงใ€‚APIใ‚„ใ‚‰ใƒฉใ‚คใƒ–ใƒฉใƒชใ‚„ใ‚‰ไฝฟใˆใชใ„ใจใใฎใ”ๅ‚่€ƒใพใงใ€‚

62
55
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
55

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?