A stepper motor consists of a metal rotor surrounded by several identical electromagnets. When a pulse of current is sent through the appropriate electromagnet, the rotor rotates by a fixed angular increment known as one "step". A simple computer known as the motion controller regulates the electromagnets and keeps track of the current position of the rotor as an integer. A clockwise step adds 1 and a counterclockwise step subtracts 1 from this position, which is relative to an arbitrary zero point and can be negative. Since stepper motors provide precise control over the motion of the rotor, they have applications ranging from floppy drives to medical imaging devices to industrial fabrication machinery.
You are writing software that communicates with the motion controller for a stepper motor with n steps in one complete revolution. The apparatus driven by your motor is purely rotational, so rotor position p is identical to position p+i*n, where i is any integer. You have the rotor's current position and a list of target positions, and you wish to move the rotor to the nearest position that is identical to one of the target positions. Write a class StepperMotor with a method rotateToNearest that takes an int n, an int current, and a int[] target, and returns an int that is the minimum number of steps (signed positive for clockwise or negative for counterclockwise) necessary to reach one of the targets from position current. If there is a tie between a positive number and negative number for the smallest value, return the positive number. |