Hello Fangyuan Wei,
As per my understanding, you want to get the K nearest neighbors of a point in 3D space with restricted directions.
You can follow these steps to get the K nearest neighbors of a point in 3D space with restricted directions:
1. Calculate the Euclidean distance between the given point and all other points in the dataset.
2. Sort the distances in ascending order to identify the closest points.
3. Initialize an empty list to store the nearest neighbors.
4. Iterate through the sorted distances and corresponding points until the desired number of neighbors (K) is reached.
5. For each point, calculate the angle between the vector formed by the given point and the current point, and the vector formed by the given point and each neighbor in the list.
6. If the angle between the two vectors is within the desired restricted angle range (e.g., 45 degrees), add the current point to the list of nearest neighbors.
7. Once the list of nearest neighbors contains K points, you can stop iterating and return the list.
Note that the angle calculation can be done using the dot product formula:
angle = (dot_product(v1, v2) / (norm(v1) * norm(v2)))
Where `v1` is the vector formed by the given point and the current point, and `v2` is the vector formed by the given point and each neighbor in the list.
Please note that this approach assumes you have a dataset of points in 3D space and their corresponding coordinates.
For more information regarding ‘dot_product’, you can refer following links:
I hope it helps!