You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: QuadTree/README.md
+16-17
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ A quadtree is a [tree](https://github.com/raywenderlich/swift-algorithm-club/tre
6
6
7
7
### Problem
8
8
9
-
Consider the following problem: your need to store a number of points (each point is a pair of `X` and `Y` coordinates) and then you need to answer which points lie in a certain rectangular region. A naive solution would be to store the points inside an array and then iterate over the points and check each one individually. This solution runs in O(n) though.
9
+
Consider the following problem: your need to store a number of points (each point is a pair of `X` and `Y` coordinates) and then you need to answer which points lie in a certain rectangular region. A naive solution would be to store the points inside an array and then iterate over the points and check each one individually. This solution runs in O(n) though.
10
10
11
11
### A Better Approach
12
12
13
-
Quadrees are most commonly used to partition a two-dimensional space by recursively subdividing it into four regions(quadrants). Let's see how we can use a Quadtree to store the points.
13
+
Quadtrees are most commonly used to partition a two-dimensional space by recursively subdividing it into four regions(quadrants). Let's see how we can use a Quadtree to store the points.
14
14
15
15
Each node in the tree represents a rectangular region and stores a limited number(`maxPointCapacity`) of points that all lie in its region.
16
16
@@ -40,7 +40,7 @@ class QuadTreeNode {
40
40
init(rect: Rect) {
41
41
self.rect= rect
42
42
}
43
-
43
+
44
44
...
45
45
}
46
46
@@ -52,11 +52,11 @@ extension QuadTreeNode {
52
52
53
53
@discardableResult
54
54
funcadd(point: Point) ->Bool {
55
-
56
-
if!rect.containts(point: point) {
55
+
56
+
if!rect.contains(point: point) {
57
57
returnfalse
58
58
}
59
-
59
+
60
60
switch type {
61
61
case .internal(let children):
62
62
// pass the point to one of the children
@@ -75,7 +75,7 @@ extension QuadTreeNode {
75
75
}
76
76
returntrue
77
77
}
78
-
78
+
79
79
privatefuncsubdivide() {
80
80
switch type {
81
81
case .leaf:
@@ -105,33 +105,33 @@ To find the points that lie in a given region we can now traverse the tree from
105
105
classQuadTree {
106
106
107
107
...
108
-
108
+
109
109
let root: QuadTreeNode
110
-
110
+
111
111
publicfuncpoints(inRectrect: Rect) -> [Point] {
112
112
return root.points(inRect: rect)
113
113
}
114
114
}
115
115
116
116
extensionQuadTreeNode {
117
117
funcpoints(inRectrect: Rect) -> [Point] {
118
-
118
+
119
119
// if the node's rect and the given rect don't intersect, return an empty array,
120
120
// because there can't be any points that lie the node's (or its children's) rect and
@@ -154,7 +154,6 @@ Both adding a point and searching can still take up to O(n) in the worst case, s
154
154
155
155
Displaying a large amount of objects in a MapView - a great use case for a Quadtree ([Thoughtbot Article](https://robots.thoughtbot.com/how-to-handle-large-amounts-of-data-on-maps))
156
156
157
-
More info on [Wiki](https://en.wikipedia.org/wiki/Quadtree)
157
+
More info on [Wikipedia](https://en.wikipedia.org/wiki/Quadtree)
158
158
159
159
*Written for Swift Algorithm Club by Timur Galimov*
0 commit comments