Line<int> getPointAlongLine() returning startPoint for all values

Hi,

I have noticed a bug I cannot seem to track down in the Line(int) function getPointAlongLine(). It seems to be only returning the start point of the line for any distance value. Below is some demo code, and the printout of that code.

This works as expected with Line(float)

Any help would be appreciated!

Thanks,
Chris

juce::Line<int> line = juce::Line<int>({0, 0}, {10, 10});
int dist = line.getLength();
int interp = 1;
int currentDist = interp;

while (currentDist < dist)
{
    juce::Point<int> point = line.getPointAlongLine(currentDist);
    DBG("Point: " << point.toString() << " Dist: " << currentDist << " Length: " << dist);
    currentDist += interp;
}

DBG Produces:

JUCE v6.0.5
Point: 0, 0 Dist: 1 Length: 14
Point: 0, 0 Dist: 2 Length: 14
Point: 0, 0 Dist: 3 Length: 14
Point: 0, 0 Dist: 4 Length: 14
Point: 0, 0 Dist: 5 Length: 14
Point: 0, 0 Dist: 6 Length: 14
Point: 0, 0 Dist: 7 Length: 14
Point: 0, 0 Dist: 8 Length: 14
Point: 0, 0 Dist: 9 Length: 14
Point: 0, 0 Dist: 10 Length: 14
Point: 0, 0 Dist: 11 Length: 14
Point: 0, 0 Dist: 12 Length: 14
Point: 0, 0 Dist: 13 Length: 14

Well spotted.
The problem appears to be in the implementation of getPointAlongLine(), which implicitly assumes that the type for coordinates is a floating point number and doesn’t work well with ints.

In favour of the developers must be said that only in special circumstances (i.e. perfectly vertical or horizontal lines) getPointAlongLine() would return precise int points laying on the line at integer distances. In all other cases, some rounding would be involved.

Your best bets for workarounds are:

  1. you calculate the proportion along the line yourself as a float (float proportion = currentDist / dist), then and use it as argument for getPointAlongLineProportionally()
  2. swtich your line and variables to float, then convert the points that you obtain to the nearest int coordinates (if you really need to) using Point::toInt() or roundToInt() depending on how you want your conversion to happen.