Robot Arm Inverse Kinematics
Time limit2sMemory limit512 MB
Given segment lengths and a target hand position for a robot arm with equal joint angles, recover the base angle and joint angle that reach it.
- Level
Medium6 of 10
- Topics
- Geometry, Binary search, Brute force
- Solved
- No attempts yet
Problem
Moving a robot arm is harder than it looks. When the arm is built from several segments, the angle at every joint contributes to where the hand ends up. In an industrial robot the operator gives only the target position of the hand, and software works out the joint angles. That computation is called inverse kinematics.
The arm here has segments, numbered 1 to starting at the base. The base is anchored at the origin and segment 1 can point in any direction. Every joint between two consecutive segments is locked to the same angle.

Write for the base angle and for the common joint angle, both in degrees. Segment 1 points in the direction reached by turning anticlockwise from the positive axis. At the joint between segment and segment , the direction of segment is reached by turning anticlockwise from the direction that points back along segment . A value of leaves the arm straight, and any smaller value bends it clockwise by the same amount at each joint. Segment therefore points in the direction
and the hand, at the far end of segment , sits at
where is the length of segment .
In the picture the arm is anchored at , the segment lengths are 1, 3 and 1, the base angle is 90 degrees and the common joint angle is 90 degrees, so the hand lands at .
Given the segment lengths and a position for the hand, recover and .
Several pairs can put the hand at the same point. Report the pair whose common joint angle is largest, which is the least bent arm that reaches the target.
Input
The input holds a sequence of problems, at most 20 of them. The first line of each problem has , and separated by spaces. is the number of segments, . and are the required coordinates of the hand and are floating point values. The next lines hold the segment lengths in order from the base, one floating point value per line. A line holding three zeros ends the input and is not a problem.
Every segment length is between 1 and 10 inclusive, and the coordinates satisfy and . The hand is never at the origin. Every problem has at least one solution, and the data avoids answers whose third decimal digit sits on a rounding boundary.
Output
Print one line per problem with two values separated by a space: the base angle first, then the common joint angle. Both are in degrees, rounded to exactly 3 decimal digits. When more than one pair of angles reaches the target, print the pair whose common joint angle is largest. The base angle is reported in the range 0 (inclusive) to 360 (exclusive) and the common joint angle in the range 0 to 180 inclusive.