Determine points within a given radius algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The problem of determining points within a given radius from a specific point is a common computational geometry challenge. This task has applications in areas such as geographic information systems (GIS), nearest neighbor searches, clustering, and many others. This article delves into the technical aspects of the algorithm, explores potential optimizations, and provides examples to elucidate its workings.
Basic Concept
The basic concept involves examining a set of points in a two-dimensional (or three-dimensional) space and identifying those that lie within a specified radius from a central point, often termed as the "query point" or "origin." Mathematically, for a set of points and a query point , we define the radius . Our goal is to find all points such that the Euclidean distance .
Euclidean Distance
The Euclidean distance between two points and in a 2D space is calculated as:
In a 3D space, incorporating the -coordinates, the formula becomes:
Algorithm
The simplest algorithmic approach is a brute-force method, where each point is individually checked against the query point to determine if it falls within the specified radius. Although straightforward, this method is computationally expensive with a time complexity of , where is the number of points.
Algorithm Steps
- Input: Set of points , query point , radius .
- Output: Subset of points that are within the radius of .
- Procedure: • Initialize an empty set . • For each point in : • Calculate the Euclidean distance . • If , add to set . • Return .
Optimizations
- Spatial Indexing: Structures such as k-d trees or R-trees organize data to allow for faster querying. These structures reduce the average-case time complexity compared to the brute-force approach.
- Precomputed Squared Distances: Avoid computing square roots by comparing squared distances instead, i.e., check if .
- Plane Sweep Algorithms: Efficiently handle points by sorting and progressively considering points in a line or plane, reducing unnecessary distance calculations.
Example
Consider points , query point , and radius .
- Calculate for each point: • For : (False) • For : (True) • For : (False) • For : (False)
- Points within the radius are .
Summary Table
| Aspect | Explanation |
| Brute-Force Complexity | - Evaluates all points individually. |
| Optimized Structures | Utilizes structures like k-d trees to improve search time. |
| Distance Calculation | Uses Euclidean distance, improved by comparing squared values. |
| Spatial Techniques | Plane sweep and spatial partitioning optimize queries. |
Applications
The algorithm finds numerous applications, including:
• Geospatial Analysis: Finding locations within a geographic radius. • Data Clustering: Identifying clusters of points based on proximity. • Recommendation Systems: Suggesting nearby points of interest.
By implementing optimizations and using efficient data structures, the performance and applicability of determining points within a given radius can be significantly enhanced, broadening its usability across various domains.
Related reading
- Determine the combinations of making change for a given amount
- Determine the most common occurrence in an array
- Determine whether integer is between two other integers
- Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?
- Determining Floating Point Square Root
- Determining if a sphere intersects an object or not
- Determine Whether Two Date Ranges Overlap
- Determining if an unordered vectorT has all unique elements

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.