splinebox.spline_curves.Spline.
distance#
- Spline.distance(points, return_t=False)#
Computes the distance of point from the spline.
- Parameters:
- pointsnumpy.array
Array with the coordinates of one or multiple point(s).
- return_tbool
Whether to return the paramter t of the spline. spline(t) gives the location on the spline closest to the point.
- Returns:
- distancefloat
The distance between the point and the spline.
- tfloat
Only returned if return_t=True. This is the parameter corresponding to the location on the spline closest to the point.
Examples
>>> import splinebox >>> import numpy as np >>> import matplotlib.pyplot as plt
We start by creating a spline
>>> spline = splinebox.Spline(M=4, basis_function=splinebox.B3(), closed=False) >>> spline.control_points = np.array([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0], [5, 1]])
We can compute the distance to a point as follows:
>>> point = np.array([2.1, 0.8]) >>> distance, closest_t = spline.distance(point, return_t=True)
To find the closest point on the spline we simply evaluate the spline at the closest_t
>>> closest_point = spline(closest_t)
The result can be checked in a plot:
>>> plt.plot(vals[:, 0], vals[:, 1]) >>> plt.scatter(point[0], point[1]) >>> plt.plot([closest_point[0], point[0]], [closest_point[1], point[1]], linestyle="--") >>> plt.gca().set_aspect("equal") >>> plt.show()