My answer is based on this post where the concept of cross-product and norm are considered. This solution applies also to an infinite line like yours, where it's constructed starting from two points.
import numpy as np
def dist_array_line(a, l1, l2, threshold):
"""
a : numpy.array
Array of points of shape (M,2)
M is the total number of points to test
l1 : numpy.array
Array of shape (2,) indicating the first point standing on the line
l2 : numpy.array
Array of shape (2,) indicating the second point standing on the line
threshold : float
Maximum distance allowed between line and points
Returns
numpy.array
Array of shape (M,) with True/False indicating whether the points in `a`
are within/outside the rectangle around the line
"""
distances = np.abs(np.cross(a - l1, a - l2)) / np.linalg.norm(l1 - l2)
return (distances < threshold)
If you want to return the actual distances instead of a True/False array, just make the function return the distances
object.
Example
# points on the line
p1 = np.array([298358.3258395831, 5625243.628060675])
p2 = np.array([298401.1779180078, 5625347.074197255])
# array of points to test
my_arr = np.array([
[298359.3258395831, 5625243.628060675],
[298368.3258395831, 5625243.628060675],
[(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2]
])
dist_array_line(my_arr, p1, p2, threshold=5.)
# array([ True, False, True])