Last active
May 14, 2024 12:44
-
-
Save naoty/5821666 to your computer and use it in GitHub Desktop.
ある緯度経度で指定された地点からある距離と方向に移動した後の緯度経度を計算する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
here = Position.new(lat_A, lng_B) | |
# 90度方向に100m移動したあとの地点 | |
there = here.move(distance: 100, heading: 90) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Position | |
attr_accessor :latitude, :longitude | |
# 地球の半径 | |
EARTH_RADIUS = 6378150 | |
def initialize(latitude, longitude) | |
@latitude = latitude | |
@longitude = longitude | |
end | |
def move(distance: 0, heading: 0) | |
# 緯線上の移動距離 | |
latitude_distance = distance * Math.cos(heading * Math::PI / 180) | |
# 1mあたりの緯度 | |
earth_circle = 2 * Math::PI * EARTH_RADIUS | |
latitude_per_meter = 360 / earth_circle | |
# 緯度の変化量 | |
latitude_delta = latitude_distance * latitude_per_meter | |
new_latitude = @latitude + latitude_delta | |
# 経線上の移動距離 | |
longitude_distance = distance * Math.sin(heading * Math::PI / 180) | |
# 1mあたりの経度 | |
earth_radius_at_longitude = EARTH_RADIUS * Math.cos(new_latitude * Math::PI / 180) | |
earth_circle_at_longitude = 2 * Math::PI * earth_radius_at_longitude | |
longitude_per_meter = 360 / earth_circle_at_longitude | |
# 経度の変化量 | |
longitude_delta = longitude_distance * longitude_per_meter | |
Position.new(new_latitude, @longitude + longitude_delta) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment