Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to find knot indexes after getting getNearestPosition #36

Open
turkerfatih opened this issue Jun 12, 2024 · 1 comment
Open

How to find knot indexes after getting getNearestPosition #36

turkerfatih opened this issue Jun 12, 2024 · 1 comment

Comments

@turkerfatih
Copy link

Let's say I want to know which indexes the result point of getNearestPositionis between. As there any way?

@kjerandp
Copy link
Owner

kjerandp commented Jun 18, 2024

From the response of getNearestPosition you have a value for "u". This is the uniform position along the curve (parameterized by curve length). You can use this value to get the time along the curve ("t"), using the getTimeFromPosition function. The time along the curve is directly mapped to point indices, so you can find it's lower index by multiplying it by the number of points if your curve is closed, or number of points - 1 if it's not closed:

Note that this will give you the index as a decimal number, and unless you floor the value, the fraction part will tell you how far you are from the lower bound index to the next.

Something like this:
const { u } = interp.getNearestPosition(point)
const t = interp.getTimeFromPosition(u)
const index = Math.floor(t * (interp.getPoints().length - 1)) // assumes non-closed curve

Maybe I'll add a function to the lib for this purpose if anyone finds it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants