HarryStevens / geometric

A JavaScript library for doing geometry.
https://www.npmjs.com/package/geometric
MIT License
970 stars 49 forks source link

Make lineInterpolate handle values outside of [0; 1] #42

Closed yannbriancon closed 1 year ago

yannbriancon commented 1 year ago

In the library, lines are actually line segments. Current implementation of lineInterpolate gives an interpolator that returns points outside of the line segment if values passed are outside the interval [0;1].

export function lineInterpolate(line){
  return t => t === 0 ? line[0] : t === 1 ? line[1] : pointTranslate(line[0], lineAngle(line), lineLength(line) * t);
}

The following change would allow passing any number value and get the appropriate point on the line segment:

export function lineInterpolate(line){
  return t => t <= 0 ? line[0] : t >= 1 ? line[1] : pointTranslate(line[0], lineAngle(line), lineLength(line) * t);
}
HarryStevens commented 1 year ago

Changing the function like that would constitute a breaking change, so I'd prefer not to do that.

However, what if I added a second argument, clamp, a boolean. It would default to false, but if you set it to true, it would restrict the input t to [0, 1]?

yannbriancon commented 1 year ago

Thanks for the new parameter 👍