Finding the squares in a plane given n points
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computational geometry, one of the intriguing problems is finding squares in a plane given a set of points. This problem is significant in computer graphics, image processing, and pattern recognition. In this article, we explore various algorithms and strategies to effectively identify squares within a point set in a two-dimensional plane.
Problem Definition
Consider a set of `n` distinct points in a 2D plane. The task is to determine all possible sets of four points that can form the vertices of a square. It's important to note that the square need not be aligned with the axis; it may be tilted.
A square can be defined by four properties:
- It has four sides of equal length.
- It has diagonals of equal length.
- The diagonals bisect each other at right angles.
- The segments connecting any two pairs of opposite points are equal and parallel.
Mathematical Foundations
Given two points and , the distance between them, , can be calculated using the Euclidean distance formula:
A given quadrilateral with four sides, if it's a square, will have two properties:
- All sides have equal length.
- Both diagonals have equal length.
Using vector dot products, for two vectors $\vec\{AB\}$ and $\vec\{AD\}$, the condition for orthogonality (90-degree angle) is:
Algorithmic Approach
To find all squares from a set of `n` points, a brute-force approach considers all combinations of four points from those available, leading to a time complexity of . However, optimizations can be made:
- Sort and Use Geometry for Verification: Consider optimizing by validating if for any point , there exists three other points such that: • • Diagonals
- Utilizing `Hash` Maps: To reduce computational cost: • Store distances between all pairs of points in a hash table, mapping distance to the pair of points. • For each pair of points (serving as one potential side of the square), look for other points that can complete the necessary geometric conditions.
- Nested Loop Elimination: By fixing one point and checking combinations of the remaining points, the complexity can be reduced significantly. By iterating over potential diagonals first, the problem turns into verifying side conditions, which can often be parallelized or dealt with using hashing techniques.
Example
Let's assume a simple set of points: . The only square that can be formed here is:
- Point Set: • All sides: • Diagonal:
Complexity Analysis
• Brute Force: Directly check all four-point combinations: . • Optimized Approach: Use hash maps and geometry properties to potentially achieve in efficient implementations.
Key Points Summary
| Aspect | Details |
| Problem Statement | Identify squares from a set of given n points |
| Mathematical Basis | Euclidean distances, orthogonality using dot products |
| Brute Force Complexity | due to combination checks |
| Optimized Approaches | Use of hash maps, reduce redundant checks |
| Example | Simple points: four vertices square |
| Complexity Improvement | Potentially with optimizations |
Subtopics for Further Exploration
- Handling Precision and Floating Points: Precision issues can arise with distance calculations due to floating-point arithmetic.
- Applications in Image Processing: Detecting squares can be related to template matching problems in images where squares symbolize certain patterns.
- Extension to Higher Dimensions: Discuss how similar principles could extend to cubes in 3D spaces.
The search for squares in a plane is not just a theoretical exercise but is practically important across various domains requiring geometric interpretation. Leveraging tools from computer science and mathematics provides robust solutions applicable to real-world problems.
Related reading
- Finding the total number of set-bits from 1 to n
- finding the width of a binary tree
- Finding three elements in an array whose sum is closest to a given number
- Finding two non-subsequent elements in array which sum is minimal
- Finding unreachable sections of a 2D map
- Finding whether a point lies inside a rectangle or not
- Finding unique numbers from sorted array in less than On
- Fingerprint matching/recognition algorithms/implementations

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.