In this scenario, an angle of 0 means that the line segment between the min index (0) and max index (7) is horizontal on the right half of the circle - the image on the left. In the middle image, the wheel has been rotated 23 degrees. Here the correct answer (x) is 3, as the yellow pointer is on the segment at index 3. In the image on the right, the wheel has been rotated 220 degrees, and the correct answer (x) is 7.
In this wheel, n = 8. There are 8 segments. The angle (a) is how far the wheel has been rotated from its original position.
So given n and a, how would you calculate x?
CodePudding user response:
Just switch the angle and create a case for each 45 angle range:
let angle = 220.0
switch angle.truncatingRemainder(dividingBy: 360) {
case 0..<45: print("3")
case 45..<90: print("2")
case 90..<135: print("1")
case 135..<180: print("0")
case 180..<225: print("7") // 7
case 225..<270: print("6")
case 270..<315: print("5")
case 315..<360: print("4")
default: break
}
if you really want a formula you can subtract the angle from 360, divide it by 45 (section angle = 360 divided by number of sections), add 4 (initial number of sections offset), round down and truncate reminder dividing by 8 (number of sections).
func segment(from angle: Double) -> Double {
((360.0-angle) / 45.0 4.0).rounded(.down)
.truncatingRemainder(dividingBy: 8)
}
segment(from: 23) // 3
segment(from: 60) // 2
segment(from: 123) // 1
segment(from: 170) // 0
segment(from: 220) // 7
segment(from: 235) // 6
segment(from: 280) // 5
segment(from: 322) // 4