LoginSignup
1
1

More than 5 years have passed since last update.

Rubyで角度を(0〜359度の)正の角度に変換する

Posted at

①(0〜359度の)正の角度変換(Numeric#modulo版)

def positive_angle(angle)
  angle.modulo(360)
end

②(0〜359度の)正の角度変換(Numeric#remainder版)

def positive_angle_by_remainder(angle)
  (angle.remainder(360) + 360).remainder(360)
end

③余談:-359〜359度の角度変換

def angle_by_remainder(angle)
  angle.remainder(360)
end

変換結果

角度
-720 0 0 0
-630 90 90 -270
-540 180 180 -180
-450 270 270 -90
-360 0 0 0
-270 90 90 -270
-180 180 180 -180
-90 270 270 -90
0 0 0 0
90 90 90 90
180 180 180 180
270 270 270 270
360 0 0 0
450 90 90 90
540 180 180 180
630 270 270 270
720 0 0 0
1
1
0

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
1
1