Use a spatial data structure such as Quadtree to save your points. Each node in the box has a bounding box and a set of 4 child nodes and a list of points (empty, except for leaf nodes). Points are stored in leaf nodes.
Point quadrit is an adaptation of a binary tree used to represent two-dimensional point data. He shares the features of all quadrants, but is a true tree, since the center of the unit is always on the point. The shape of the tree depends on the processing order. This is often very effective when comparing two-dimensional ordered data points, usually working in O (log n) time .
For each point, maintain a set of all points that are independent of that point.
Insert all your points into the quadrant, then iterate over the points and use the quadrant to find points that are independent of each:
main() { for each point p insert p into quadtree set p set to empty for each point p findIndependentPoints(p, root node of quadtree) } findIndependentPoints(Point p, QuadTreeNode n) { Point f = farthest corner of bounding box of n if distance between f and p < 1 return // none of the points in this node or // its children are independent of p for each point q in n if distance between p and q > 1 find intersection r of q set and p set if r is non-empty then p, q, r are the 3 points -> ***SOLVED*** add p to q set of independent points add q to p set of independent points for each subnode m of n (up 4 of them) findIndependentPoints(p, m) }
You can speed it up:
find intersection r of q set and p set
saving each set as a quadrant. Then you can find the intersection by searching q quadtree for a point independent of p using the same technique:
// find intersection r of q set and p set: // r = findMututallyIndependentPoint(p, q quadtree root) Point findMututallyIndependentPoint(Point p, QuadTreeNode n) { Point f = farthest corner of bounding box of n if distance between f and p < 1 return // none of the points in this node or // its children are independent of p for each point r in n if distance between p and r > 1 return r for each subnode m of n (up 4 of them) findMututallyIndependentPoint(p, m) }
An alternative to using Quadtrees is to use Kd trees , which produces more balanced trees, where each leaf node is a similar depth from the root. The algorithm for finding independent points in this case will be the same, except that there will only be up to 2 in the data structure. not 4 child nodes for each node, but the bounding fields at each level will have a variable size.