Skip to content

Instantly share code, notes, and snippets.

@naoty
Last active November 29, 2021 05:26
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoty/5821666 to your computer and use it in GitHub Desktop.
Save naoty/5821666 to your computer and use it in GitHub Desktop.
ある緯度経度で指定された地点からある距離と方向に移動した後の緯度経度を計算する
here = Position.new(lat_A, lng_B)
# 90度方向に100m移動したあとの地点
there = here.move(distance: 100, heading: 90)
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