Check if point is near a list of vectors?

I am appending points with a modal operator & would like to check if the last point is near the other points. Any way of doing this with distance detection? or length?

This is so that if it is near other points, the raycast location changes

Thank you for your time.

Perhaps with a KD Tree:
https://docs.blender.org/api/current/mathutils.kdtree.html

1 Like

You can measure the length of the relative vector between two points like this:

import bpy
import math
import mathutils

refPoint = mathutils.Vector((0.0,0.0,1.0))
obj = bpy.context.object     
mesh = obj.data
tresh = 3
for v in mesh.vertices:
    if (refPoint - v.co).length > tresh:
        print("too far away")
1 Like

A kdtree search will be significantly faster than manual comparison

1 Like

Well, building and balancing a kd-tree itself also takes time.What suits best here is depending on what he wants , repeating spatial searches for the nearest points, or some sort of clustering, weighting over all points, or whatever.

1 Like

Awesome, I’ll look into that example & the kdtree. The points are not real mesh yet, just vectors being stored. For example: <Vector (-1.0481, 1.0129, 0.5050)>

So when I am drawing out the points, the last point has to check if it is going over an existing points and if its does, i need the raycast hit location to offset over those points so I can draw on top of them.

uEhf3Wccn0

last_point = self.points[len(self.points)-1]
    
 
proximity = 0
for i in range(len(self.points)):
    proximity += ( hit - self.points[i] ).length