Problem 287556 · medium · Robotics · Level 02 Motion

One Joint Sweeps the Stage

forward kinematics · polyline length · maximum with tolerance · plotting

A camera crane over a theatre stage is a planar arm with n links, its base at the origin (x right, y up). Link i has length lengths[i] and joint angle angles[i] (radians, counter-clockwise). The angles are relative: angles[0] is measured from the +x axis, every later one from the direction of the link before it. The camera is at the hand, the end of the last link.

For a dress rehearsal, joint number joint (counting from 0) is swept through the angles in values, one after the other, while every other joint keeps its angle from angles. At each value the camera stops and takes a picture.

Write sweep(lengths, angles, joint, values) that returns a tuple (distance, top):

  • distance: the length of the camera's path, measured as the sum of the straight distances between consecutive stops (0.0 for a single stop);
  • top: the stop (x, y) where the camera is highest. Heights within 1e-9 of each other count as equal: if M is the greatest height, return the first stop, in the order of values, whose height is at least M - 1e-9.

Collect the stops and press Run with plot(xs, ys, kind="scatter") to see the camera's path.

Examples

Input:  lengths = [1, 1], angles = [0, 0], joint = 1, values = [0, math.pi / 2, math.pi]
Output: (2.82842712, (1.0, 1.0))
Explanation: the stops are (2, 0), (1, 1) and (0, 0): two straight steps of
length sqrt(2) each. The highest is (1, 1).

Input:  lengths = [2], angles = [0], joint = 0, values = [math.pi / 4, 3 * math.pi / 4]
Output: (2.82842712, (1.41421356, 1.41421356))
Explanation: both stops are at height sqrt(2), up to rounding in the last digit,
so the first one wins.

Input:  lengths = [0.5, 0.4, 0.3], angles = [0.8, 0.0, -0.6], joint = 1,
        values = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]
Output: (1.3352927, (0.36617984, 1.02782882))

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Recompute the hand position while one joint takes a list of values
  • Measure a path as the sum of the straight steps between its points
  • Pick a maximum with a stated tolerance and tie-break
Starting Python…