Fill Missing Points on Line Segments
Problem statement
Consecutive points in points are joined by straight line segments. Fill in the missing points so that consecutive output x-coordinates differ by step.
Return points in increasing x order, beginning at the first given x and ending at the last given x. Preserve every given point. For an inserted x between two consecutive given points, calculate y on the straight segment joining those two points. Use the endpoints of that segment rather than one slope for the entire input. Do not add points outside the given range.
The given x-coordinates are already on the same grid. A single given point returns that point. Return a two-dimensional array of double values; for each numeric output, accept abs(actual - expected) <= 0.000001 × max(1, abs(expected)).
Function
interpolatePoints(points: int[][], step: int) → double[][]Examples
Example 1
points = [[0,10],[5,20],[20,-10]]step = 5return = [[0.0,10.0],[5.0,20.0],[10.0,10.0],[15.0,0.0],[20.0,-10.0]]The given point [5,20] is preserved. From x 5 through 20, y falls by 10 for each x step of 5. The missing values at x 10 and 15 are therefore 10 and 0.
Example 2
points = [[0,10],[10,10],[20,-10]]step = 5return = [[0.0,10.0],[5.0,10.0],[10.0,10.0],[15.0,0.0],[20.0,-10.0]]The segment from [0,10] to [10,10] is horizontal, so the inserted y-value at x 5 is 10. Halfway between [10,10] and [20,-10], the y-value at x 15 is 0.
Example 3
points = [[-2,0],[4,5]]step = 2return = [[-2.0,0.0],[0.0,1.6666666666666667],[2.0,3.3333333333333335],[4.0,5.0]]The y-value rises by 5 across an x-distance of 6. At x 0 and 2, the fractions of that distance are 1/3 and 2/3, giving y-values 5/3 and 10/3.
Constraints
1 <= points.length <= 200- Every point has exactly two integers
[x, y], each between-1000000and1000000, inclusive. 1 <= step <= 1000000- The x-coordinates are strictly increasing. Each consecutive x gap is a positive multiple of
step. (lastX - firstX) / step <= 1000