Robot Arm Inverse Kinematics

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.

Medium6GeometryBinary searchBrute forceNo attempts yetTime limit2sMemory limit512 MB

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 NN segments, numbered 1 to NN 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 θ\theta for the base angle and φ\varphi for the common joint angle, both in degrees. Segment 1 points in the direction reached by turning θ\theta anticlockwise from the positive xx axis. At the joint between segment kk and segment k+1k+1, the direction of segment k+1k+1 is reached by turning φ\varphi anticlockwise from the direction that points back along segment kk. A value of φ=180\varphi = 180 leaves the arm straight, and any smaller value bends it clockwise by the same amount at each joint. Segment kk therefore points in the direction

θ(k1)(180φ)\theta - (k-1)(180 - \varphi)

and the hand, at the far end of segment NN, sits at

(k=1Nkcos(θ(k1)(180φ)),  k=1Nksin(θ(k1)(180φ)))\left(\sum_{k=1}^{N} \ell_k \cos\big(\theta - (k-1)(180-\varphi)\big),\; \sum_{k=1}^{N} \ell_k \sin\big(\theta - (k-1)(180-\varphi)\big)\right)

where k\ell_k is the length of segment kk.

In the picture the arm is anchored at (0,0)(0, 0), 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 (3,0)(3, 0).

Given the segment lengths and a position for the hand, recover θ\theta and φ\varphi.

Several pairs (θ,φ)(\theta, \varphi) can put the hand at the same point. Report the pair whose common joint angle φ\varphi 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 NN, XX and YY separated by spaces. NN is the number of segments, 2N302 \le N \le 30. XX and YY are the required coordinates of the hand and are floating point values. The next NN 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 10X10-10 \le X \le 10 and 10Y10-10 \le Y \le 10. 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.